Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi WTSQuerySessionInformation IPv6 address (https://www.delphipraxis.net/103329-wtsquerysessioninformation-ipv6-address.html)

Remko 14. Nov 2007 13:39


WTSQuerySessionInformation IPv6 address
 
With the MSDN-Library durchsuchenWTSQuerySessionInformation API you can get the Client IP address of a Terminal Server client. On Vista and Windows Server 2008 this returns an IPv6 address in a pointer to a WtsClientAddress structure:
Code:
  _WTS_CLIENT_ADDRESS = record
    AddressFamily: DWORD;          // AF_INET, AF_IPX, AF_NETBIOS, AF_UNSPEC
    Address: array [0..19] of BYTE; // client network address
  end;
I want to convert the Address to a string but don't know how. I tried to use MSDN-Library durchsuchenRtlIpv6AddressToString API this succeeds but returns bogus.

My IPConfig:
IPv6 Address. . . . . . . . . . . : 1111:2222:1111:2222:1111:2222:1111:2222
Link-local IPv6 Address . . . . . : fe80::fd16:38fb:af7a:66c4%10

Dump of the Address byte array:
182530000010000000000710
Hex Dump: 7C F4 12 00 8B 1D 49 00 38 F4 12 00 AF 1E 49 00 7C F4 12 00

Anyone?

Remko 16. Nov 2007 09:00

Re: WTSQuerySessionInformation IPv6 address
 
push

Muetze1 16. Nov 2007 16:21

Re: WTSQuerySessionInformation IPv6 address
 
And what IPv6 address do you expect? Can you write the expected IPv6 address for the given hex dump please?

Remko 16. Nov 2007 16:23

Re: WTSQuerySessionInformation IPv6 address
 
That would be the ones from the listed ipconfig:
My IPConfig:
IPv6 Address. . . . . . . . . . . : 1111:2222:1111:2222:1111:2222:1111:2222
Link-local IPv6 Address . . . . . : fe80::fd16:38fb:af7a:66c4%10

Muetze1 16. Nov 2007 16:29

Re: WTSQuerySessionInformation IPv6 address
 
So, the returned structure contains crap. The call to WTSQuerySessionInformation seems to fail - did you checked the result value and even (if it say no error) check it deeply? Checking deeply meaning to call SetLastError(0); directly before calling the WTSQuerySessionInformation() and checking GetLastError() directly after the call. Are there any errors?

Remko 16. Nov 2007 16:31

Re: WTSQuerySessionInformation IPv6 address
 
I'm don't think the function failed, but just to be sure I will try again and make a new dump.

Remko 16. Nov 2007 21:51

Re: WTSQuerySessionInformation IPv6 address
 
This is odd, I tried the function 3 times and 3 times successfull. But I get 3 different values :wall:

1st four bytes is the addressfamily which always returns 0, then follows the clientaddress:
Code:
17 00 00 00 D8 03 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 49 00
17 00 00 00 04 06 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 49 00
17 00 00 00 A1 03 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 49 00

Muetze1 17. Nov 2007 01:00

Re: WTSQuerySessionInformation IPv6 address
 
Ok, can you show the code around the call to WTSQuerySessionInformation()? So memory & pointer preparation, etc?

Remko 19. Nov 2007 12:47

Re: WTSQuerySessionInformation IPv6 address
 
This is a helper function:
Delphi-Quellcode:
procedure TJwWTSSession.GetSessionInfoPtr(const WTSInfoClass: _WTS_INFO_CLASS;
  var ABuffer: Pointer);
var dwBytesReturned: DWORD;
  Res: Bool;
begin
  Res :=
{$IFDEF UNICODE}
    WTSQuerySessionInformationW(GetServerHandle, FSessionId, WTSInfoClass,
      ABuffer, dwBytesReturned);
{$ELSE}
    WTSQuerySessionInformationA(GetServerHandle, FSessionId, WTSInfoClass,
      ABuffer, dwBytesReturned);
{$ENDIF}
  // function always returns an error 997: overlapped IO on session 0
  if (not Res) and (FSessionId <> 0) then
  begin
    raise EJwsclWinCallFailedException.CreateFmtWinCall(RsWinCallFailed,
      'WTSQuerySessionInformation', ClassName, RSUNTerminalServer, 0, True,
      'WTSQuerySessionInformation', ['WTSQuerySessionInformation']);
  end;
end;
And this is where the ClientAddress is retrieved, as you can see I'm dumping this to a .bin file now (which is what I pasted above)
Delphi-Quellcode:
function TJwWTSSession.GetClientAddress: TJwString;
var ClientAddressPtr: PWtsClientAddress;
begin
  ZeroMemory(ClientAddressPtr, SizeOf(TWtsClientAddress));
  GetSessionInfoPtr(WTSClientAddress, Pointer(ClientAddressPtr));
  {Note that the first byte of the IP address returned in the ppBuffer
   parameter will be located at an offset of 2 bytes from the start of
   the Address member of the returned WTS_CLIENT_ADDRESS structure.}
  case ClientAddressPtr^.AddressFamily of
    AF_INET:
      Result := Format('%d.%d.%d.%d', [ClientAddressPtr^.Address[2],
        ClientAddressPtr^.Address[3], ClientAddressPtr^.Address[4],
        ClientAddressPtr^.Address[5]]);
    AF_INET6:
//      Result := 'IPv6 address not yet supported';
      begin
        with TFileStream.Create('C:\temp\AF_INET6.bin', fmCreate) do
        try
          Write(ClientAddressPtr^, SizeOf(TWtsClientAddress));
        finally
          Free;
        end;
      end;
    AF_IPX:
      Result := 'IPX is not supported';
    AF_NETBIOS:
      Result := 'NETBIOS is not supported';
    AF_UNSPEC:
      Result := '';
  end;
  WTSFreeMemory(ClientAddressPtr);
end;

Muetze1 19. Nov 2007 18:58

Re: WTSQuerySessionInformation IPv6 address
 
1. You pass the local variable ClientAddressPtr to the call, but as far as I can read from the documentation, you have to pass the pointer to this variable. So call it with @ClientAddressPtr.

2. What about this comment
Zitat:

// function always returns an error 997: overlapped IO on session 0
and what does dwBytesReturned contain after the call?


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:02 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz