【问题标题】:Cannot found any data type used by SendInput() function找不到 SendInput() 函数使用的任何数据类型
【发布时间】:2018-04-24 06:50:28
【问题描述】:

我正在尝试使用SendInput() 函数。我写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>

#define WIN32_LEAN_AND_MEAN

//...

    KEYBDINPUT kbi;
    kbi.wVk = 0x31;
    kbi.wScan = 0;
    kbi.dwFlags = 0;
    kbi.time = 0;

    INPUT input;
    input.type = INPUT_KEYBOARD;
    input.ki = kbi;

    SendInput(1, &input, sizeof input);

编译:

gcc -Wall -o window.exe win32.c -lWs2_32

我明白了:

win32.c: In function ‘main’:
win32.c:13:2: error: ‘KEYBDINPUT’ undeclared (first use in this function)
win32.c:13:2: note: each undeclared identifier is reported only once for each function it appears in
win32.c:13:13: error: expected ‘;’ before ‘kbi’
win32.c:14:2: error: ‘kbi’ undeclared (first use in this function)
win32.c:20:2: error: ‘INPUT’ undeclared (first use in this function)
win32.c:20:8: error: expected ‘;’ before ‘input’
win32.c:21:2: error: ‘input’ undeclared (first use in this function)
win32.c:21:15: error: ‘INPUT_KEYBOARD’ undeclared (first use in this function)

我不知道如何解决这个问题。根据documentation,它在Winuser.h 标头中声明。但对我不起作用。

【问题讨论】:

  • 您可以检查头文件是否已在某个宏下声明了这些结构,在这种情况下,您必须使用 -D 选项通过命令行传递该宏定义或在源代码中显式定义它

标签: c winapi sendinput


【解决方案1】:
#define _WIN32_WINNT 0x0403
#include <windows.h>

似乎这是您在项目某处需要的魔法#define(在代码中明确显示,或通过编译器命令行参数 -D)。

请注意,windows.h 包含 winuser.h,因此无需包含它,因为它已经为您包含在内。此外,WIN32_LEAN_AND_MEAN 定义仅在包含 before 窗口时才有效。有关它的作用的详细信息here;这些天它不需要或特别有用。

--

那么这里发生了什么?在winuser.h(C:\Cygwin\usr\include\w32api\winuser.h)中寻找KBDINPUT的定义,我们看到:

#if (_WIN32_WINNT >= 0x0403)
typedef struct tagMOUSEINPUT {
...
} MOUSEINPUT,*PMOUSEINPUT;
typedef struct tagKEYBDINPUT {
...

这就是问题所在;这些只有在 _WIN32_WINNT 大于 0x0403 时才会被定义。

这些是 cygwin 包中的文件。有趣的是,来自 Microsoft SDK(通常安装在 C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\WinUser.h)中的 winuser.h 使用了不同的条件:

#if (_WIN32_WINNT > 0x0400)

...这解释了 Jay 的建议 - 他可能正在查看 MS 文件,这里 0x0401 就足够了;并且还解释了为什么它不适合你——你可能使用的是具有更高版本要求的 cygwin。至于为什么这两个文件不同 - 我不知道...

【讨论】:

    【解决方案2】:

    我猜你需要添加

    #define _WIN32_WINNT 0x0401
    #include <windows.h>
    #include <winuser.h>
    

    在您的源代码中包含 windowssh 和 winuser.h 之前。

    【讨论】:

      【解决方案3】:

      这是 VC6 等旧 IDE 的问题,我尝试了上述方法,但没有成功。我必须在项目设置中提供标志。

      转到设置 >> C/C++ 选项卡 >> 从类别组合框中选择“常规” 将/D _WIN32_WINNT=0x401 添加到项目设置编辑框。这是 VC6 的。

      /d 是您提供标志的方式,实际标志是_WIN32_WINNT=0x401。我必须将其设置为 0x401,其他值(如 0x0500)会导致更多错误。

      【讨论】:

        猜你喜欢
        • 2014-04-20
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-15
        • 2010-12-12
        相关资源
        最近更新 更多