ID of the current process

ID of the current process

Postby Natter » Tue Dec 21, 2021 12:56 pm

To find out the ID of the current process I used GetCurrentProcessId().

Code: Select all  Expand view
DLL32 function GCP_ID() AS LONG PASCAL FROM "GetCurrentProcessId" LIB "kernel32.dll "


However, GCP_ID() returns an empty value. What could be the problem ?
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current process

Postby karinha » Tue Dec 21, 2021 1:22 pm

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7794
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: ID of the current process

Postby Natter » Tue Dec 21, 2021 2:16 pm

Thaks, karinha ! that would be good, but GCP() returns "U", not a number :(
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current process

Postby Antonio Linares » Tue Dec 21, 2021 3:07 pm

This is working fine:

Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( GCP() )

return nil

DLL FUNCTION GCP() AS LONG FROM "GetCurrentProcessId" LIB "kernel32.dll"
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42059
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current process

Postby Natter » Tue Dec 21, 2021 5:02 pm

Thank you, Antonio! But for some reason this function doesn't work for me.
I'm trying to solve this problem:
Many years ago, in my applications, I made a re-launch check. That is, the application loads normally for the first time, and when trying to re-load, it is simply brought to the fore.
Now there is a need to launch the application via RDP. I.e. each user runs the same EXE file. The first user opens the application normally. When the application is opened by the second user, all processes are viewed and I read the owners of each of them (.getOwner()). If the user has administrator rights, everything is fine. Otherwise, when trying to execute the getParent of someone else's process, the application is automatically terminated. I do not know how to avoid this
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current process

Postby Jimmy » Wed Dec 22, 2021 1:23 am

hi,

"how" do you want to identify User when have a ProcessId :?:

Return value
The return value is the process identifier of the calling process.


---

i guess you need WTSQuerySessionInformationA function
https://docs.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsquerysessioninformationa
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1725
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: ID of the current process

Postby Natter » Wed Dec 22, 2021 6:36 am

"how" do you want to identify User when have a ProcessId

The PID of the process is unique. I am requesting a list of processes of the same name via WMI. If we are on a local computer, then there is no need to identify the owner of the application. If this is a shared resource, then you need to specify the owner of the process.
Using the method GetOwner()

Can you give an example of using the WTSQuerySessionInformationA function in xHarbour ?
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current process

Postby Jimmy » Thu Dec 23, 2021 1:08 am

hi,

i do not have (yet) a harbour Version, only Xbase++

1st Sample use BAP (Binary Access Package) to "simulate" Structure
( Xbase++ v1.9x does not have Structure, v2.x now have it )

2nd Sample is a CLASS and use OT4XB Syntax ( PeekByte() )

as i say i have not "translate" that Code to harbour yet

---

i´m not sure how WMI will work on remote PC ...

i do have a Email System where User get Update after Timeout.
so i can identify which User is Online ... but not which Process User is running


Code: Select all  Expand view
// DEFINES FOR GetTSInfo
#define  WTS_CURRENT_SERVER_HANDLE  0
#define  WTS_CURRENT_SESSION  -1

#define  SM_REMOTESESSION 0x1000
#define  PROCESS_QUERY_INFORMATION 0x400
#define  PROCESS_VM_READ 0x10

//Public Enum WTS_INFO_CLASS
#define  WTSInitialProgram        0
#define  WTSApplicationName       1
#define  WTSWorkingDirectory      2
#define  WTSOEMId                 3
#define  WTSSessionId             4
#define  WTSUserName              5
#define  WTSWinStationName        6
#define  WTSDomainName            7
#define  WTSConnectState          8
#define  WTSClientBuildNumber     9
#define  WTSClientName            10
#define  WTSClientDirectory       11
#define  WTSClientProductId       12
#define  WTSClientHardwareId      13
#define  WTSClientAddress         14
#define  WTSClientDisplay         15
#define  WTSClientProtocolType    16

// GetTSInfo
****************************************************************************
*********************
FUNCTION GetTSInfo()
LOCAL cTSInfo := ""
LOCAL cNewLine :=  (CHR(13)+CHR(10))
LOCAL Success
LOCAL pBuffer := 0
LOCAL BytesReturned := 0
LOCAL nDll
LOCAL nSessionID := 0
LOCAL xWTSInitialProgram := SPACE(100)+CHR(0)
LOCAL xWTSApplicationName := ""+CHR(0)
LOCAL xWTSWorkingDirectory := ""+CHR(0)
LOCAL xWTSOEMId := 0
LOCAL xWTSSessionId := 0
LOCAL xWTSUserName := SPACE(20)+CHR(0)
LOCAL xWTSWinStationName := ""+CHR(0)
LOCAL xWTSDomainName := ""+CHR(0)
LOCAL xWTSConnectState := 0
LOCAL xWTSClientBuildNumber := 0
LOCAL xWTSClientName := ""+CHR(0)
LOCAL xWTSClientDirectory := ""+CHR(0)
LOCAL xWTSClientProductId := 0
LOCAL xWTSClientHardwareId := 0
LOCAL xWTSClientAddress
LOCAL xWTSClientDisplay
LOCAL xWTSClientProtocolType := 0

LOCAL a_WTS_CLIENT_DISPLAY
LOCAL a_WTS_CLIENT_ADDRESS

LOCAL cStationName := ""
LOCAL cAddressStr := "", oSocket


/***************************************************
typedef struct _WTS_CLIENT_ADDRESS {
DWORD AddressFamily;
BYTE Address[20];
} WTS_CLIENT_ADDRESS, *PWTS_CLIENT_ADDRESS;
***************************************************/

a_WTS_CLIENT_ADDRESS := BaInit(2)
BaStruct(a_WTS_CLIENT_ADDRESS, 0)
BaStruct(a_WTS_CLIENT_ADDRESS, SPACE(20))
xWTSClientAddress := BaAccess(a_WTS_CLIENT_ADDRESS)


/***************************************************
typedef struct _WTS_CLIENT_DISPLAY {
DWORD HorizontalResolution;
DWORD VerticalResolution;
DWORD ColorDepth;
} WTS_CLIENT_DISPLAY, *PWTS_CLIENT_DISPLAY;
***************************************************/


a_WTS_CLIENT_DISPLAY := BaInit(3)
BaStruct(a_WTS_CLIENT_DISPLAY, 0)
BaStruct(a_WTS_CLIENT_DISPLAY, 0)
BaStruct(a_WTS_CLIENT_DISPLAY, 0)
xWTSClientDisplay := BaAccess(a_WTS_CLIENT_DISPLAY)

nDll := DllLoad("WTSAPI32.DLL")

IF nDll != 0

   // Server Name
   cTSInfo += "Server Name|"+sy_netname+cNewLine

   // WTSInitialProgram -- not working
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSInitialProgram    , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSInitialProgram := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSInitialProgram, @pBuffer, BytesReturned)
      xWTSInitialProgram := TruncateNull(xWTSInitialProgram)
      cTSInfo += "Initial Program|"+xWTSInitialProgram+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSApplicationName -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSApplicationName   , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSApplicationName := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSApplicationName, pBuffer, BytesReturned)
      xWTSApplicationName := TruncateNull(xWTSApplicationName)
      cTSInfo += "Application Name|"+xWTSApplicationName+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSWorkingDirectory -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSWorkingDirectory  , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSWorkingDirectory := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSWorkingDirectory, pBuffer, BytesReturned)
      xWTSWorkingDirectory := TruncateNull(xWTSWorkingDirectory)
      cTSInfo += "Working Directory|"+xWTSWorkingDirectory+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSSessionId - WORKS
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSSessionId         , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSSessionId, pBuffer, BytesReturned)
      sy_sessionid := xWTSSessionId
      cTSInfo += "Session ID|"+ALLTRIM(TOSTRVAL(xWTSSessionId))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSUserName -- works but returns nil?
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName          , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSUserName, pBuffer, BytesReturned)
      xWTSUserName := TruncateNull(xWTSUserName)
      cTSInfo += "User Name|"+xWTSUserName+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSWinStationName -- not working
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSWinStationName    , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSWinStationName := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSWinStationName, pBuffer, BytesReturned)
      xWTSWinStationName := TruncateNull(xWTSWinStationName)
      cTSInfo += "Win Station Name|"+xWTSWinStationName+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSDomainName -- not working
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSDomainName        , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSDomainName := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSDomainName, pBuffer, BytesReturned)
      xWTSDomainName := TruncateNull(xWTSDomainName)
      cTSInfo += "Domain Name|"+xWTSDomainName+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSConnectState -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSConnectState      , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSConnectState, pBuffer, BytesReturned)
      cTSInfo += "ConnectState|"+ALLTRIM(TOSTRVAL(xWTSConnectState))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientBuildNumber -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientBuildNumber , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientBuildNumber, pBuffer, BytesReturned)
      cTSInfo += "Client BuildNumber|"+ALLTRIM(TOSTRVAL(xWTSClientBuildNumber))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientName -- not working, ugh.
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientName        , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSClientName := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientName, pBuffer, BytesReturned)
      xWTSClientName := TruncateNull(xWTSClientName)
      cTSInfo += "Client Name|"+xWTSClientName+cNewLine
      IF sy_sessionid == 0
         cStationName := sy_netname
      ELSE
         cStationName := xWTSClientName
      ENDIF
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientDirectory -- not working either
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientDirectory   , @pBuffer, @BytesReturned)
   IF Success == 1
      xWTSClientDirectory := SPACE(BytesReturned)+CHR(0)
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientDirectory, pBuffer, BytesReturned)
      xWTSClientDirectory := TruncateNull(xWTSClientDirectory)
      cTSInfo += "Client Directory|"+xWTSClientDirectory+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientProductId -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProductId   , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientProductId, pBuffer, BytesReturned)
      cTSInfo += "Client ProductId|"+ALLTRIM(TOSTRVAL(xWTSClientProductId))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientHardwareId -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientHardwareId  , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientHardwareId, pBuffer, BytesReturned)
      cTSInfo += "Client HardwareId|"+ALLTRIM(TOSTRVAL(xWTSClientHardwareId))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientAddress -- works, need to figure out address format though
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress     , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientAddress, pBuffer, BytesReturned)
      cTSInfo += "AddressFamily|"+ALLTRIM(TOSTRVAL(BaExtract(a_WTS_CLIENT_ADDRESS,xWTSClientAddress)))+cNewLine
  cAddressStr := BaExtract(a_WTS_CLIENT_ADDRESS)
  cTSInfo += "Len|"+FSTR(LEN(cAddressStr))+cNewLine
  oSocket := xbSocket():New(AF_INET, SOCK_STREAM, IPPROTO_IP)
  cTSInfo +="Address|"+oSocket:InetNtoA(Bin2U(RIGHT(cAddressStr,18)))+cNewLine
  oSocket:Destroy()
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientDisplay -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientDisplay     , @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientDisplay, pBuffer, BytesReturned)
      cTSInfo += "HorizontalRes|"+ALLTRIM(TOSTRVAL(BaExtract(a_WTS_CLIENT_DISPLAY,xWTSClientDisplay)))+cNewLine
      cTSInfo += "VerticalRes|"+ALLTRIM(TOSTRVAL(BaExtract(a_WTS_CLIENT_DISPLAY)))+cNewLine
      cTSInfo += "ColorDepth|"+ALLTRIM(TOSTRVAL(BaExtract(a_WTS_CLIENT_DISPLAY)))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF

   // WTSClientProtocolType -- works
   Success := DLLCALL(nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientProtocolType, @pBuffer, @BytesReturned)
   IF Success == 1
      DLLCALL("kernel32.dll", DLL_STDCALL, "RtlMoveMemory", @xWTSClientProtocolType, pBuffer, BytesReturned)
      cTSInfo += "Client ProtocolType|"+ALLTRIM(TOSTRVAL(xWTSClientProtocolType))+cNewLine
      DLLCALL(nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer)
   ENDIF
