X
xiaofi2001
Unregistered / Unconfirmed
GUEST, unregistred user!
这段程序主要是取出IPV6地址,并转换成用户可读的字符串,但是在转换时总是出错,就错在WSAAddressToString函数中!错误是,access violation at address00000000.我估计是参数格式没传对,但是能编译过去。网上找不到相关例子,达人帮忙,多谢了。
procedure TForm1.Button4Click(Sender: TObject);
var
m_data : TWSADATA ;
slist : lpsocket_ADDRESS_LIST;
storge : LPWsockaddr_storage;
s1 : TSOCKET;
buf : array[0..2047] of char;
dwBytesRet : DWORD;
ret,i,len,ats : integer;
t1 : char;
lpszAddressString : array[0..39] of char;
dwAddressStringLength : DWORD;
//lpProtocolInfo : LPWSAProtocol_Info;
begin
ZeroMemory(@lpszAddressString,40);
dwAddressStringLength := 40;
if (WSAStartup(MAKEWORD(2,2),m_data)<>0) then
begin
//初始化失败
MessageBox(Application.Handle, PChar('Init Failed'), '提示', MB_OK or MB_ApplModal or MB_ICONINFORMATION or MB_TOPMOST);
end;
s1 := socket(AF_INET6,SOCK_STREAM,IPPROTO_IP);
if(s1 = SOCKET_ERROR) then
begin
WSAGetLastError();
end;
ret := WSAIoctl(s1,SIO_ADDRESS_LIST_QUERY,NIL,0,@buf,2048,@dwBytesRet,NIL,NIL);
if(ret = SOCKET_ERROR) then
begin
WSAGetLastError();
end;
len := sizeof(Tsockaddr_storage);
slist := lpsocket_ADDRESS_LIST(@buf);
//StrpCopy(@slist,buf);
for i:=0 to slist^.iAddressCount do
begin
storge := Psockaddr_storage(slist.Address.lpSockaddr);
//StrpCopy(@storge,slist.Address.lpSockaddr);
WSAAddressToString(LPSOCKADDR(storge),len,nil,@lpszAddressString,dwAddressStringLength);
//if ats <> 0 then WSAGetLastError;
t1 := lpszAddressString[0];
if StrComp(PChar(t1),'2') = 0 then
begin
MessageBox(Application.Handle, lpszAddressString, '提示', MB_OK or MB_ApplModal or MB_ICONINFORMATION or MB_TOPMOST);
m_IP := lpszAddressString;
end;
end;
closesocket(s1);
WSACleanup();
end;
procedure TForm1.Button4Click(Sender: TObject);
var
m_data : TWSADATA ;
slist : lpsocket_ADDRESS_LIST;
storge : LPWsockaddr_storage;
s1 : TSOCKET;
buf : array[0..2047] of char;
dwBytesRet : DWORD;
ret,i,len,ats : integer;
t1 : char;
lpszAddressString : array[0..39] of char;
dwAddressStringLength : DWORD;
//lpProtocolInfo : LPWSAProtocol_Info;
begin
ZeroMemory(@lpszAddressString,40);
dwAddressStringLength := 40;
if (WSAStartup(MAKEWORD(2,2),m_data)<>0) then
begin
//初始化失败
MessageBox(Application.Handle, PChar('Init Failed'), '提示', MB_OK or MB_ApplModal or MB_ICONINFORMATION or MB_TOPMOST);
end;
s1 := socket(AF_INET6,SOCK_STREAM,IPPROTO_IP);
if(s1 = SOCKET_ERROR) then
begin
WSAGetLastError();
end;
ret := WSAIoctl(s1,SIO_ADDRESS_LIST_QUERY,NIL,0,@buf,2048,@dwBytesRet,NIL,NIL);
if(ret = SOCKET_ERROR) then
begin
WSAGetLastError();
end;
len := sizeof(Tsockaddr_storage);
slist := lpsocket_ADDRESS_LIST(@buf);
//StrpCopy(@slist,buf);
for i:=0 to slist^.iAddressCount do
begin
storge := Psockaddr_storage(slist.Address.lpSockaddr);
//StrpCopy(@storge,slist.Address.lpSockaddr);
WSAAddressToString(LPSOCKADDR(storge),len,nil,@lpszAddressString,dwAddressStringLength);
//if ats <> 0 then WSAGetLastError;
t1 := lpszAddressString[0];
if StrComp(PChar(t1),'2') = 0 then
begin
MessageBox(Application.Handle, lpszAddressString, '提示', MB_OK or MB_ApplModal or MB_ICONINFORMATION or MB_TOPMOST);
m_IP := lpszAddressString;
end;
end;
closesocket(s1);
WSACleanup();
end;