How to detect IPV6 address change in Delphi 7?

I was looking for a way to detect IPV6 address changes using delphi 7 on Windows XP and above. For Vista there is a Windows API that I know. I came across a code that claimed to do the same on XP. Here is the code below:

uses  WinSock2 ; //

procedure TForm1.Button1Click(Sender: TObject);
var
  s: TSocket;
  wsaD: TWSADATA;
  BytesReturned: u_long;
  PtrA: Pointer;
  Buffer: array[0..20] of INTERFACE_INFO;
  ret: Integer;
begin
  try
    FillChar(wsaD, SizeOf(wsaD), #0);
    FillChar(Buffer, SizeOf(Buffer), #0);
    WSAStartup($0101, wsaD);                      // Start WinSock,You should normally check for errors here

    {Create a WSA Socket}
    // s := WSASocketA(AF_INET6,SOCK_STREAM, IPPROTO_TCP, nil, 0, 0);

    s := Socket(AF_INET6, SOCK_STREAM, IPPROTO_IP);         // Open a socket
    if (s = INVALID_SOCKET) then
      Exit;

    PtrA := @bytesReturned;
    //   Ret :=  WSAIoCtl(s, SIO_ADDRESS_LIST_CHANGE, @Buffer,1024, @Buffer, 1024, PtrA, nil, nil);
    Ret :=  WSAIoCtl(s, SIO_ADDRESS_LIST_QUERY, nil,0, @Buffer, SizeOf(Buffer), PtrA, nil, nil);
    DebugLog('Ret Value = ' + SysErrorMessage(WSAGetLastError),0);
    if ret = 0 then
    begin
      // DebugLog('IP Address changed !',0);
      //Showmessage('Operation succeeded !')
    end
    else
    begin
      //DebugLog('No change found in IP Address',0);
      // showmessage(SysErrorMessage(WSAGetLastError));
    end;

    WSAcleanUp;
  except

  end;
end;

I get the error code as

"An invalid argument was specified."

When I change the control code to SIO_ADDRESS_LIST_QUERY, then the code works fine.

Can someone help me with this?

+5
source share

All Articles