直接输入编译错误 (C++)
DirectInput Compile Error (C++)
我正在使用Jon Parise的DirectInput包装器(抱歉,丢失了URL,找不到它(,并且在编译它时遇到问题。
我正在使用这个包装器从 3 个单独的操纵杆设备接收输入并处理数据。
以下是导致麻烦.cpp:
// DirectInput Joystick Wrapper
// Copyright 2001 by Jon Parise <jparise@cmu.edu>
//
// $Id: joystick.cpp,v 1.1.1.1 2001/10/29 19:47:56 jon Exp $
#include "stdafx.h"
#include "joystick.h"
#include <stdio.h>
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p) = NULL; } }
Joystick::Joystick(unsigned int id)
{
this->id = id;
device_counter = 0;
di = NULL;
joystick = NULL;
}
Joystick::~Joystick()
{
close();
}
HRESULT
Joystick::deviceName(char* name)
{
HRESULT hr;
DIDEVICEINSTANCE device;
ZeroMemory(&device, sizeof(device));
device.dwSize = sizeof(device);
if (!di || !joystick) {
return E_INVALIDARG;
}
if (FAILED(hr = joystick->GetDeviceInfo(&device))) {
return hr;
}
strncpy(name, device.tszProductName, MAX_PATH);
return hr;
}
HRESULT
Joystick::open()
{
HRESULT hr;
// Create a DirectInput device
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(VOID**)&di, NULL))) {
return hr;
}
// Look for the first simple joystick we can find.
if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, ::enumCallback,
(LPVOID)this, DIEDFL_ATTACHEDONLY))) {
return hr;
}
// Make sure we got a joystick
if (joystick == NULL) {
return E_FAIL;
}
// Set the data format to "simple joystick" - a predefined data format
//
// A data format specifies which controls on a device we are interested in,
// and how they should be reported. This tells DInput that we will be
// passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) {
return hr;
}
// Set the cooperative level to let DInput know how this device should
// interact with the system and with other DInput applications.
if (FAILED(hr = joystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE | DISCL_FOREGROUND))) {
return hr;
}
return S_OK;
}
HRESULT
Joystick::close()
{
if (joystick) {
joystick->Unacquire();
}
SAFE_RELEASE(joystick);
SAFE_RELEASE(di);
return S_OK;
}
HRESULT
Joystick::poll(DIJOYSTATE2 *js)
{
HRESULT hr;
if (joystick == NULL) {
return S_OK;
}
// Poll the device to read the current state
hr = joystick->Poll();
if (FAILED(hr)) {
// DirectInput is telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so we
// don't have any special reset that needs to be done. We just
// re-acquire and try again.
hr = joystick->Acquire();
while (hr == DIERR_INPUTLOST) {
hr = joystick->Acquire();
}
// If we encounter a fatal error, return failure.
if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) {
return E_FAIL;
}
// If another application has control of this device, return success.
// We'll just have to wait our turn to use the joystick.
if (hr == DIERR_OTHERAPPHASPRIO) {
return S_OK;
}
}
// Get the input's device state
if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE2), js))) {
return hr;
}
return S_OK;
}
BOOL CALLBACK
Joystick::enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
// If this is the requested device ID ...
if (device_counter == this->id) {
// Obtain an interface to the enumerated joystick. Stop the enumeration
// if the requested device was created successfully.
if (SUCCEEDED(di->CreateDevice(instance->guidInstance, &joystick, NULL))) {
return DIENUM_STOP;
}
}
// Otherwise, increment the device counter and continue with
// the device enumeration.
device_counter++;
return DIENUM_CONTINUE;
}
BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
if (context != NULL) {
return ((Joystick *)context)->enumCallback(instance, context);
} else {
return DIENUM_STOP;
}
}
unsigned int
Joystick::deviceCount()
{
unsigned int counter = 0;
LPDIRECTINPUT8 di = NULL;
HRESULT hr;
if (SUCCEEDED(hr = DirectInput8Create(GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(VOID**)&di, NULL))) {
di->EnumDevices(DI8DEVCLASS_GAMECTRL, ::countCallback,
&counter, DIEDFL_ATTACHEDONLY);
}
return counter;
}
BOOL CALLBACK
countCallback(const DIDEVICEINSTANCE* instance, VOID* counter)
{
if (counter != NULL) {
unsigned int *tmpCounter = (unsigned int *)counter;
(*tmpCounter)++;
counter = tmpCounter;
}
return DIENUM_CONTINUE;
}
引发的错误是:
error C2664: 'char *strncpy(char *,const char *,size_t)': cannot convert argument 2 from 'WCHAR [260]' to 'const char *'
有没有人知道如何解决这个问题?
如果我注释掉有问题的行,我可以让代码编译和工作,但我需要该功能(基本上告诉我 3 个操纵杆中的哪一个是操纵杆、油门和踏板(
DIDEVICEINSTANCE::tszProductName
是一种TCHAR
类型,定义为在Unicode平台上wchar_t
,在ANSI平台上char
。
更改项目设置以使用 ANSI 编码而不是 Unicode,或者相应地更改代码的相关部分。您只是不能将char
与wchar_t
混为一谈.
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'