那个函数可以得到程序自身的所在位置路径?最好给段例子
还有能告诉我一下GetModuleFileName的用法吗?

解决方案 »

  1.   

    CString str[255];
    GetCurrentDirectory(255,str);
    strcat(str,"\\myfile");
      

  2.   

    GetCurrentDirectory就可以
    GetCurrentDirectory
    The GetCurrentDirectory function retrieves the current directory for the current process. DWORD GetCurrentDirectory(
      DWORD nBufferLength,  // size of directory buffer
      LPTSTR lpBuffer       // directory buffer
    );在你程序里char dir[128];
    然后GetCurrentDirectory(128,dir),就可以
      

  3.   

    char szFile[256];
    GetModuleFileName(NULL,szFile,256);
    ::MessageBox(hWnd,szFile,"路径",MB_OK);
      

  4.   

    我觉得还是这个函数保险:DWORD GetModuleFileName(
      HMODULE hModule,    // handle to module
      LPTSTR lpFilename,  // path buffer
      DWORD nSize         // size of buffer
    );
    Parameters
    hModule 
    [in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module. 
    lpFilename 
    [out] Pointer to a buffer that receives the fully-qualified path for the module. If the length of the string exceeds the size specified by the nSize parameter, the string is truncated. 
    Windows NT/2000/XP: The path can have the prefix "\\?\", depending on how the module was loaded. For more information, see File Name Conventions. nSize 
    [in] Specifies the length of the lpFilename buffer, in TCHARs. 
      

  5.   

    GetCurrentDirectory 有时候会失灵的!
      

  6.   

    TCHAR chDir[1024];
    GetModuleFileName(NULL, chDir, 1024);
    CString strAppPath = chDir;//路徑和APP名
    strAppPath= strAppPath .Left(strAppPath.ReverseFind(_T('\\'))1);//路徑
      

  7.   

    bool GetExecutable (CString strPathNoDirectory, CString &strPathCompleted)
    {
      TCHAR                                tszTemp[2000];
      int                                  nTemp;
      HMODULE                              hSelf;
      hSelf  = NULL;
      if (!strPathNoDirectory.IsEmpty ())
        hSelf  = ::GetModuleHandle (strPathNoDirectory);  nTemp = ::GetModuleFileName (hSelf, tszTemp, sizeof(tszTemp)/sizeof(*tszTemp));
      if (0  ==  nTemp)
        return false;  ASSERT (nTemp  <  sizeof(tszTemp)/sizeof(*tszTemp));
      tszTemp[nTemp]    = _T('\0');
      strPathCompleted   = tszTemp;
      return true;
    }
      

  8.   

    或者把声明改成这样子(顺序也换了):
    bool GetExecutable (CString &strPathCompleted, CString strPathNoDirectory = _T(""));
    如果你是把函数放在DLL里的话,就必须指定strPathNoDirectory(形式如:_T("MSDEV.EXE")),如果函数不在DLL里可以不必指定该参数
      

  9.   

    用GetModuleFileName比較好
    用GetCurrentDirectory設置會有問題得.比如說你在打開一個
    CFileDialog d.Open(....);
    d.Domodal();//假如你在這裡做了路徑選擇以後
    你會發現只要你在程序任何地方緊接掉用GetCurrentDirectory..他得到得是
    CFileDialog所選擇後得路徑..這並不是你想要的.