c++ - 无法将 DIDEVICEINSTANCE tszProductName 打印到 Windows 控制台
问题描述
我一直在学习 DirectInput 教程,但无法将DirectInput 设备产品名称列表打印到 Windows 控制台。
我正在使用 VS19 的 C++ 编译器并使用定义的 UNICODE 进行编译。由于 tszProductName 是解析为 WCHAR 的 TCHAR 类型,因此我遵循此堆栈溢出答案以允许 Windows 控制台打印 unicode,同时避免在同一程序中将 wcout 与 cout 混合。
在我进行这些更改之前,没有打印任何内容。现在控制台会为每个设备名称重复打印“쳌”。我尝试从 Unicode 切换到多字节并返回到 cout 无济于事。这是有问题的代码。
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#pragma comment (lib,"dinput8.lib")
#pragma comment (lib,"dxguid.lib")
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <locale.h>
#include <clocale>
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
IDirectInput8* dev;
std::vector<LPDIRECTINPUTDEVICE8> gameControllers;
BOOL CALLBACK enumGameControllers(LPCDIDEVICEINSTANCE devInst, LPVOID pvRef) {
LPDIRECTINPUTDEVICE8 gameController;
if (FAILED(dev->CreateDevice(devInst->guidInstance, &gameController, NULL)))
return DIENUM_CONTINUE;
else {
gameControllers.push_back(gameController);
return DIENUM_CONTINUE;
}
}
int wmain(int argc, wchar_t* argv[]) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
char* a = setlocale(LC_ALL, "en-US.UTF8");
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
CONSOLE_FONT_INFOEX fontInfo;
fontInfo.cbSize = sizeof(fontInfo);
fontInfo.FontFamily = 20;
fontInfo.FontWeight = 400;
fontInfo.nFont = 0;
const wchar_t myFont[] = L"Lucida Console";
fontInfo.dwFontSize = { 8, 16 };
std::copy(myFont, myFont + _countof(myFont), fontInfo.FaceName);
SetCurrentConsoleFontEx(hConsole, false, &fontInfo);
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dev, NULL))) {
std::wcout << L"Critical error: Unable to create the main DirectInput 8 COM object!\n";
return 1;
}
if (FAILED(dev->EnumDevices(DI8DEVCLASS_GAMECTRL, &enumGameControllers, NULL, DIEDFL_ATTACHEDONLY))) {
std::wcout << L"Critical error: Unable to enumerate input devices!\n";
return 1;
}
if (gameControllers.empty()) {
std::wcout << L"No peripherals found\n";
return 1;
}
int count = 1;
std::wcout << L"Number of devices: " << gameControllers.size() << std::endl;
for (LPDIRECTINPUTDEVICE8 i : gameControllers) {
DIDEVICEINSTANCE deviceInfo;
i->GetDeviceInfo(&deviceInfo);
std::wcout << L"Device Number " << count << L": ";
std::wcout << deviceInfo.tszProductName << std::endl;
if (FAILED(i->SetCooperativeLevel(GetConsoleWindow(), DISCL_BACKGROUND | DISCL_EXCLUSIVE))) {
std::wcout << L"Cooperative level could not be set\n";
return 1;
}
++count;
}
return 0;
}
提前感谢您的任何建议和/或解决方案。
解决方案
DIDEVICEINSTANCE deviceInfo;
i->GetDeviceInfo(&deviceInfo);
这段代码的问题是:
DIDEVICEINSTANCE::dwSize
未按要求初始化,并且的返回值
IDirectInputDevice8::GetDeviceInfo
不检查错误。
改为以下内容。
DIDEVICEINSTANCE deviceInfo = { sizeof(DIDEVICEINSTANCE) };
if(i->GetDeviceInfo(&deviceInfo) != DI_OK) { /* call failed, handle error */ }
推荐阅读
- c# - C# 要修复此错误,请将 JSON 更改为 JSON 数组
- android - 编程套接字以将 Kotlin Android 应用程序客户端连接到 Python 服务器
- python - PyQt5 选择要播放的音频设备输出
- javascript - 如何访问浏览器日志并将它们以编程方式存储在变量中?
- javascript - 如何通过 URL 获取图像?(JavaScript)
- java - 在片段内使用 switch case
- spring-boot - ERROR 6748 dispatcherServlet in context with path [] throw exception [.....] with root cause
- swift - 格式化 URL 并打开它们
- python - 在 pygame 空间入侵者的按键上交换船舶图像
- c++ - 函数如何返回一个大于它正在填充的 char 数组的 cstring?