在 CodeLite 中编译代码时,中文输出乱码通常是因为控制台编码设置不正确。你可以通过以下几种方式解决这个问题:
设置控制台编码
在 Windows 控制台中,你可以通过设置代码页(SetConsoleOutputCP(CP_UTF8);)来改变字符编码。你可以在代码中添加以下行来设置控制台为 UTF-8 编码:
#include <windows.h>
#include <stdio.h>
int main() {
// 设置控制台为 UTF-8 编码
SetConsoleOutputCP(CP_UTF8);
// 其余代码...
}
#include <windows.h>
#include <stdio.h>
int main() {
HKEY hKey;
LONG result;
char value[256];
DWORD valueLength = sizeof(value);
SetConsoleOutputCP(CP_UTF8);
// 打开注册表键
result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey);
if (result != ERROR_SUCCESS) {
printf("无法打开注册表键。错误代码: %ld\n", result);
return 1;
}
// 查询注册表值
result = RegQueryValueEx(hKey, "ProductName", NULL, NULL, (LPBYTE)value, &valueLength);
if (result == ERROR_SUCCESS) {
printf("Windows 产品名称: %s\n", value);
} else {
printf("无法读取注册表值。错误代码: %ld\n", result);
}
// 关闭注册表键
RegCloseKey(hKey);
return 0;
}