ELSE
   PopMess("no wtsapi32.dll")
ENDIF
RETURN ({cStationName,cTSInfo})
// GetTSInfo
 


Code: Select all  Expand view
#include "Appevent.ch"
#include "Common.ch"
#include "Xbp.ch"
#include "dll.ch"
#include "wts.ch"
#include "c:\ot4xb\ot4xb.ch"

***************************
CLASS WTSsessionInformation
***************************
PROTECTED:
    VAR _aWTSSessionInformation

EXPORTED
    VAR lLoadClientDirectory
    VAR lLoadWorkingDirectory
    VAR lIsRemoteSession

    INLINE METHOD init
    ******************
       ::lLoadClientDirectory := .F.
       ::lLoadWorkingDirectory:= .F.
    RETURN self

    INLINE METHOD create
    ********************
       ::lIsRemoteSession:= ( GetSystemMetrics( SM_REMOTESESSION ) > 0 )
    RETURN self

    INLINE METHOD destroy
    *********************
       ::_aWTSSessionInformation:= ::lLoadClientDirectory:= ::lLoadWorkingDirectory:= ::lIsRemoteSession:= nil
    RETURN self

    *****************************
    INLINE METHOD loadInformation
    *****************************
       LOCAL aDatos, nDll
       LOCAL pBuffer      := 0
       LOCAL BytesReturned:= 0

       IF ::lIsRemoteSession

          nDll:= DllLoad("WTSAPI32.DLL")

          IF nDll != 0
             ::_aWTSSessionInformation:= Array( WTSClientProtocolType + 1 )

             IF DLLCALL( nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, ;
                         WTS_CURRENT_SESSION, WTSSessionId, @pBuffer, @BytesReturned ) = 1

                IF Valtype( ( aDatos:= PeekByte( pBuffer, 0, BytesReturned ) ) ) = 'A' .AND. Len( aDatos ) > 0
                   ::_aWTSSessionInformation[ WTSSessionId + 1 ]:= Ltrim( Str( aDatos[ 1 ] ) )
                ENDIF
                DLLCALL( nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer )
             ENDIF

             IF DLLCALL( nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, ;
                         WTS_CURRENT_SESSION, WTSClientAddress, @pbuffer, @BytesReturned ) = 1

                IF Valtype( ( aDatos:= PeekByte( pBuffer, 0, BytesReturned ) ) ) = 'A' .AND. Len( aDatos ) > 10
                   // cAddressFamily:= aDatos[  1 ]
                   ::_aWTSSessionInformation[ WTSClientAddress + 1 ]:= Ltrim( Str( aDatos[  7 ] ) ) + '.' + Ltrim( Str( aDatos[  8 ] ) )+ '.' + ;
                                                                       Ltrim( Str( aDatos[  9 ] ) ) + '.' + Ltrim( Str( aDatos[ 10 ] ) )
                ENDIF
                DLLCALL( nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer )
             ENDIF

             IF ::lLoadClientDirectory .AND. DLLCALL( nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, ;
                                                      WTS_CURRENT_SESSION, WTSClientDirectory, @pBuffer, @BytesReturned ) = 1

                ::_aWTSSessionInformation[ WTSClientDirectory + 1 ]:= Space( BytesReturned )
                PeekStr( pBuffer, 0, @::_aWTSSessionInformation[ WTSClientDirectory + 1 ] )
                DLLCALL( nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer )
             ENDIF

             IF ::lLoadWorkingDirectory .AND. DLLCALL( nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, ;
                                                       WTS_CURRENT_SESSION, WTSWorkingDirectory, @pBuffer, @BytesReturned ) = 1

                ::_aWTSSessionInformation[ WTSWorkingDirectory + 1 ]:= Space( BytesReturned )
                PeekStr( pBuffer, 0, @::_aWTSSessionInformation[ WTSWorkingDirectory + 1 ] )
                DLLCALL( nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer )
             ENDIF

             DllUnLoad( nDll )
          ENDIF
       ENDIF
    RETURN self

    INLINE METHOD clientAddress
    ***************************
       IF ! ::lIsRemoteSession
          RETURN ''
       ELSEIF ::_aWTSSessionInformation = nil
          ::loadInformation()
       ENDIF
    RETURN ::_aWTSSessionInformation[ WTSClientAddress + 1 ]

    INLINE METHOD sessionId
    ***********************
       IF ! ::lIsRemoteSession
          RETURN 0
       ELSEIF ::_aWTSSessionInformation = nil
          ::loadInformation()
       ENDIF
    RETURN ::_aWTSSessionInformation[ WTSSessionId + 1 ]

    INLINE METHOD clientDirectory
    *****************************
       IF ! ::lIsRemoteSession
          RETURN ''
       ELSEIF ::_aWTSSessionInformation = nil .OR. ! ::lLoadClientDirectory
          ::lLoadClientDirectory:= .T.
          ::loadInformation()
       ENDIF
    RETURN ::_aWTSSessionInformation[ WTSClientDirectory + 1 ]

    INLINE METHOD currentSize
    *************************
       LOCAL aSize, nDll, aDatos
       LOCAL pBuffer      := 0
       LOCAL BytesReturned:= 0

       IF ::lIsRemoteSession .AND. ( nDll:= DllLoad("WTSAPI32.DLL") ) != 0
          IF DLLCALL( nDll, DLL_STDCALL, "WTSQuerySessionInformationA", WTS_CURRENT_SERVER_HANDLE, ;
                                          WTS_CURRENT_SESSION, WTSClientDisplay, @pBuffer, @BytesReturned ) = 1

             IF Valtype( ( aDatos:= PeekByte( pBuffer, 0, BytesReturned ) ) ) = 'A' .AND. Len( aDatos ) > 0
                aSize:= { Bin2u( Chr( aDatos[ 1 ] ) + Chr( aDatos[ 2 ] ) + Chr( aDatos[ 3 ] ) + Chr( aDatos[ 4 ] ) ), ;
                          Bin2u( Chr( aDatos[ 5 ] ) + Chr( aDatos[ 6 ] ) + Chr( aDatos[ 7 ] ) + Chr( aDatos[ 8 ] ) ) }
             ENDIF
             DLLCALL( nDll, DLL_STDCALL, "WTSFreeMemory", pBuffer )
          ENDIF

          DllUnLoad( nDll )
       ENDIF
    RETURN IIF( aSize = nil, AppDesktop():currentSize(), aSize )
