gpt4 book ai didi

c++ - 获取进程用户名c++

转载 作者:行者123 更新时间:2023-11-30 04:36:03 37 4
gpt4 key购买 nike

我正在制作一个类似应用程序的任务管理器。对于 Windows,我可以获取所有系统进程,现在我想获取进程的用户名。我从网上获得了代码。

void enableDebugPrivileges()
{
HANDLE hcurrent=GetCurrentProcess();
HANDLE hToken;
BOOL bret=OpenProcessToken(hcurrent,40,&hToken);
LUID luid;
bret=LookupPrivilegeValue(NULL,SE_LOAD_DRIVER_NAME, &luid);
TOKEN_PRIVILEGES NewState,PreviousState;
DWORD ReturnLength;
NewState.PrivilegeCount =1;
NewState.Privileges[0].Luid =luid;
NewState.Privileges[0].Attributes=2;
AdjustTokenPrivileges(hToken,FALSE,&NewState,28,&PreviousState,&ReturnLength);
}

char *GetProcessUsername(HANDLE *phProcess, BOOL bIncDomain)
{
static char sname[300];
HANDLE tok = 0;
HANDLE hProcess;
TOKEN_USER *ptu;
DWORD nlen, dlen;
char name[300], dom[300], tubuf[300], *pret = 0;
int iUse;

//if phProcess is NULL we get process handle of this
//process.
hProcess = phProcess?*phProcess:GetCurrentProcess();

//open the processes token
if (!OpenProcessToken(hProcess,TOKEN_QUERY,&tok)) goto ert;

//get the SID of the token
ptu = (TOKEN_USER*)tubuf;
if (!GetTokenInformation(tok,(TOKEN_INFORMATION_CLASS)1,ptu,300,&nlen)) goto ert;

//get the account/domain name of the SID
dlen = 300;
nlen = 300;
if (!LookupAccountSidA(0, ptu->User.Sid, name, &nlen, dom, &dlen, (PSID_NAME_USE)&iUse)) goto ert;


//copy info to our static buffer
if (dlen && bIncDomain) {
strcpy(sname,dom);
strcat(sname,"");
strcat(sname,name);
} else {
strcpy(sname,name);
}
//set our return variable
pret = sname;

ert:
if (tok) CloseHandle(tok);
return pret;
}
int main(){
enableDebugPrivileges();
DWORD dwPID=3436;
HANDLE hProcess_i = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPID);
printf("%s",GetProcessUsername(&hProcess_i,0));
}

它在 system 和 curr 上运行良好。用户进程,但不是网络服务和本地服务,我得到了空字符串。请告诉我如何获得这些进程的用户名。谢谢。

最佳答案

IIRC 有一个名为 LocalService 的单独伪帐户,但它不在正常的安全系统中(因此你得到一个空字符串)。还有一个 NetworkService 帐户。

关于c++ - 获取进程用户名c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4800924/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com