Windows Terminal Server

Moderator: Enrico Maria Giordano

Windows Terminal Server

Postby Ugo » Thu Dec 29, 2011 6:34 pm

Ciao a tutti,

per prima cosa, visto il periodo, faccio i miei auguri di buone feste e per un migliore 2012.

Vengo alla domanda:
ho un applicativo che gira su server windows con terminal server utilizzato tramite desktop remoto.

Nell'applicativo, per alcune funzioni c'è bisogno di sapere da dove l'utente si è collegato, al momento si sfrutta una informazione già memorizzata sul server: "il nome del pc" che però non è una garanzia di univocità assoluta. L'idea è quella di poter accedere a qualche informazione più "Univoca" della macchina client anche in associazione al nome del pc come ad esempio l'indirizzo IP.

Ho trovato il seguente codice in giro per la rete ma, data la mia ignoranza con il C, non riesco a far funzionare.

Sono graditi suggerimenti e aiuti a riguardo, grazie.
Code: Select all  Expand view
FUNCTION Test()

   LOCAL cClntAddr

   cClntAddr := GetTSClientAddress()

   MsgInfo( cClntAddr )

   RETURN Nil

//------------------------------------------------------------------------------


#pragma BEGINDUMP
#define HB_OS_WIN_32_USED
#define _WIN32_WINNT   0x0400

#include <hbapi.h>
#include <windows.h>
#include <wtsapi32.h>

typedef BOOL (WINAPI * _WTSQUERYSESSIONINFORMATION)( HWND hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPTSTR *ppBuffer, DWORD *pBytesReturned);
typedef VOID (WINAPI * _WTSFREEMEMORY)( LPTSTR ppBuffer );
char * GetTSClientAddress()
{
   LPTSTR              ppBuffer       = NULL;
   PWTS_CLIENT_ADDRESS pReturn        = NULL;
   DWORD               pBytesReturned = 0;
   char *              ClientIP ;
   HINSTANCE handle = LoadLibrary("WTSAPI32.DLL");
   if (handle)
   {
       _WTSQUERYSESSIONINFORMATION pFunc;
       _WTSFREEMEMORY pFun2;
       pFunc = (_WTSQUERYSESSIONINFORMATION) GetProcAddress(handle, "WTSQuerySessionInformation");
       if ( pFunc( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, &ppBuffer, &pBytesReturned ) )
       {
           pReturn    = (PWTS_CLIENT_ADDRESS) ppBuffer;
           ClientIP = (char *) hb_xgrab( 20 );
           memset( ClientIP, 0, 20 );
           sprintf( ClientIP, "%u.%u.%u.%u", pReturn->Address[2],                      pReturn->Address[3], pReturn->Address[4], pReturn->Address[5] );
       }
       pFun2 = (_WTSFREEMEMORY) GetProcAddress(handle, "WTSFreeMemory");
       pFun2( ppBuffer );
       FreeLibrary( handle );
   }
   return ClientIP;
}

//----------------------------------------------------------------------------//

HB_FUNC( GETTSCLIENTADDRESS )
{
   char *ClientIP = GetTSClientAddress( (DWORD) hb_parnl(1) );
   hb_retc( ClientIP );
   if ( ClientIP )
      hb_xfree( ClientIP );
}

//----------------------------------------------------------------------------//

char * GetTSDomainName()
{
    LPTSTR  ppBuffer  = NULL;
    DWORD   pBytesReturned = 0;
    char *  currentDomainName = NULL;
    HINSTANCE handle = LoadLibrary("WTSAPI32.DLL");
    if (handle)
    {
            _WTSQUERYSESSIONINFORMATION pFunc;
            _WTSFREEMEMORY pFun2;
            pFunc = (_WTSQUERYSESSIONINFORMATION) GetProcAddress(handle, "WTSQuerySessionInformation");
            if ( pFunc( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION , WTSDomainName, &ppBuffer, &pBytesReturned ) )
            {
                 currentDomainName = (char*) hb_xgrab( strlen( ppBuffer ) + 1 );
                 memset( currentDomainName, 0, strlen( ppBuffer ) + 1 );
                 strcpy( currentDomainName, ppBuffer );
            }
            pFun2 = (_WTSFREEMEMORY) GetProcAddress(handle, "WTSFreeMemory");
            pFun2( ppBuffer );
            FreeLibrary( handle );
     }
     return currentDomainName;
}

//----------------------------------------------------------------------------//

HB_FUNC( GETTSDOMAINNAME )

