在我的代码中使用 Unicode 编译 Win32 控制台项目时出现异常 (C++ Mingw64 VSCODE)
Getting An Exception When Compiling The Win32 Console Project In My Code With Unicode (C++ Mingw64 VSCODE)
我正在使用 mingw64
编译器和 VSCode。我按照教程编写了一些代码,使用 <Windows.h>
将某些内容打印到控制台。然后我修改了我的代码以使用 UNICODE 字符。我已经使用 WriteConsoleOutputCharacter*( )
这样做了。但是当我 运行 程序时,我得到一个 运行time 异常。
VSCode 中的分段错误。如果我的代码有错误,请告诉我。
#define UNICODE
#define _UNICODE
#include <windows.h>
const short screenWidth = 80, screenHeight = 80;
const wchar_t* consoleTitle = L"My Console Window";
int main()
{
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowArea = { 0, 0, screenWidth - 1, screenHeight - 1 };
COORD windowSize = { screenWidth, screenHeight };
SetConsoleWindowInfo(hOut, TRUE, &windowArea);
SetConsoleTitle(consoleTitle);
SetConsoleActiveScreenBuffer(hOut);
SetConsoleScreenBufferSize(hOut, windowSize);
wchar_t* screenBuffer = new wchar_t[screenWidth * screenHeight];
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
screenBuffer[y * screenWidth + x] = L' ';
}
}
LPDWORD numberOfCharsWritten = 0;
WriteConsoleOutputCharacter(hOut, screenBuffer, screenWidth * screenHeight, { 0, 0 }, numberOfCharsWritten);
return 0;
}
您必须声明 DWORD 变量而不是 LPDWORD,并传递变量的内存。
DWORD numberOfCharsWritten = 0;
WriteConsoleOutputCharacter(hOut, screenBuffer, screenWidth * screenHeight, { 0, 0 }, &numberOfCharsWritten);
我正在使用 mingw64
编译器和 VSCode。我按照教程编写了一些代码,使用 <Windows.h>
将某些内容打印到控制台。然后我修改了我的代码以使用 UNICODE 字符。我已经使用 WriteConsoleOutputCharacter*( )
这样做了。但是当我 运行 程序时,我得到一个 运行time 异常。
VSCode 中的分段错误。如果我的代码有错误,请告诉我。
#define UNICODE
#define _UNICODE
#include <windows.h>
const short screenWidth = 80, screenHeight = 80;
const wchar_t* consoleTitle = L"My Console Window";
int main()
{
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowArea = { 0, 0, screenWidth - 1, screenHeight - 1 };
COORD windowSize = { screenWidth, screenHeight };
SetConsoleWindowInfo(hOut, TRUE, &windowArea);
SetConsoleTitle(consoleTitle);
SetConsoleActiveScreenBuffer(hOut);
SetConsoleScreenBufferSize(hOut, windowSize);
wchar_t* screenBuffer = new wchar_t[screenWidth * screenHeight];
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
screenBuffer[y * screenWidth + x] = L' ';
}
}
LPDWORD numberOfCharsWritten = 0;
WriteConsoleOutputCharacter(hOut, screenBuffer, screenWidth * screenHeight, { 0, 0 }, numberOfCharsWritten);
return 0;
}
您必须声明 DWORD 变量而不是 LPDWORD,并传递变量的内存。
DWORD numberOfCharsWritten = 0;
WriteConsoleOutputCharacter(hOut, screenBuffer, screenWidth * screenHeight, { 0, 0 }, &numberOfCharsWritten);