Jump to content
Dave Nottage

Obtaining client IP address from Windows Virtual Desktop app

Recommended Posts

I've found some code from an older repo (namely https://github.com/ccy/jedi-apilib) for what I want to achieve however I'm having trouble making it work. This is the code in question:

 

const
  WTS_CURRENT_SERVER_HANDLE = 0;
  WTS_CURRENT_SESSION = DWORD(-1);
  
type
  _WINSTATION_REMOTE_ADDRESS = record
    AddressFamily: DWORD;
    Port: WORD;
    Address: array [0..19] of Byte;
    Reserved: array[0..5] of Byte;
  end;
  PWINSTATION_REMOTE_ADDRESS = ^_WINSTATION_REMOTE_ADDRESS;
  TWinStationRemoteAddress = _WINSTATION_REMOTE_ADDRESS;
  PWinStationRemoteAddress = PWINSTATION_REMOTE_ADDRESS;

function WinStationQueryInformation(hServer: THandle; SessionId: DWORD; WinStationInformationClass: Cardinal; pWinStationInformation: PVOID;
  WinStationInformationLength: DWORD; var pReturnLength: DWORD): Boolean; stdcall;
  external 'winsta.dll' name 'WinStationQueryInformationW';
  
function GetWVDClientAddress: string;
var
  LBytesReturned: DWORD;
  LRemoteAddress: _WINSTATION_REMOTE_ADDRESS;
begin
  LBytesReturned := 0;
  ZeroMemory(@LClientAddress, SizeOf(LClientAddress));
  if WinStationQueryInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WinStationRemoteAddress, @LClientAddress, SizeOf(LClientAddress), LBytesReturned) then
  begin
    // Do something with LClientAddress here
  end
  else
    Result := SysErrorMessage(GetLastError);
end;

The call to WinStationQueryInformation is failing, and the message being returned by SysErrorMessage is: "The tag is invalid".

Is there something obvious that I'm missing?

Share this post


Link to post
On 2/8/2022 at 4:22 AM, qubits said:

looks like you have to enumerate through all sessions, don't think you can pass -1

That was it; thanks

Share this post


Link to post
On 2/10/2022 at 9:06 AM, Dave Nottage said:

That was it; thanks

Unfortunately, this gives the public facing IP address, which makes sense, however in my case isn't suitable.

 

What I am after is a way of obtaining a unique identifier for the "local" machine, by way of either one of the WTS functions or the Winstation functions.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×