Article View: borland.public.cppbuilder.internet.socket
Article #344Re: Error handling in Indy 10
From: Remy Lebeau (TeamB)
Date: Wed, 16 Apr 2008 20:23
Date: Wed, 16 Apr 2008 20:23
96 lines
4496 bytes
4496 bytes
"Dennis Cote" <denniscote@gmail.com> wrote in message
news:480632fb$1@newsgroups.borland.com...
> I am now testing the application under Vista. It does not
> work under Vista and I believe the issue has to do with
> Administrator rights
You are using the TIdICMPClient component, which uses a RAW socket
internally (as opposed to using a TCP or UDP socket). Admin rights are
required in order to use RAW sockets on NT4 onwards (which means your code
should not have worked on XP, either, unless you are running as an admin).
Of course, Microsoft circumvents its own rules by allowing icmp.dll to use
RAW sockets without admin rights.
> I think some type casting must be needed to tell the
> compiler not to check the range of the result before
> passing it to CheckForSocketError() but I'm not
> sure how that is done in Delphi.
Rather than casting, you could disable range checking itself for
NewSocketHandle(). Indy does this in a couple of other places, for example:
function TIdStackBSDBase.NewSocketHandle(const ASocketType:
TIdSocketType;
const AProtocol: TIdSocketProtocol;
const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION;
const AOverlapped: Boolean = False): TIdStackSocketHandle;
begin
{$IFOPT R+}
{$DEFINE RestoreRangeCheckingON}
{$R-}
{$ENDIF}
Result := CheckForSocketError(WSSocket(IdIPFamily[AIPVersion],
ASocketType, AProtocol, AOverlapped));
{$IFDEF RestoreRangeCheckingON}
{$R+}
{$UNDEF RestoreRangeCheckingON}
{$ENDIF}
end;
Otherwise, you would have to hard-code the casting, which can be pretty
ugly:
function TIdStackBSDBase.NewSocketHandle(const ASocketType:
TIdSocketType;
const AProtocol: TIdSocketProtocol;
const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION;
const AOverlapped: Boolean = False): TIdStackSocketHandle;
begin
Result :=
TIdStackSocketHandle(CheckForSocketError(Integer(WSSocket(IdIPFamily[AIPVersion],
ASocketType, AProtocol, AOverlapped))));
end;
Here is a cleaner version:
function TIdStackBSDBase.NewSocketHandle(const ASocketType:
TIdSocketType;
const AProtocol: TIdSocketProtocol;
const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION;
const AOverlapped: Boolean = False): TIdStackSocketHandle;
begin
Result := WSSocket(IdIPFamily[AIPVersion], ASocketType, AProtocol,
AOverlapped);
CheckForSocketError(Integer(Result));
end;
> I also suspect this same error may exist in other places in the code.
Actually, it doesn't. Everywhere else that calls CheckForSocketError()
passes it an Integer.
> Can anyone say if this analysis is correct, and if so how the
> result should be cast from unsigned to signed without the
> range check?
Since CheckForSocketError() is expecting an Integer error code, not a
TSocket handle, I would suggest changing TIdStackBSDBase.NewSocketHandle()
to the following instead:
function TIdStackBSDBase.NewSocketHandle(const ASocketType:
TIdSocketType;
const AProtocol: TIdSocketProtocol;
const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION;
const AOverlapped: Boolean = False): TIdStackSocketHandle;
begin
Result := WSSocket(IdIPFamily[AIPVersion], ASocketType, AProtocol,
AOverlapped);
if Result = Id_INVALID_SOCKET then begin
RaiseLastSocketError;
end;
end;
Gambit
Technical Details
Message-ID:
<4806604b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!eweka.nl!lightspeed.eweka.nl!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mailReferences:
<480632fb$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,9330c485b2d28336 X-Google-Attributes: gid11508e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <480632fb$1@newsgroups.borland.com> Subject: Re: Error handling in Indy 10 Date: Wed, 16 Apr 2008 13:23:28 -0700 Lines: 98 Organization: Lebeau Software X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 NNTP-Posting-Host: 207.178.157.90 Message-ID: <4806604b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1208377419 207.178.157.90 (16 Apr 2008 13:23:39 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!eweka.nl!lightspeed.eweka.nl!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail