Enzo Costantini
Enzo Costantini

Reputation: 167

Delphi XE and Winsock

I'm upgrading my application from Delphi 2007 to Delphi XE. I have my personal written Socket component. In the new environment (XE) it is not working properly. The same code works in Delphi 2007.

Here's my code fragment:

uses WinSock;

procedure TForm1.GetProtocolClick(Sender: TObject);
var
  ProtoEnt: PProtoEnt;
  FProtocol: Integer;
begin
  FProtocol := IPPROTO_TCP;
  ProtoEnt := getprotobynumber(FProtocol);
  if Assigned(ProtoEnt)
    then ShowMessage(ProtoEnt.p_name);
end;

var
  WSAData: TWSAData;

procedure Startup;
begin
  if WSAStartup($0101, WSAData) <> 0
  then raise Exception.Create('WSAStartup');
end;

procedure Cleanup;
begin
  if WSACleanup <> 0
  then raise Exception.Create('WSACleanup');
end;

initialization
  Startup;
finalization
  Cleanup;
end.

ProtoEnt is always not Assigned (i.e. = nil)!!!!!

WHY?

I am going crazy to solve this problem... Thanks

Enzo

Upvotes: 2

Views: 2814

Answers (1)

kludg
kludg

Reputation: 27493

If you change your code a bit

procedure TForm1.GetProtocolClick(Sender: TObject);
var
  ProtoEnt: PProtoEnt;
  FProtocol: Integer;
begin
  FProtocol := IPPROTO_TCP;
  ProtoEnt := getprotobynumber(FProtocol);
  if Assigned(ProtoEnt)
    then ShowMessage(ProtoEnt.p_name)
    else ShowMessage(IntToStr(WSAGetLastError));
end;

you will see error code; after that check the WinSock error codes

I tried the next, it works perfectly on XE:

var
  WSAData: TWSAData;

procedure Startup;
begin
  if WSAStartup($0101, WSAData) <> 0
  then raise Exception.Create('WSAStartup');
end;

procedure Cleanup;
begin
  if WSACleanup <> 0
  then raise Exception.Create('WSACleanup');
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  ProtoEnt: PProtoEnt;
  FProtocol: Integer;
begin
  StartUp;
  FProtocol := IPPROTO_TCP;
  ProtoEnt := getprotobynumber(FProtocol);
  if Assigned(ProtoEnt)
    then ShowMessage(ProtoEnt.p_name)
    else ShowMessage(IntToStr(WSAGetLastError));
  CleanUp;
end;

Upvotes: 4

Related Questions