GetModuleFileName返回值52428

Pokeeeer 2015-07-29 02:24:42
想通过GetModuleFileName获取dll所在路径,返回值错误
TCHAR CMiLang::getPath()
{
TCHAR dllPath[MAX_PATH] = {0};
GetModuleFileName(NULL, dllPath, MAX_PATH);
return dllPath[MAX_PATH];
}


main函数下单步调试,返回值是52428+一个韩文...


求大神帮分析下问题在哪。。。
...全文
275 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
二班的码农 2015-07-30
  • 打赏
  • 举报
回复
TCHAR转化成string std::string TCHAR2STRING(TCHAR *STR) { int iLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL); char* chRtn =new char[iLen*sizeof(char)]; WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL); std::string str(chRtn); return str; }
Pokeeeer 2015-07-30
  • 打赏
  • 举报
回复
引用 3 楼 SXJIAKE 的回复:
问题很多的: 1. 返回一个临时变量,而不是类成员或全局变量? 2. 返回字符串(字符地址),为什么返回的是字符? 3. 返回是一个字符也就罢了,还是个越界的字符!
class CMiLang {
private:
    TCHAR dllPath[MAX_PATH];
public:
    LPCTSTR getPath(void);
}

LPCTSTR CMiLang::getPath(void)
{
    DWORD dwCount = GetModuleFileName(NULL, dllPath, MAX_PATH);
    return ((dwCount > 0) && (dwCount < MAX_PATH)) ? dllPath : TEXT("");
}
很感谢你的指正,的确是越界了,可是size已经设置成MAX_PATH(应该是260)了,这个应该怎么处理呢? 实际上,我要得到这个路径是要对路径进行修改,比如:获取到“c:\\a.exe” 我想将a.exe replace其他文件名,在对“a.exe”进行搜索时rfind只能处理string类型的,这个应该怎么办呢?TEXT可以吧TCHAR转化成字符串吗? 还请帮吗解答!
「已注销」 2015-07-30
  • 打赏
  • 举报
回复
问题很多的: 1. 返回一个临时变量,而不是类成员或全局变量? 2. 返回字符串(字符地址),为什么返回的是字符? 3. 返回是一个字符也就罢了,还是个越界的字符!
class CMiLang {
private:
    TCHAR dllPath[MAX_PATH];
public:
    LPCTSTR getPath(void);
}

LPCTSTR CMiLang::getPath(void)
{
    DWORD dwCount = GetModuleFileName(NULL, dllPath, MAX_PATH);
    return ((dwCount > 0) && (dwCount < MAX_PATH)) ? dllPath : TEXT("");
}
Pokeeeer 2015-07-29
  • 打赏
  • 举报
回复
引用 楼主 UAVGCS 的回复:
想通过GetModuleFileName获取dll所在路径,返回值错误
TCHAR CMiLang::getPath()
{
	TCHAR dllPath[MAX_PATH] = {0};
	GetModuleFileName(NULL, dllPath, MAX_PATH);
	return dllPath[MAX_PATH];
}
main函数下单步调试,返回值是52428+一个韩文... 求大神帮分析下问题在哪。。。
dllPath[MAX_PATH]应该是一个内存地址,怎么把里面的东西拷出来打印呢?
赵4老师 2015-07-29
  • 打赏
  • 举报
回复
GetModuleFileName The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module. Windows 95: The GetModuleFilename function will return long filenames when an application's version number is greater than or equal to 4.00 and the long filename is available. Otherwise, it returns only 8.3 format filenames. DWORD GetModuleFileName( HMODULE hModule, // handle to module to find filename for LPTSTR lpFilename, // pointer to buffer to receive module path DWORD nSize // size of buffer, in characters ); Parameters hModule Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process. lpFilename Pointer to a buffer that is filled in with the path and filename of the given module. nSize Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated. Return Values If the function succeeds, the return value is the length, in characters, of the string copied to the buffer. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks If a module is loaded in two processes, its module filename in one process may differ in case from its module filename in the other process. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, GetModuleHandle, LoadLibrary

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