Link to home
Start Free TrialLog in
Avatar of JReam
JReamFlag for United States of America

asked on

Remote Desktop Gateway , getting remote clients IP address programmatically

Our Clients have always directly connected to our RDS via RDP 3389.   We host and provide one primary application for them.     All good.  

We have always been able to get the remote clients IP address programmatically with API calls code similar to :

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32.dll" _
      Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, ByVal SessionID As Long, _
      ByVal WTSInfoClass As Long, ByRef ppBuffer As Long, ByRef pBytesReturned As Long) As Long

    lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSCLIENTADDRESS, ppBuffer, pBytesReturned)

Open in new window

We recently set up an RD Gateway for an alternate access path for those clients who are firewalled 3389 on their end.  So far so good.   Except that we can't seen to get their correct remote IP address.  Code above returns some wacky and incorrect IP addresses.  We know that the real IP address is there somewhere because we can see it on the RD Gateway server role Monitoring properties.  

We did some Google searches and think that me may need to query the WMI class Win32_TSGatewayConnection.  Info Source.    Our app is written in VB6 which makes API programming a tad tedious but always doable.  We usually are able to find examples of API programming online, but we cant seem to find any syntax examples of  Win32_TSGatewayConnection.  Can you help us program the code syntax for Win32_TSGatewayConnection?
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JReam

ASKER

Hi David -  I've been trying literally for the last few hours to get your code to work.  

I'm this close:    

Set address_results = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\TerminalServices\MS_409") 

Set address_results = obj.ExecQuery("SELECT * FROM Win32_TSGatewayConnection")
Debug.Print =  address_results.Count 
For Each objItem In address_results
    Debug.Print  objItem.ClientAddress
Next

Open in new window


Where strComputer the name of our RD Gateway host.  

The code runs error free.  But address_results.COUNT is always = 0.     I would expect this Count to be a least a few items as we currently have users active running thru the RD gateway to our RDS host.
Avatar of JReam

ASKER

David thanks for enlightening me about  Win32_TSGatewayConnection.  

David, I'd sure like to ask you  about comment:  "Why are you still using VB6, this product went end of life 7 years ago.  I know that microsoft has said that the runtime will be supported until 2024 (9 more years). "   I'll PM you.