2010-12-31 39 views

Trả lời

2

Nếu bạn đang cố gắng lấy tên hình ảnh thực thi của một quy trình cụ thể, hãy xem GetModuleFileName.

+0

GetModuleFileName không mất PID trong việc xem xét . Nó chỉ cung cấp đường dẫn đầy đủ cho tệp chứa mô-đun được chỉ định. –

23

Tôi đoán chức năng OpenProcess sẽ hữu ích, với điều kiện quy trình của bạn có các quyền cần thiết. Một khi bạn có được một xử lý cho quá trình này, bạn có thể sử dụng chức năng GetModuleFileNameEx để có được đường dẫn đầy đủ (đường dẫn đến tệp .exe) của quá trình.

#include "stdafx.h" 
#include "windows.h" 
#include "tchar.h" 
#include "stdio.h" 
#include "psapi.h" 
// Important: Must include psapi.lib in additional dependencies section 
// In VS2005... Project > Project Properties > Configuration Properties > Linker > Input > Additional Dependencies 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    HANDLE Handle = OpenProcess(
     PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 
     FALSE, 
     8036 /* This is the PID, you can find one from windows task manager */ 
    ); 
    if (Handle) 
    { 
     TCHAR Buffer[MAX_PATH]; 
     if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH)) 
     { 
      // At this point, buffer contains the full path to the executable 
     } 
     else 
     { 
      // You better call GetLastError() here 
     } 
     CloseHandle(Handle); 
    } 
    return 0; 
} 
+0

Thật thú vị khi biết làm thế nào để có được một PID ở nơi đầu tiên, mặc dù điều này không được hỏi bởi OP. – Jaywalker

+0

@Jaywalker Cuộc gọi wmi và lớp win32_process sẽ thực hiện thủ thuật – ROAR

+0

@Jaywalker: Bạn có thể sử dụng EnumProcesses (http://msdn.microsoft.com/en-us/library/ms682629.aspx) –

12

Bạn có thể lấy tên quy trình bằng cách sử dụng WIN32 API GetModuleBaseName sau khi xử lý quy trình. Bạn có thể xử lý quy trình bằng cách sử dụng OpenProcess.

Để có được tên thực thi, bạn cũng có thể sử dụng GetProcessImageFileName.

1

Hãy thử chức năng này:

std::wstring GetProcName(DWORD aPid) 
{ 
PROCESSENTRY32 processInfo; 
    processInfo.dwSize = sizeof(processInfo); 
    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); 
    if (processesSnapshot == INVALID_HANDLE_VALUE) 
    { 
     std::wcout << "can't get a process snapshot "; 
     return 0; 
    } 

    for(BOOL bok =Process32First(processesSnapshot, &processInfo);bok; bok = Process32Next(processesSnapshot, &processInfo)) 
    { 
     if(aPid == processInfo.th32ProcessID) 
     { 
      std::wcout << "found running process: " << processInfo.szExeFile; 
      CloseHandle(processesSnapshot); 
      return processInfo.szExeFile; 
     } 

    } 
    std::wcout << "no process with given pid" << aPid; 
    CloseHandle(processesSnapshot); 
    return std::wstring(); 
} 
Các vấn đề liên quan