ENDCLASS

STATIC DLLFUNCTION GetSystemMetrics( nIndex ) USING STDCALL FROM USER32.DLL


Code: Select all  Expand view
#define WTS_CURRENT_SERVER_HANDLE          0
#define WTS_CURRENT_SESSION               -1

#define WTSInitialProgram                  0
#define WTSApplicationName                 1
#define WTSWorkingDirectory                2
#define WTSOEMId                           3
#define WTSSessionId                       4
#define WTSUserName                        5
#define WTSWinStationName                  6
#define WTSDomainName                      7
#define WTSConnectState                    8
#define WTSClientBuildNumber               9
#define WTSClientName                     10
#define WTSClientDirectory                11
#define WTSClientProductId                12
#define WTSClientHardwareId               13
#define WTSClientAddress                  14
#define WTSClientDisplay                  15
#define WTSClientProtocolType             16

#define WTSSBX_ADDRESS_FAMILY_AF_UNSPEC    0
#define WTSSBX_ADDRESS_FAMILY_AF_INET      1
#define WTSSBX_ADDRESS_FAMILY_AF_INET6     2
#define WTSSBX_ADDRESS_FAMILY_AF_IPX       3
#define WTSSBX_ADDRESS_FAMILY_AF_NETBIOS   4

#define SM_REMOTESESSION                   0x1000
#define WM_DESTROY                         0x0002
#define WM_WTSSESSION_CHANGE               689
#define NOTIFY_FOR_THIS_SESSION            0
#define WTS_REMOTE_CONNECT                 3
#define WTS_REMOTE_DISCONNECT              4

/*
WTS_CONSOLE_CONNECT = 1;
  WTS_CONSOLE_DISCONNECT = 2;
  WTS_REMOTE_CONNECT = 3;
  WTS_REMOTE_DISCONNECT = 4;
  WTS_SESSION_LOGON = 5;
  WTS_SESSION_LOGOFF = 6;
  WTS_SESSION_LOCK = 7;
  WTS_SESSION_UNLOCK = 8;
  WTS_SESSION_REMOTE_CONTROL = 9;

  NOTIFY_FOR_THIS_SESSION = 0;
  NOTIFY_FOR_ALL_SESSIONS = 1;
*/

 
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1725
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: ID of the current process

Postby Antonio Linares » Thu Dec 23, 2021 4:42 pm

Dear Jimmy,

You xBase++ and Harbour experience is great to make Harbour more and more xBase++ compatible

Thank you so much for the great work that you are doing understanding and using both of them :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42059
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 43 guests