怎么用API函数获取已经文件名的文件的路径呢?比如说已知qq.exe,怎么获取QQ可执行文件的路径

解决方案 »

  1.   

    #include "stdafx.h"#include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <psapi.h>
    #pragma comment(lib, "PSAPI.lib")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) );
    GetModuleFileNameEx(hProcess, hMod, szProcessName, MAX_PATH);
    }
    } // 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] );
    }