【问题标题】:Trouble printing DIDEVICEINSTANCE tszProductName to Windows Console无法将 DIDEVICEINSTANCE tszProductName 打印到 Windows 控制台
【发布时间】:2021-04-08 13:59:08
【问题描述】:

我一直在关注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;
}

提前感谢您提供任何建议和/或解决方案。

【问题讨论】:

    标签: c++ windows unicode wchar directinput


    【解决方案1】:
        DIDEVICEINSTANCE deviceInfo;
        i->GetDeviceInfo(&deviceInfo);
    

    这段代码的问题是:

    改为以下内容。

        DIDEVICEINSTANCE deviceInfo = { sizeof(DIDEVICEINSTANCE) };
        if(i->GetDeviceInfo(&deviceInfo) != DI_OK) { /* call failed, handle error */ }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 2022-10-19
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多