Delphi XE 和 Winsock

发布于 2024-12-28 10:58:11 字数 805 浏览 3 评论 0原文

我正在将我的应用程序从 Delphi 2007 升级到 Delphi XE。我有我个人编写的 Socket 组件。在新环境(XE)中它不能正常工作。相同的代码在 Delphi 2007 中工作。

这是我的代码片段:

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 始终未分配(即 = nil)!!!!

为什么?

我要疯了才能解决这个问题...... 谢谢恩佐

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2025-01-04 10:58:11

如果您稍微更改一下代码,

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;

您将看到错误代码;之后检查 WinSock 错误代码

我尝试了下一个,它在 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文