GetRawInputDeviceList失败,错误87(参数不正确)

时间:2014-07-29 04:43:06

标签: winapi ada raw-input

我在为64位应用程序调用Raw Input函数时遇到问题,在从32位切换之前工作正常。我不知道参数的大小需要是什么,或者当我切换位大小时它们为什么会改变。具体来说,我对API做的第一个调用是GetRawInputDeviceList,它失败了,错误代码为87.我想让它在64位工作,但我已经尝试了所有东西(类型转换指针等)但没有成功。

这与此问题非常相似:

http://social.msdn.microsoft.com/Forums/en-US/23414edf-0288-4148-98ef-1a03742a6ef8/raw-input-x64-api-question-getrawinputdevicelist-call-fails-in-windows-7-x64?forum=libraryfeedback

编辑:我项目的代码

function Get_Device_List(
  List  : in Address; -- Assume 8 byte pointer
  Count : in Address; -- Assume 8 byte pointer
  Size  : in Integer_4_Unsigned_C) -- Assume 4 byte integer
  return Integer_4_Unsigned_C;
pragma Import(Stdcall, Get_Device_List, "GetRawInputDeviceList");
Assert(Get_Device_List( -- works for 32 bit applications
  List  => NULL_ADDRESS,
  Count => Number_Of_X'address,
  Size  => Record_Device_List_Element'size / Byte'size) /= -1);

1 个答案:

答案 0 :(得分:1)

这是一个非常简单的工作示例,它显示了x64 Windows上结构的大小和布局。

C:\Users\Ben\Documents\SO>type rawinputdevicelist.cpp
#include <windows.h>
#include <stdio.h>

int main(void)
{
    UINT nDevices;
    PRAWINPUTDEVICELIST pRawInputDeviceList;
    auto retval = GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST));
    auto errval = GetLastError();
    printf("sizeof (RAWINPUTDEVICELIST) is %d\n", (int)sizeof(RAWINPUTDEVICELIST));
    printf("offsetof (RAWINPUTDEVICELIST::hDevice) is %d\n", (char*)(&pRawInputDeviceList->hDevice) - (char*)pRawInputDeviceList);
    printf("sizeof (RAWINPUTDEVICELIST::hDevice) is %d\n", (int)(sizeof pRawInputDeviceList->hDevice));
    printf("offsetof (RAWINPUTDEVICELIST::dwType) is %d\n", (char*)(&pRawInputDeviceList->dwType) - (char*)pRawInputDeviceList);
    printf("sizeof (RAWINPUTDEVICELIST::dwType) is %d\n", (int)(sizeof pRawInputDeviceList->dwType));
    printf("\nGetRawInputDeviceList() returned %d\n", (int)retval);
    printf("GetLastError() returned %d\n", (int)errval);
    printf("nDevices = %u\n", (unsigned int)nDevices);
    return 0;
}

C:\Users\Ben\Documents\SO>cl rawinputdevicelist.cpp user32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30501 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

rawinputdevicelist.cpp
Microsoft (R) Incremental Linker Version 12.00.30501.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:rawinputdevicelist.exe
rawinputdevicelist.obj
user32.lib

C:\Users\Ben\Documents\SO>rawinputdevicelist
sizeof (RAWINPUTDEVICELIST) is 16
offsetof (RAWINPUTDEVICELIST::hDevice) is 0
sizeof (RAWINPUTDEVICELIST::hDevice) is 8
offsetof (RAWINPUTDEVICELIST::dwType) is 8
sizeof (RAWINPUTDEVICELIST::dwType) is 4

GetRawInputDeviceList() returned 0
GetLastError() returned 0
nDevices = 9