VS2008 C++ 获取exe文件所在路径

本文介绍了一个从TCHAR类型转换为char*类型的实用示例,通过使用WideCharToMultiByte函数实现宽字符到多字节字符的转换,并展示了如何获取当前模块文件路径并进行转换。
TCHAR szFilePath[MAX_PATH + 1] = { 0 };
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0;

TCHAR转换为char*:

char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)  
{  
	LPSTR pszOut = NULL;  
	if (lpwszStrIn != NULL)  
	{  
		int nInputStrLen = wcslen (lpwszStrIn);  

		// Double NULL Termination  
		int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;  
		pszOut = new char [nOutputStrLen];  

		if (pszOut)  
		{  
			memset (pszOut, 0x00, nOutputStrLen);  
			WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);  
		}  
	}  
	return pszOut;  
}  

实例输出:

int main()
{
	TCHAR szFilePath[MAX_PATH + 1] = { 0 };  
	GetModuleFileName(NULL, szFilePath, MAX_PATH);  
	(_tcsrchr(szFilePath, _T('\\')))[1] = 0;
	char *p = ConvertLPWSTRToLPSTR (szFilePath); 
	cout<<p<<endl;

	return 0;
}

C++中,有多种方法可以获取当前运行的exe所在的路径,以下为你介绍几种常见的方法: ### 方法一:通过主函数参数 将主函数写成`int main(int argc, char *argv[])`的形式,文件路径存放在`argv[]`数组里。示例代码如下: ```cpp #include <iostream> using namespace std; int main(int argc, char *argv[]) { cout << *argv; // 此处就可以显示文件路径 return 0; } ``` 需要注意的是,虽然`int argc`在这里没有用到,但是不能省略[^1]。 ### 方法二:使用`GetModuleFileNameA`函数 通过`GetModuleFileNameA`函数获取当前exe的完整路径,再使用`strrchr`函数删除文件名,只保留路径字符串。示例代码如下: ```cpp #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <Windows.h> #include <string> using namespace std; string GetExePath() { char szFilePath[MAX_PATH + 1] = { 0 }; GetModuleFileNameA(NULL, szFilePath, MAX_PATH); (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串 string path = szFilePath; return path; } int main() { string str = GetExePath(); system("pause"); return 0; } ``` 此方法适用于Windows系统,能满足不想使用公司库,将文件放到exe所在文件夹以避免使用绝对路径的需求[^2]。 ### 方法三:使用`GetModuleFileName`结合`substr`和`find_last_of` 使用`GetModuleFileName`函数获取exe的完整路径,再利用`substr`和`find_last_of`函数提取路径部分。示例代码如下: ```cpp #include <windows.h> #include <iostream> #include <string> int main() { char buffer[MAX_PATH]; GetModuleFileName(NULL, buffer, MAX_PATH); std::string exePath(buffer); std::string exeDirectory = exePath.substr(0, exePath.find_last_of("\\/")); std::cout << "Current executable path: " << exeDirectory << std::endl; return 0; } ``` 这种方法同样适用于Windows系统,能有效提取exe所在路径[^3]。 ### 方法四:跨平台方法 使用条件编译,针对Windows和非Windows系统分别处理。在Windows系统中使用`GetModuleFileName`,在非Windows系统中使用`readlink`函数。示例代码如下: ```cpp #include <iostream> #include <string> #include <windows.h> int main(void) { #if WIN32 char pbuf[128] = { 0 }; int bytes = GetModuleFileName(NULL, pbuf, sizeof(pbuf)); std::cout << bytes << " " << pbuf << std::endl; #else #define PATH_MAX 128 char result[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); std::cout << std::string(result, (count > 0) ? count : 0); #endif return 0; } ``` 此方法具有跨平台性,可在不同操作系统下获取exe所在路径[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值