/*此代码用于获取通过终端登录的当前用户winlogon进程pid
在开了终端服务的机器上运行没问题,没有开终端服务的得不到pid,自己修改吧
Author:009
Email:baicker.009(a)gmail.com
Web:http://www.blogjava.net/baicker
Date:20070720
*/
#include <stdio.h>#include <windows.h>#include <wtsapi32.h>#pragma comment(lib, "wtsapi32.lib") void main()
{
DWORD ProcessId, SessionId;
BOOL
Res=0; DWORD winlogonPid = 0;
PWTS_PROCESS_INFO pProcessInfo=NULL; DWORD ProcessCount = 0; DWORD CurrentProcess = 0; ProcessId = GetCurrentProcessId();
Res = ProcessIdToSessionId( ProcessId, &SessionId );
//printf("Now SessionID = %d\n", SessionId );
if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &ProcessCount))
{
// dump each process description for (CurrentProcess = 0; CurrentProcess < ProcessCount; CurrentProcess++)
{
if( strcmp(pProcessInfo[CurrentProcess].pProcessName, "winlogon.exe") == 0 && pProcessInfo[CurrentProcess].SessionId == SessionId )
{
winlogonPid = pProcessInfo[CurrentProcess].ProcessId; break;
}
}
WTSFreeMemory(pProcessInfo);
}
else{ printf("WTSEnumerateProcesses() failed\n"); //没有终端服务就到这里了
} printf("Current winlogon's pid is %d\n",winlogonPid);
}