获取程序运行目录

获取运行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将检索当前进程的可执行文件的路径。
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.

一个指向一个缓冲区的指针,它接收到模块的全限定路径。如果路径的长度超过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.

  ( 返回的字符串将使用与加载模块时指定的格式相同的格式。 因此,路径可以是一个长或短的文件名,并且可以使用前缀“\\?.\” 要了解更多信息,请参   阅命名文件。
nSize

The size of the lpFilename buffer, in TCHARs.

PathRemoveFileSpec():

Removes the trailing file name and backslash from a path, if they are present.(如果这个文件名存在,那么移除路径最后面的文件名)

Syntax

BOOL PathRemoveFileSpec(   LPTSTR pszPath);

Parameters

pszPath

[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的字符串的指针,该字符串包含了删除文件名的路径。
Return Value

Returns nonzero if something was removed, or zero otherwise.(如果移除了一些文件名,则返回非0,否则返回0)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值