C++实现获取当前执行文件全路径

用GetModuleFileName(NULL,exeFullPath,MAX_PATH)得到当前执行文件的全路径。

 

#include <Windows.h>	
#include <iostream>	
#include <string>	
using namespace std;	
	
string GetProgramDir()	
{	
	char exeFullPath[MAX_PATH];	// Full path
	string strPath = "";

	GetModuleFileName(NULL,exeFullPath,MAX_PATH);
	strPath=(string)exeFullPath;	// Get full path of the file
	int pos = strPath.find_last_of('\\', strPath.length());
	return strPath.substr(0, pos);	// Return the directory without the file name
}	

int main ()
{
	string str = "";

	str = GetProgramDir();
	cout << str << endl;

	return 0;
}

 

posted on 2010-11-02 22:08  浩然119  阅读(23977)  评论(0编辑  收藏  举报