我有一个文件main.exe,但是不知道放在哪个盘哪个文件夹中,我想得到这个程序的完全路径,要用什么函数?怎样做?谢谢!

解决方案 »

  1.   

    main.exe内部可以用GetModuleFileName得到路径
      

  2.   

    以下例程摘自msdn,打印系统全部进程所有模块的文件路径和名称
    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"void PrintModules( DWORD processID )
    {
        HMODULE hMods[1024];
        HANDLE hProcess;
        DWORD cbNeeded;
        unsigned int i;    // Print the process identifier.    printf( "\nProcess ID: %u\n", processID );    // Get a list of all the modules in this process.    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                        PROCESS_VM_READ,
                                        FALSE, processID );
        if (NULL == hProcess)
            return;    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
        {
            for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
            {
                char szModName[MAX_PATH];            // Get the full path to the module's file.            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
                                          sizeof(szModName)))
                {
                    // Print the module name and handle value.                printf("\t%s (0x%08X)\n", szModName, hMods[i] );
                }
            }
        }    CloseHandle( hProcess );
    }int main( )
    {
        // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return 1;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name of the modules for each process.    for ( i = 0; i < cProcesses; i++ )
            PrintModules( aProcesses[i] );    system( "PAUSE" );
            
        return 0;
    }
      

  3.   

    如果程序还没有运行,那么你就做个递归的Find吧。整个机器搜索
      

  4.   

    TO ouyh12345(五岭散人) 
     谢谢你,我要的不是这种。我要的是单独的外挂小程序,指定文件或者程序名称,就能够找到文件路径。TO  WingForce(初六,履霜,坚冰至。) 
    谢谢你,你的得到文件路径的函数是GetModuleFileNameEx()和GetModuleFileName差不多。我要的就好是搜索文件一样。
      

  5.   

    TO  happyparrot(快乐鹦鹉) 
     你能具体给我几个函数吗?谢谢!那种不管程序有没有运行都能够搜索到的。
      

  6.   

    #define _WIN32_WINNT 0x0501#include <windows.h>
    #include <string.h>
    #include <stdio.h>int main(int argc, char *argv[])
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       char DirSpec[MAX_PATH];  // directory specification
       DWORD dwError;   printf ("Target directory is %s.\n", argv[1]);
       strncpy (DirSpec, argv[1], strlen(argv[1])+1);
       strncat (DirSpec, "\\*", 3);   hFind = FindFirstFile(DirSpec, &FindFileData);   if (hFind == INVALID_HANDLE_VALUE) 
       {
          printf ("Invalid file handle. Error is %u\n", GetLastError());
          return (-1);
       } 
       else 
       {
          printf ("First file name is %s\n", FindFileData.cFileName);
          while (FindNextFile(hFind, &FindFileData) != 0) 
          {
             printf ("Next file name is %s\n", FindFileData.cFileName);
          }
        
          dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES) 
          {
             printf ("FindNextFile error. Error is %u\n", dwError);
             return (-1);
          }
       }
       return (0);
    }
    //------------------------------------------------
    主要是2个函数的使用:
    FindFirstFile
    FindNextFile
      

  7.   

    没啥捷径,用BeRoy(失眠)的方法开多线程去Find吧,呵呵
      

  8.   

    多谢各位,看来是没有什么好方法了。还是用GetModuleFileName 或者 GetFullPathName吧。