3

我有一个运行以下代码的服务。每次点击 CreateProcessAsUser 时,内存都会增加 4K(我可以在任务管理器中看到它)。当我在 CreateProcess 中使用时没有发生这种情况。

为什么会发生这种情况以及如何释放这段记忆?

PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
int dwSessionId = 0;

WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwCount);
for(DWORD i = 0; i < dwCount; ++i)
{
    WTS_SESSION_INFO si = pSessionInfo[i];
    if (WTSActive == si.State)
    {
        dwSessionId = si.SessionId;
        break;
    }
}
WTSFreeMemory(pSessionInfo);

HANDLE currentToken;
BOOL bRet = WTSQueryUserToken(dwSessionId, &currentToken);
if (bRet == false)
{
    return 0;
}

PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
if (CreateProcessAsUser(currentToken, "MY_EXE.exe", "MY_ARGS_STR", NULL, NULL, false, 0, NULL, NULL, &si, &pi))
{
    if (pi.hProcess != NULL)
    {
        CloseHandle(pi.hProcess);
    }
    if (pi.hThread != NULL)
    {
        CloseHandle(pi.hThread);
    }
}
CloseHandle(currentToken);

return 0;
4

0 回答 0