解决方案 »

  1.   

    这个函数我一般在获取本应用程序路径,我现在想枚举所有进程的信息,就像金山进程管理器那样,我查了百科,如果用GetModuleFileName来取路径,那么他的第一个参数HMOUDLE,模块句柄又要如何去取?
      

  2.   

    提升权限LookupPrivilegeValue/AdjustTokenPrivileges + OpenProcess() + GetModuleFileNameEx()
      

  3.   

    MODULEENTRY32
    Describes an entry from a list that enumerates the modules used by a specified process.typedef struct tagMODULEENTRY32 { 
        DWORD   dwSize; 
        DWORD   th32ModuleID; 
        DWORD   th32ProcessID; 
        DWORD   GlblcntUsage; 
        DWORD   ProccntUsage; 
        BYTE  * modBaseAddr; 
        DWORD   modBaseSize; 
        HMODULE hModule; 
        char    szModule[MAX_MODULE_NAME32 + 1]; 
        char    szExePath[MAX_PATH]; 
    } MODULEENTRY32; 
    typedef MODULEENTRY32 *  PMODULEENTRY32; 
    typedef MODULEENTRY32 *  LPMODULEENTRY32; 
     
    Members 
    dwSize 
    Specifies the length, in bytes, of the structure. Before calling the Module32First function, set this member to sizeof(MODULEENTRY32). If you do not initialize dwSize, Module32First will fail. 
    th32ModuleID 
    Module identifier in the context of the owning process. The contents of this member has meaning only to the tool help functions. It is not a handle, nor is it usable by Win32 API elements. 
    th32ProcessID 
    Identifier of the process being examined. The contents of this member can be used by Win32 API elements. 
    GlblcntUsage 
    Global usage count on the module. 
    ProccntUsage 
    Module usage count in the context of the owning process. 
    modBaseAddr 
    Base address of the module in the context of the owning process. 
    modBaseSize 
    Size, in bytes, of the module. 
    hModule 
    Handle to the module in the context of the owning process. 
    szModule 
    String containing the module name. 
    szExePath 
    String containing the location (path) of the module. 
    Note modBaseAddr and hModule are valid only in the context of the process specified by th32ProcessID. QuickInfo
      Windows NT: Requires version 5.0 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in tlhelp32.h.See Also
    Tool Help Library Overview, Tool Help Structures  
      

  4.   

    GetModuleFileNameEx
    The GetModuleFileNameEx function retrieves the fully qualified path for the specified module. DWORD GetModuleFileNameEx(
      HANDLE hProcess,    // handle to the process
      HMODULE hModule,    // handle to the module
      LPTSTR lpFilename,  // buffer that receives the path
      DWORD nSize         // size of the buffer
    );
     
    Parameters
    hProcess 
    Handle to the process that contains the module. 
    hModule 
    Handle to the module. 
    lpFilename 
    Pointer to the buffer that receives the fully qualified path to the module. 
    nSize 
    Specifies the size, in bytes, of the lpFilename buffer. 
    Return Value
    If the function succeeds, the return value specifies the length of the string copied to the buffer. If the function fails, the return value is zero. To get extended error information, call GetLastError. See Also
    Process Status Helper Overview, PSAPI Functions, EnumProcesses, GetModuleBaseName  
      

  5.   

    Module32First 本身没有什么问题,也不应该出现得不到全路径的问题,不知道楼主具体问题在哪儿。提供一点修改意见,楼主看一下吧。hfilename = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE , procList.th32ProcessID); 
    Module32First(hfilename,&pes);要判断 hfilename 的有效性,而且,还要判断 Module32First 的返回值,最后,要关闭句柄。像这样while(hfilename != INVALID_HANDLE_VALUE && bRet)
    {
    hfilename = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, procList.th32ProcessID);
    if(hfilename != INVALID_HANDLE_VALUE && Module32FirstW(hfilename, &pes))
    {
    CloseHandle(hfilename);
    wprintf(L"%s\n", pes.szModule);
    wprintf(L"%s\n", pes.szExePath);
    }
    bRet = Process32Next(hSnapshot, &procList);
    }
    CloseHandle(hSnapshot);
      

  6.   

    while 那里写错了,是
    while(hSnapshot != INVALID_HANDLE_VALUE && bRet)
      

  7.   

    谢谢仁兄,问题应该是进程的令牌权限问题,MODULEENTRY32确实没错,不过有些进程貌似你必须有足够的权限才能访问其信息,刚才用了版主的方法解决了一些,仁兄你用MODULEENTRY32可以枚举到所有进程的路径吗
      

  8.   

    哈哈,成功了HANDLE pHandle=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE,procList.th32ProcessID);//必须有这两个权限DWORD dword=GetModuleFileNameEx(pHandle,NULL,path,MAX_PATH);这样就可以了,,另外系统进程SYSTEM和系统空闲进程是没有路径的,path会存成乱码,你用系统自带的任务管理器或者金山的都能发现路径是空白的!
      

  9.   

    谢谢版主帮助,弄好了,不过我没用前面那个提升权限的函数,不知道咋回事,另外,仁兄我该做后续的事了,就是定位文件,网上查资料找到一个SHOpenFolderAndSelectItems的函数,但是加了头文件shlobj.h还是提示未定义,再请教一下
      

  10.   

    不要使用
    while (条件)
    更不要使用
    while (组合条件)
    要使用
    while (1) {
     if (条件1) break;
     //...
     if (条件2) continue;
     //...
     if (条件3) return;
     //...
    }
    因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
    典型如:
    下面两段的语义都是当文件未结束时读字符
    while (!feof(f)) {
     a=fgetc(f);
     //...
     b=fgetc(f);//可能此时已经feof了!
     //...
    }
    而这样写就没有问题:
    while (1) {
     a=fgetc(f);
     if (feof(f)) break;
     //...
     b=fgetc(f);
     if (feof(f)) break;
     //...
    }
    类似的例子还可以举很多。