gpt4 book ai didi

c++ - 为什么 WinAPI FormatMessage 失败,总是返回 false

转载 作者:行者123 更新时间:2023-11-30 02:51:03 26 4
gpt4 key购买 nike

我正在尝试从 FormatMessage 方法获取错误消息,但该方法总是返回 false 并且 lpBuffer 为空。

我想知道我做错了什么?

谢谢

LPTSTR lpBuffer = NULL;
DWORD dwError = GetLastError();
int nResult = 0;
if (dwError >= 12000 && dwError <= 12174)
{
logger.info("dwError >= 12000 && dwError <= 12174");
nResult = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE, GetModuleHandle("wininet.dll"), dwError, 0, lpBuffer, 0, NULL);
}
else
{
logger.info("in else");
nResult = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),lpBuffer, 0, NULL);
}

if (nResult)
{
logger.critical("Cannot start Drools client [" + m_exe + "] because [" + lpBuffer + "]");
//wcout << (LPWSTR)lpBuffer << endl;
LocalFree(lpBuffer);
}
else
{
logger.critical("Cannot start Drools client [" + m_exe + "] because [ Windows error code : " + boost::lexical_cast<string>(dwError) + " ]");
}

最佳答案

你说错了。如果您传递 FORMAT_MESSAGE_ALLOCATE_BUFFER,则必须为其提供存储缓冲区的位置,因此“您必须将指针转换为 LPTSTR(例如,(LPTSTR)&lpBuffer)。 "

即:

nResult = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle("wininet.dll"),
dwError,
0,
(LPTSTR)&lpBuffer,
BUFFER_SIZE,
NULL
);

请参阅此处的文档:

关于c++ - 为什么 WinAPI FormatMessage 失败,总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19979625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com