哪个API可以取得本程序所在目录的绝对路径
如题 问题点数:30、回复次数:5Top
1 楼zfive5(醉马不肖)回复于 2003-07-02 13:44:07 得分 10
GetModuleFileName
The GetModuleFileName function retrieves the fully qualified path for the specified module.
To specify the process that contains the module, use the GetModuleFileNameEx function.
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.
Return Values
If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs.
If the function fails, the return value is 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.
Windows 95/98/Me: The GetModuleFilename function retrieves long file names when an application's 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.
Windows 95/98/Me: GetModuleFileNameW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
Top
2 楼zfive5(醉马不肖)回复于 2003-07-02 13:44:58 得分 0
GetModuleFileName
The GetModuleFileName function retrieves the fully qualified path for the specified module.
To specify the process that contains the module, use the GetModuleFileNameEx function.
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.
Return Values
If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs.
If the function fails, the return value is 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.
Windows 95/98/Me: The GetModuleFilename function retrieves long file names when an application's 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.
Windows 95/98/Me: GetModuleFileNameW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
Top
3 楼newkey007(无限天空 www.xDrv.com)回复于 2003-07-02 13:45:58 得分 8
TCHAR szItemValue[MAX_PATH];
// 得到程序全路径名
GetModuleFileName( NULL, szItemValue, MAX_PATH );
AppPath.Format("%s",szItemValue);Top
4 楼zfive5(醉马不肖)回复于 2003-07-02 13:46:00 得分 0
hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.Top
5 楼FAICHEN(CC)回复于 2003-07-02 13:56:25 得分 2
agreeTop
6 楼xubobbs(波波酷)回复于 2003-07-02 13:57:09 得分 10
我给你一个函数吧,你放在你的APP文件类里面
CString CXXXXXXApp::GetCurDir()
{
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFilename[_MAX_FNAME],Filename[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];
GetModuleFileName(AfxGetInstanceHandle(), Filename, _MAX_PATH);
_tsplitpath(Filename, sDrive, sDir, sFilename, sExt);
CString homeDir(CString(sDrive) + CString(sDir));
int nLen = homeDir.GetLength();
if(homeDir.GetAt(nLen-1) != _T('\\'))
homeDir += _T('\\');
return homeDir;
}Top




