获取运行exe的文件路径:
const TCHAR* GetAppPath(__inout LPTSTR lpOut, __in const DWORD nSize)
{
TCHAR szBuf[MAX_PATH] = { 0 };
ZeroMemory(szBuf, sizeof(szBuf));
assert(NULL != lpOut);
if (NULL != lpOut && ::GetModuleFileName(NULL, szBuf, MAX_PATH))
{
::PathRemoveFileSpec(szBuf);
_tcsncpy_s(lpOut, nSize, szBuf, _TRUNCATE);
return lpOut;
}
return NULL;
}
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.
(如果模块是由其他进程加载的,需要用另外一个函数GetModuleFileNameEx。)
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.
(
被请求的加载模块的句柄。
如果这个参数是NULL,GetModuleFileName将检索当前进程的可执行文件的路径。)
-
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.
-
(一个指向一个缓冲区的指针,它接收到模块的全限定路径。如果路径的长度超过nSize参数指定的大小,那么函数将成功,并且字符串被截断为nSize字符,不能为null终止。)
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.
(
返回的字符串将使用与加载模块时指定的格式相同的格式。
因此,路径可以是一个长或短的文件名,并且可以使用前缀“\\?.\”
要了解更多信息,请参 阅命名文件。)
-
The size of the lpFilename buffer, in TCHARs.
lpFilename
nSize
PathRemoveFileSpec():
Removes the trailing file name and backslash from a path, if they are present.(如果这个文件名存在,那么移除路径最后面的文件名)
Syntax
BOOL PathRemoveFileSpec( LPTSTR pszPath);
Parameters
Return ValuepszPath
- [in, out] A pointer to a null-terminated string of maximum length MAX_PATH that contains the path from which to remove the file name.( 一个指向一个以NULL终止的最大长度为maxpath的字符串的指针,该字符串包含了删除文件名的路径。)
Returns nonzero if something was removed, or zero otherwise.(如果移除了一些文件名,则返回非0,否则返回0)