双击MFC文档时如何获取MFC应用程序文件的路径

问题描述:

你好家伙

我有一个MFC应用程序,并且doc是* .cvt

当我双击cvt文件时,GetCurrentDir函数获取cvt的路径文件,而不是应用程序文件的路径

如何获取路径应用程序文件?



Hi guy
I'm have a MFC application, and there doc is *.cvt
When I double click cvt file, GetCurrentDir function is get path of cvt file, not path of application file
How i get path Application file ?

CString utility::GetPathExe()
{
	char cCurrentPath[FILENAME_MAX];
	if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
	{
		return _T("");
	}
	cCurrentPath[sizeof(cCurrentPath)-1] = '\0'; /* not really required */
	CString szResult = _T("");
	szResult = cCurrentPath;
	return szResult;
}

可执行文件的路径和名称存储在传递的第一个命令行参数数组条目中到主要功能。所以你可以复制或存储一个指针。



或者你可以使用 GetModuleFileName() [ ^ ]功能:

The path and name of the executable file are stored in the first command line argument array entry that is passed to the main function. So you may copy that or store a pointer.

Alternatively you can use the GetModuleFileName()[^] function:
// Get path of the executable file of the current process
TCHAR path[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH);
// Remove the file name
// Set ext[0] to zero to strip also the trailing back slash
LPCTSTR ext = _tcsrchr(path, _T('\\'));
if (ext)
    ext[1] = _T('\0');