下面來(lái)看一個(gè)VC++根據(jù)explorer.exe進(jìn)程檢測(cè)用戶是否已登錄,希望例子能幫助到各位。
代碼如下
void CALLBACK timeCall(UINT timeid, UINT umsg, DWORD_PTR dwUser, DWORD dw1, DWORD_PTR dw2)
{
DWORD dwSessionId = WTSGetActiveConsoleSessionId(); //獲取控制臺(tái)的session
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //為創(chuàng)建環(huán)境快照
if (hSnap == INVALID_HANDLE_VALUE)
return;
//遍歷快照,通過(guò)快照的 procEntry.szExeFile 獲取應(yīng)用程序名字,從而取得相應(yīng)的PID
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &procEntry);
DWORD dwExplorerPid = -1;
do
{
if (strcmp(procEntry.szExeFile, "explorer.exe") == 0)
{
DWORD dwExplorerSessId = 0;
if (ProcessIdToSessionId(procEntry.th32ProcessID, &dwExplorerSessId) && dwExplorerSessId == dwSessionId)
{
dwExplorerPid = procEntry.th32ProcessID;
break;
}
}
} while (Process32Next(hSnap, &procEntry));
LOG_INFO("檢測(cè)用戶是否已經(jīng)登錄 : %s", (dwExplorerPid == -1) ? "未登錄" : "已經(jīng)登錄");
if (dwExplorerPid != -1)
{
//已登錄
DoSomething();
}
}
更多信息請(qǐng)查看IT技術(shù)專欄