如题,这样做是不行的:        CString str;
char strN[400]; HANDLE hP=OpenProcess(PROCESS_ALL_ACCESS,FALSE,PID);
GetModuleFileName(hp,strN,400);
已知PID ,怎么得到进程对应的程序名称?

解决方案 »

  1.   

    微软遍历进程的例子:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <psapi.h>void PrintProcessNameAndID( DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");    // Get a handle to the process.    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );    // Get the process name.    if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }    // Print the process name and identifier.    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );    CloseHandle( hProcess );
    }void main( )
    {
        // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name and process identifier for each process.    for ( i = 0; i < cProcesses; i++ )
            if( aProcesses[i] != 0 )
                PrintProcessNameAndID( aProcesses[i] );
    }
      

  2.   

    CreateToolhelp32Snapshot()
    Process32First()
    Process32Next()
    比较pid与找到的, 就可以得到了
      

  3.   

    补充:(看看msdn的说明吧)
    GetModuleFileName FunctionRetrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.
      

  4.   


    #include <Windows.h>
    #include <Psapi.h>
    #include <stdio.h>#pragma comment(lib, "psapi.lib")
    int main(void)
    {
        TCHAR      szFilePath[MAX_PATH];
        DWORD    dwProcessId = 2624;
        HANDLE    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                                              FALSE,
                                                              dwProcessId) ;    if (hProcess != NULL)
        {
            GetModuleFileNameEx(hProcess, NULL, szFilePath, MAX_PATH);
            printf("%S", szFilePath);
            CloseHandle(hProcess);
        }    return 0;
    }
    用GetModuleFileNameEx才可以
      

  5.   

    我这个例子是找到ieuser.exe进程关闭它,没有问题:),good luck!HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
    PROCESS_VM_READ | PROCESS_TERMINATE ,
    FALSE, processID ); // Get the process name. if (NULL != hProcess )
    {
    HMODULE hMod;
    DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
    &cbNeeded) )
    {
    GetModuleBaseName( hProcess, hMod, szProcessName, 
    sizeof(szProcessName)/sizeof(TCHAR) );
    CString strProcessName = szProcessName;
    if (strProcessName.CompareNoCase("IEUser.exe") == 0)
    {
    if(TerminateProcess(hProcess,0) != 0 )
    {
    printf("Kill IEUser.exe process! \n");
    }
    else
    {
    printf("Kill IEUser.exe failed! error is %d \n", GetLastError());
    }
    }
    }
    } // Print the process name and identifier. _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID ); CloseHandle( hProcess );