gpt4 book ai didi

c++ - 如何知道启动应用程序的可执行文件名称?

转载 作者:行者123 更新时间:2023-11-28 06:16:45 25 4
gpt4 key购买 nike

如何确定启动 C++ 应用程序的可执行文件?

例如:我的应用程序名为(a.exe),还有一个应用程序名为(b.exe)。我如何知道 a.exe 何时与 b.exe 一起启动?

最佳答案

我找到了一种方法,感谢 Wimmel

要获取进程 ID,您可以使用 GetParentProcessId()。你将需要这个功能:

ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
ULONG_PTR pbi[6];
ULONG ulSize = 0;
LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);
*(FARPROC *)&NtQueryInformationProcess =
GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
if(NtQueryInformationProcess){
if(NtQueryInformationProcess(GetCurrentProcess(), 0, &pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
return pbi[5];
}
return (ULONG_PTR)-1;
}

从进程 ID ProcessName(GetParentProcessId()) 获取进程名称。

然后你将需要这个函数:

char* ProcessName(int ProcessId){
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapshot) {
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hSnapshot,&pe32)) {
do {
int th32ProcessID = pe32.th32ProcessID;
if (th32ProcessID == ProcessId)
return pe32.szExeFile;
} while(Process32Next(hSnapshot,&pe32));
}
CloseHandle(hSnapshot);
}
return 0;
}

关于c++ - 如何知道启动应用程序的可执行文件名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131731/

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