据我所知,WriteConsole()
函数将一个字符数组写入屏幕缓冲区。直到我试着做一个循环,它才开始工作。
#include <iostream>
#include <Windows.h>
int main() {
HANDLE hScreenBuffer;
hScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);
char map[100];
LPDWORD number_of_chars_to_write;
for (int i = 0; i < 100; i++) {
map[i] = '#';
}
while (1) {
WriteConsole(
hScreenBuffer,
map,
100,
number_of_chars_to_write,
NULL
);
}
}
您的代码中有两个问题:
首先,正如评论中指出的,您需要使用WriteConsoleA
,因为您写窄char
s。
第二,您将未初始化的指针LPDWORD number_of_chars_to_write;
传递给WriteConsole
,这可能会导致崩溃。
这是正确的代码:
#include <iostream>
#include <Windows.h>
int main() {
HANDLE hScreenBuffer;
hScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);
char map[100];
DWORD number_of_chars_to_write;
for (int i = 0; i < 100; i++) {
map[i] = '#';
}
while (1) {
WriteConsoleA(
hScreenBuffer,
map,
100,
&number_of_chars_to_write,
NULL
);
}
}