如何解决无法将 DIDEVICEINSTANCE tszProductName 打印到 Windows 控制台
我一直在关注 DirectInput tutorial,但在将 DirectInput device product names 列表打印到 Windows 控制台时遇到问题。
我正在使用 VS19 的 C++ 编译器,并且正在使用定义的 UNICODE 进行编译。由于 tszProductName 是解析为 WCHAR 的 TCHAR 类型,因此我遵循 this stack overflow answer 以允许 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 */ }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。