用什么API函数读取自身的EXE文件所在目录?

解决方案 »

  1.   

    TCHAR szBuffer[_MAX_PATH]; 
    VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuffer, _MAX_PATH));只要目录的话可以用
    PathRemoveFileSpec
    msdn上有示例
      

  2.   

    同意楼上用GetModuleFileName()
    DWORD GetModuleFileName(
      HMODULE hModule,    // handle to module
      LPTSTR lpFilename,  // path buffer
      DWORD nSize         // size of buffer
    );
    如果第一个参数为NULL表示获取当前程序的可执行文件的路径!
      

  3.   

    DWORD GetModuleFileName(
      HMODULE hModule,    // handle to module to find filename for
      LPTSTR lpFilename,  // pointer to buffer to receive module path
      DWORD nSize         // size of buffer, in characters
    );If hModule is NULL, GetModuleFileName returns the path for the file used to create the calling process.
      

  4.   

    GetCurrentDirectory得到的是当前的工作目录,不是在所有情况下都跟exe所在的目录相同的。
      

  5.   

    CString sPath;
    GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
    sPath.ReleaseBuffer ();
    int nPos;
    nPos=sPath.ReverseFind ('\\');
    sPath=sPath.Left (nPos);//sPath中就保存了可执行文件的当前目录