Pfad eines bestimmten laufenden Prozesses auslesen



  • Hallo.
    Wie der Threadtitel schon sagt möchte ich den Pfad eines Prozesses erhalten.
    Jedoch bekomme ich zum schluss segmentation fault bei GetModuleFileNameEx -.-

    Googlen --> funktioniert nicht wegn 64bit
    Benutze win7.

    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <psapi.h>
    
    using namespace std;
    
    int main( )
    {
        TCHAR szModName[MAX_PATH];
        std::string process_name;
        int pid = 0;
    
        HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        PROCESSENTRY32* processInfo=new PROCESSENTRY32;
        processInfo->dwSize=sizeof(PROCESSENTRY32);
    
        while(Process32Next(hSnapShot,processInfo)!=FALSE)
        {
          process_name = processInfo->szExeFile;
          if(process_name.compare("firefox.exe") == 0)
          {
            pid = processInfo->th32ProcessID;
            cout<<"Name: "<<process_name << endl;
          }
        }
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                PROCESS_VM_READ,
                                FALSE, pid );
    
        if(hProcess==NULL)
        {
          cout<<"Unable to get handle of process: "<<pid;
          cout<<"Error is: "<<GetLastError();
          return 1;
        }
        else
          cout<<endl<< "Success" << endl;
    
        GetModuleFileNameEx(hProcess, NULL, szModName, sizeof(szModName));
        cout << "Path: " << szModName << endl;
        CloseHandle( hProcess );
    
        delete processInfo;
        return 0;
    }
    

    Bitte um hilfe!
    lg



  • Hi

    So wie es aussieht ist das Programm Ascii, und nicht Unicode !

    Das Problem sehe ich in Zeile 44. Und zwar verwendest du bei

    TCHAR szModName[MAX_PATH];
    GetModuleFileNameEx(hProcess, NULL, szModName, sizeof(szModName));
    

    beim 3 Parameter ein TCHAR, doch es sollte ein LPSTR sein !

    und zwar so.

    LPSTR szModName[MAX_PATH];
    GetModuleFileNameEx(hProcess, NULL, szModName, sizeof(szModName));
    

    Dies erklärt dein segmentation fault.

    Noch etwas anderes ist mir aufgefallen ....

    #include <string>
    

    sollte so aussehen ...

    #include <string.h>
    

    lowbyte


  • Mod

    lowbyte_ schrieb:

    #include <string>
    

    sollte so aussehen ...

    #include <string.h>
    

    Ganz gewiss soll das nicht so aussehen. Die Standard Header haben keine Erweiterung .h!



  • Danke erstmal für die Antwort!

    Wenn ich

    LPSTR szModName[MAX_PATH];
    GetModuleFileNameEx(hProcess, NULL, szModName, sizeof(szModName));
    

    mache erhalte ich folgenden error:

    cannot convert 'CHAR**' to 'CHAR*' for argument '3' to 'DWORD GetModuleFileNameExA(void*, HINSTANCE__*, CHAR*, DWORD)'|
    

    nur

    LPSTR szModName;
    

    verändert auch nichts, weiterhin segfault.

    Vllt noch andere Ideen?
    Lg



  • Benjo schrieb:

    Googlen --> funktioniert nicht wegn 64bit

    Und wie hast du denn deine Beiträge erstellt?



  • Beim Community-Content hier schreibt jemand:

    For best results use the following table to convert paths.

    Windows 2000 = GetModuleFileName()
    Windows XP x32 = GetProcessImageFileName()
    Windows XP x64 = GetProcessImageFileName()
    Windows Vista = QueryFullProcessImageName()
    Windows 7 = QueryFullProcessImageName()

    Note: If you are not aware the API GetProcessImageFileName() returns a kernel DOS device path. You can use the following API to map the device paths to a Win32 format.
    GetLogicalDriveStrings
    QueryDosDevice



  • hi

    char var(20);

    so !
    sorry fûr die klammern, schreibe von meinem cell phone.

    @martin richter

    dies trifft vieleich bei c++ zu ja.
    aber nicht bei c89.

    lowbyte



  • lowbyte- schrieb:

    @martin richter

    dies trifft vieleich bei c++ zu ja.
    aber nicht bei c89.

    lowbyte

    Und std::string ist von der C++ Standard Lib.
    Muss also ohne Endung sein.



  • hi

    das ost schon klar.

    lowbyte


Anmelden zum Antworten