{
   char *zDomainName = GetTSDomainName( (DWORD) hb_parnl(1) );
   hb_retc( zDomainName );
   if ( zDomainName )
       hb_xfree( zDomainName );
}

//----------------------------------------------------------------------------//

char * GetTSClientName()
{
    LPTSTR  ppBuffer  = NULL;
    DWORD   pBytesReturned = 0;
    char *  currentClientName = NULL;
    HINSTANCE handle = LoadLibrary("WTSAPI32.DLL");
    if (handle)
    {
         _WTSQUERYSESSIONINFORMATION pFunc;
         _WTSFREEMEMORY pFun2;
         pFunc = (_WTSQUERYSESSIONINFORMATION) GetProcAddress(handle, "WTSQuerySessionInformation");
         if ( pFunc( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION , WTSClientName, &ppBuffer, &pBytesReturned ) )
         {
               currentClientName = (char*) hb_xgrab( strlen( ppBuffer ) + 1 );
               memset( currentClientName, 0, strlen( ppBuffer ) + 1 );
               strcpy( currentClientName, ppBuffer );
         }
         pFun2 = (_WTSFREEMEMORY) GetProcAddress(handle, "WTSFreeMemory");
         pFun2( ppBuffer );
         FreeLibrary( handle );
     }
     return currentClientName;
}

//----------------------------------------------------------------------------//

HB_FUNC( GETTSCLIENTNAME )
{
   char *zClientName = GetTSClientName( (DWORD) hb_parnl(1) );
   hb_retc( zClientName );
   if ( zClientName )
       hb_xfree( zClientName );
}

//----------------------------------------------------------------------------//

char * GetTSUserName()
{
    LPTSTR  ppBuffer  = NULL;
    DWORD   pBytesReturned = 0;
    char *  currentUserName = NULL;
    HINSTANCE handle = LoadLibrary("WTSAPI32.DLL");
    if (handle)
    {
        _WTSQUERYSESSIONINFORMATION pFunc;
        _WTSFREEMEMORY pFun2;
        pFunc = (_WTSQUERYSESSIONINFORMATION) GetProcAddress(handle, "WTSQuerySessionInformation");
        if ( pFunc( WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION ,WTSUserName,&ppBuffer,&pBytesReturned ) )
        {
             currentUserName = (char*) hb_xgrab( strlen( ppBuffer ) + 1 );
             memset( currentUserName, 0, strlen( ppBuffer ) + 1 );
             strcpy( currentUserName, ppBuffer );
        }
        pFun2 = (_WTSFREEMEMORY) GetProcAddress(handle, "WTSFreeMemory");
        pFun2( ppBuffer );
        FreeLibrary( handle );
    }
    return currentUserName;
}

//----------------------------------------------------------------------------//

HB_FUNC( GETTSUSERNAME )
{
   char *zUserName = GetTSUserName( (DWORD) hb_parnl(1) );
   hb_retc( zUserName );
   if ( zUserName )
       hb_xfree( zUserName );
}

//----------------------------------------------------------------------------//

char * GetTSWinStationName()
{
    LPTSTR  ppBuffer  = NULL;
    DWORD   pBytesReturned = 0;
    char *  currentUserName = NULL;
    HINSTANCE handle = LoadLibrary("WTSAPI32.DLL");
    if (handle)
    {
       _WTSQUERYSESSIONINFORMATION pFunc;
       _WTSFREEMEMORY pFun2;
       pFunc = (_WTSQUERYSESSIONINFORMATION) GetProcAddress(handle, "WTSQuerySessionInformation");
       if ( pFunc( WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION ,WTSWinStationName,&ppBuffer,&pBytesReturned ) )
       {
            currentUserName = (char*) hb_xgrab( strlen( ppBuffer ) + 1 );
            memset( currentUserName, 0, strlen( ppBuffer ) + 1 );
            strcpy( currentUserName, ppBuffer );
        }
        pFun2 = (_WTSFREEMEMORY) GetProcAddress(handle, "WTSFreeMemory");
        pFun2( ppBuffer );
        FreeLibrary( handle );
    }
    return currentUserName;
}

//----------------------------------------------------------------------------//

HB_FUNC( GETTSWINSTATIONNAME )
{
   char *zUserName = GetTSWinStationName( (DWORD) hb_parnl(1) );
   hb_retc( zUserName );
   if ( zUserName )
       hb_xfree( zUserName );
}

//----------------------------------------------------------------------------//
#pragma ENDDUMP
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Return to All products support

Who is online

Users browsing this forum: No registered users and 8 guests