无法将 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 deviceInfo = { sizeof(DIDEVICEINSTANCE) };
    if(i->GetDeviceInfo(&deviceInfo) != DI_OK) { /* call failed,handle error */ }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...