【发布时间】:2020-04-08 06:22:07
【问题描述】:
我正在尝试在单元测试中创建一个尽可能简单的 win32 窗口,但我收到错误 87:如何获取哪个参数不正确?
#include <windows.h>
#include <strsafe.h>
LPTSTR GetErrorMessage(wstring fnName)
{
LPTSTR messageBuffer{};
LPTSTR displayBuffer{};
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&messageBuffer, 0, 0);
displayBuffer = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, lstrlen(messageBuffer) + (fnName.size() + 40) * sizeof(TCHAR));
StringCchPrintf(displayBuffer, LocalSize(displayBuffer) / sizeof(TCHAR), L"%s failed with error %d: %s", fnName.c_str(), dw, messageBuffer);
return displayBuffer;
}
TEST_METHOD(WinTest)
{
auto hwnd = CreateWindowExW(WS_EX_APPWINDOW, 0, L"test w", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 800, 600, 0, 0, 0, 0);
Assert::IsNotNull(hwnd, GetErrorMessage(L"CreateWindowExW"));
ShowWindow(hwnd, SW_SHOWDEFAULT);
Sleep(1000);
}
断言失败。 CreateWindowExW 失败,错误 87:参数不正确。
【问题讨论】:
-
类名不能为0
-
@walnut 对。我按照您的建议进行了编辑,以消除这种干扰。