develooper Front page | perl.perl6.users | Postings from November 2022

NativeCall C++ structure question

Thread Next
ToddAndMargo via perl6-users
November 21, 2022 13:01
NativeCall C++ structure question
Message ID:
Hi All,

Windows ChromeBook Edition (W11-22H2).

I have been doing a lot of head scratching here.

I have a project were I need to use

     BOOL WTSEnumerateSessionsA(
       [in]  HANDLE             hServer,
     # WTS_CURRENT_SERVER_HANDLE to use the RD Session Host server that 
hosts your application.
       [in]  DWORD              Reserved,
       [in]  DWORD              Version,
       [out] PWTS_SESSION_INFOA *ppSessionInfo,
       [out] DWORD              *pCount

to dig out a pointer (*ppSessionInfo) to a
C++ OOP data structure, which is repeated
pCount times.  (No problem coding the above.)

The data structure is
     typedef struct _WTS_SESSION_INFOA {
        DWORD    SessionId;            # 4 bytes
        LPSTR    pWinStationName;      # 4 or 8 or 12 bytes
        WTS_CONNECTSTATE_CLASS State;  # 4 or 8 bytes maybe

"_WTS_SESSION_INFOA" is 12 bytes in x86 and
24 bytes in x64:

     int nSize1 = sizeof WTS_SESSION_INFOA;
     // 12 in x86, 24 in x64

What I need to do is shown in C++

     WTSEnumerateSessionsA(WTS_CURRENT_SERVER, 0, 1, &pwsi, &dwCount)

      for (DWORD i = 0; i < dwCount; i++)
          if (pwsi[i].State == WTSActive)
              dwSession = pwsi[i].SessionId;

Basically, I need to walk though the C++ structure
looking for State == WTSActive (0), read the
value of SessionId, exit the loop, and return
the value of SessionId.

How in the world do I use NativeCall to read
"_WTS_SESSION_INFOA"'s structure?

I will only be operating in 64 bit, so I take it
I have to create a Buf of 24 bytes.  Then
take the buffer apart using little endian.

And the [i] in the C++ code will be me incrementing
*pCount by 24 bytes each successive read.

Is there an easier way to do this?

Many thanks,

I have to do the above becasue the "SessionID"
is reported incorrectly by M$'s other API's
if you are running your program from the Task

Oh and I almost forgot:  AAAAA HHHHH !!!!!

Computers are like air conditioners.
They malfunction when you open windows

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About