0
我在Windows 10中使用西雅圖。奇怪的修改阻止了這種訪問衝突的原因是什麼?
我嘗試打印公共IP地址和網關地址。
訪問衝突發生在最後一行。
當我刪除第1行時,它運行良好。
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Win.ComObj, Vcl.StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, winapi.iphlpapi, winapi.iptypes, winapi.winsock;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
IdHTTP1: TIdHTTP;
procedure FormCreate(Sender: TObject);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
_IPAdapterAddresses: PIP_ADAPTER_ADDRESSES;
_SizePointer: Integer;
begin
Label2.Caption := Label2.Caption + ' ' + IdHTTP1.Get'http://ipinfo.io/ip'); // line 1
_IPAdapterAddresses := AllocMem(_SizePointer);
GetAdaptersAddresses(2, 128, nil, _IPAdapterAddresses, @_sizepointer);
Label1.Caption := Label1.Caption + ' ' + string(inet_ntoa(_IPAdapterAddresses^.FirstGatewayAddress.Address.lpSockaddr.sin_addr)); // access violation
end;
當我修改最後一行,如下所示,它運作良好。
Label1.Caption := {Label1.Caption + ' ' + }string(inet_ntoa(_IPAdapterAddresses^.FirstGatewayAddress.Address.lpSockaddr.sin_addr));
end;
我根據一個類似的例子設置大小15000在這裏,它工作良好。我試圖使用sizeof,但輸出爲4.即使在我看到msdn的示例(sizeof(PIP_ADAPTER_ADDRESSES))後,我無法使用delphi獲取PIP_ADAPTER_ADDRESSES的大小。並釋放了記憶。感謝大衛。 –
你在問一個指針的大小,而且你有一個32位的進程,那就是4.沒有任何理由要你去查詢內存塊的大小,反正你也做不到。你只是分配了塊,你知道它有多大。另外,請不要在你的問題中使用代碼。它不檢查錯誤。 –