VC得到当前目录和指定应用程序目录的方法

2012-04-24 20:38:38|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C/C++, MFC, 操作系统, 文件操作|来源:唯设编程网

2.GetModuleFileName函数:

Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.

To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.

DWORD WINAPI GetModuleFileName(
  __in     HMODULE hModule,
  __out     LPTSTR lpFilename,
  __in     DWORD nSize
);

Parameters

hModule

A handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.

lpFilename

A pointer to a buffer that receives the fully-qualified path of the module. If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds, and the string is truncated to nSize characters and cannot be null terminated.

The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "?". For more information, see Naming a File.

nSize

The size of the lpFilename buffer, in TCHARs.

Return Value

If the function succeeds, the return value is the length of the string that is copied to the buffer, in TCHARs. If the buffer is too small to hold the module name, the string is truncated to nSize, the function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.

If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

Remarks

If a DLL is loaded in two processes, its file name in one process may differ in case from its file name in the other process.

For the ANSI version of the function, the number of TCHARs is the number of bytes; for the Unicode version, it is the number of characters.

The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file.

Windows Me/98/95:  This function retrieves long file names when an application version number is greater than or equal to 4.00 and the long file name is available. Otherwise, it returns only 8.3 format file names.

使用实例:

    bool ReadIni() 
    { 
         
        char databuf[1024]; 
         
        CString sPath; 
        GetModuleFileName(NULL,sPath.GetBufferSetLength(1023),1024); 
        sPath.ReleaseBuffer(); 
        int nPos; 
        nPos = sPath.ReverseFind(’’); 
        sPath = sPath.Left(nPos); 
     
        CString str = sPath + "database.ini"; 
     
        char filepath[1024]; 
         strcpy(filepath,(LPSTR)(LPCTSTR)str); 
         
        CFile file; 
        if ( !file.Open(filepath,CFile::modeRead))  
        { 
            return false; 
        } 
        file.Close(); 
         
        memset(databuf,0,1024); 
        GetPrivateProfileString("oracle","user id","",databuf,1024,filepath); 
        m_sUserId = databuf; 
         
        memset(databuf,0,1024); 
        GetPrivateProfileString("oracle","password","",databuf,1024,filepath); 
        m_sPassword = databuf; 
         
        memset(databuf,0,1024); 
        GetPrivateProfileString("oracle","data source","",databuf,1024,filepath); 
        m_sDataSource = databuf; 
         
        memset(databuf,0,1024);         
        memset(filepath,0,1024); 
     
        return true; 
    }

在通常情况下,你会发现用这2个函数并没有任何的不一样。但是当你在同一个程序里面,哪怕是不同的界面,如果你打开一个文件选择或保存对话框的时候,用 GetCurrentDirectory函数的这种方法就不行了。因为当前目录被你的文件选择或保存对话框修改了。这时用 GetModuleFileName函数的方法就非常有必要,但要注意的是GetModuleFileName得到的应用路径已经包含exe扩展名,所以 EXE名还是要去掉的。(前提是database.ini和EXE文件是在同个目录下)

<12>
发表评论0条 】
网友评论(共?条评论)..
VC得到当前目录和指定应用程序目录的方法