bool IsAnyRDPSessionActive() { WTS_SESSION_INFO *pSessions = 0; DWORD nSessions(0); DWORD rdpSessionExists = false; typedef BOOL(WINAPI *pfnWTSEnumerateSessions)(HANDLE, DWORD, DWORD, PWTS_SESSION_INFO*, DWORD*); typedef VOID(WINAPI *pfnWTSFreeMemory)(PVOID); helper::DynamicFn<pfnWTSEnumerateSessions> pWTSEnumerateSessions("wtsapi32", "WTSEnumerateSessionsA"); helper::DynamicFn<pfnWTSFreeMemory> pWTSFreeMemory("wtsapi32", "WTSFreeMemory"); if (pWTSEnumerateSessions.isValid() && pWTSFreeMemory.isValid()) if ((*pWTSEnumerateSessions)(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessions, &nSessions)) { for (DWORD i(0); i < nSessions && !rdpSessionExists; ++i) { if ((_stricmp(pSessions[i].pWinStationName, "Console") != 0) && (pSessions[i].State == WTSActive || pSessions[i].State == WTSShadow || pSessions[i].State == WTSConnectQuery )) { rdpSessionExists = true; } } (*pWTSFreeMemory)(pSessions); } return rdpSessionExists ? true : false; }
char *GetLogUserXP() { typedef BOOL (WINAPI *WTSQuerySessionInformationAT)( IN HANDLE hServer, IN DWORD SessionId, IN WTS_INFO_CLASS WTSInfoClass, OUT LPSTR * ppBuffer, OUT DWORD * pBytesReturned ); WTSQuerySessionInformationAT pWTSQuerySessionInformationA=(WTSQuerySessionInformationAT)GetProcAddress(LoadLibrary("WTSAPI32.dll"),"WTSQuerySessionInformationA"); typedef VOID (WINAPI *WTSFreeMemoryT)( IN PVOID pMemory ); WTSFreeMemoryT pWTSFreeMemory=(WTSFreeMemoryT)GetProcAddress(LoadLibrary("WTSAPI32.dll"),"WTSFreeMemory"); typedef LPSTR (WINAPI *lstrcpyAT)( __out LPSTR lpString1, __in LPCSTR lpString2 ); lstrcpyAT plstrcpyA=(lstrcpyAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"lstrcpyA"); TCHAR *szLogName = NULL; DWORD dwSize = 0; if (pWTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, &szLogName, &dwSize)) { char *lpUser = new char[256]; plstrcpyA(lpUser, szLogName); pWTSFreeMemory(szLogName); return lpUser; } else return NULL; }
DWORD Find_winlogon(DWORD SessionId) { PWTS_PROCESS_INFOA pProcessInfo = NULL; DWORD ProcessCount = 0; // char szUserName[255]; DWORD Id = -1; typedef BOOL(WINAPI *pfnWTSEnumerateProcesses)(HANDLE, DWORD, DWORD, PWTS_PROCESS_INFOA*, DWORD*); typedef VOID(WINAPI *pfnWTSFreeMemory)(PVOID); DynamicFn<pfnWTSEnumerateProcesses> pWTSEnumerateProcesses("wtsapi32", "WTSEnumerateProcessesA"); DynamicFn<pfnWTSFreeMemory> pWTSFreeMemory("wtsapi32", "WTSFreeMemory"); if (pWTSEnumerateProcesses.isValid() && pWTSFreeMemory.isValid()) { if ((*pWTSEnumerateProcesses)(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &ProcessCount)) { // dump each process description for (DWORD CurrentProcess = 0; CurrentProcess < ProcessCount; CurrentProcess++) { if (_stricmp(pProcessInfo[CurrentProcess].pProcessName, "winlogon.exe") == 0) { if (SessionId == pProcessInfo[CurrentProcess].SessionId) { Id = pProcessInfo[CurrentProcess].ProcessId; break; } } } (*pWTSFreeMemory)(pProcessInfo); } } return Id; }