如何知道启动应用程序的可执行名称

How to know the executable name that launches an application?

本文关键字:可执行 应用程序 何知道 启动      更新时间:2023-10-16

如何确定使用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;
}