【问题标题】:DX11 DirectInput8Create causing LNK2019 errorDX11 DirectInput8Create 导致 LNK2019 错误
【发布时间】:2017-07-18 12:06:35
【问题描述】:

我正在尝试创建一个 GPU 渲染的粒子系统,它使用这个输入类来处理鼠标/键盘输入。

问题在于线路;

HRESULT result = DirectInput8Create(.....);

导致 LNK2019: Unresolved External Symbol 错误。我已经包含了必要的文件,所以我不确定为什么会这样。下面分别是Input.hInput.cpp文件。

INPUT.H文件

#ifndef _INPUT_
#define _INPUT_

#include <stdafx.h>
#include <dinput.h>

class Input{
private:
    IDirectInputDevice8*    _DIKeyboard;
    IDirectInputDevice8*    _DIMouse;

    LPDIRECTINPUT8          _directInput;

    LONG                    _mouseXabsolute, _mouseYabsolute, _mouseZabsolute;
    LONG                    _mouseXrelative, _mouseYrelative, _mouseZrelative;
    BYTE                    _keyboardState[256];
    BYTE                    _leftMouseButton, _rightMouseButton;

    int                     _screenWidth, _screenHeight;
    HWND                    _hWnd;

    POINT                   _point;
    RECT                    _rect;

public:
    Input();
    ~Input();

    void unload();
    bool initializeInput(HINSTANCE hInstance, HWND hWnd, int screenWidth, int screenHeight);

    void updateInput();

    BYTE* getKeyboardState();

    LONG getMouseXRelative();
    LONG getMouseYRelative();
    LONG getMouseZRelative();

    LONG getMouseXAbsolute();
    LONG getMouseYAbsolute();
    LONG getMouseZAbsolute();

    BYTE getLeftMouseClick();
    BYTE getRightMouseClick();
};

#endif 

INPUT.CPP文件

#include <stdafx.h>
#include <Input.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

using namespace std;

Input::Input() : _DIKeyboard(), _DIMouse(), _directInput(), _point(), _rect(){
    _mouseXabsolute = _mouseYabsolute = 0;
    _mouseZabsolute = 1;
    _mouseXrelative = _mouseXrelative = _mouseXrelative = 0;
}

Input::~Input(){
    unload();
}

void Input::unload(){
    if (_DIKeyboard) _DIKeyboard->Release();
    if (_DIMouse) _DIMouse->Release();
    if (_directInput) _directInput->Release();
}

bool Input::initializeInput(HINSTANCE hInstance, HWND hWnd, int screenWidth, int screenHeight){

    _screenWidth = screenWidth;
    _screenHeight = screenHeight;
    _hWnd = hWnd;
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////Create direct input, keyboard and mouse devices///////////////////////////////////////////////////

    HRESULT result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&_directInput, NULL);

    if (FAILED(result)){
        MessageBox(0, L"Could not create direct input!", L"Error", MB_OK);
        return false;
    }
...
...
...
}

对于解决此问题的任何帮助,我将不胜感激。

【问题讨论】:

  • 这是一个链接器错误,因此您的 .h 和 .cpp 文件无关紧要。您只是忘记链接导入库 dinput8.lib
  • 请注意,如果您使用的是 DirectX 11,则无需使用古老的 DirectInput。此外,您不应该在现代版本的 Windows 上使用 DirectInput 进行键盘或鼠标输入——它只是在 Win32 消息之上实现的。见DirectX Tool Kit: Now with GamePadsDirectX Tool Kit: Keyboard and Mouse support

标签: c++ directx directx-11 unresolved-external directinput


【解决方案1】:

你应该链接到 dinput8.lib 静态库:

#pragma comment(lib, "dinput8")

此外,您应该考虑使用原始输入 API 而不是 DirectInput:https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx

【讨论】:

  • 我还必须添加 dxguid。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 2021-04-14
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
相关资源
最近更新 更多