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


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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
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).  You can't say that visual studio is too expensive (visual studio community is available)

    Dim ipaddress As String
    Dim address_query As String
    Dim address_results As Object
    addrress_query = "SELECT * FROM Win32_TSGatewayConnection"
    Set addrress_results = _
    For Each info In addrress_results
        ipaddress = info.ClientAddress"
    Next info

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JReamAuthor Commented:
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

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.
JReamAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Remote Access

From novice to tech pro — start learning today.