#include < WinUsb.h> 在 MFC 应用中产生编译器错误

#include <WinUsb.h> produces compiler error in MFC app

本文关键字:编译器 应用 错误 WinUsb lt gt #include MFC      更新时间:2023-10-16

我有一个由VS2010生成的通用SDI应用程序,我想用它来测试WinUsb API。我已经安装了当前版本的WDK。根据我在其他地方读到的帖子,我应该能够设置VS2010项目包含路径指向WDK并添加包含WinUsb.h来做到这一点。所以我将它添加到stdafx.h中,如下所示

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <Winusb.h>

但是当我编译时,我得到错误

1>c:winddk7600.16385.1incapiusb200.h(93): error C2332: 'struct' : missing tag name
1>c:winddk7600.16385.1incapiusb200.h(93): error C2011: '<unnamed-tag>' : 'enum' type redefinition
1>          c:program files (x86)microsoft sdkswindowsv7.0aincludehtmlhelp.h(331) : see declaration of '<unnamed-tag>'
1>c:winddk7600.16385.1incapiusb200.h(93): error C2059: syntax error : 'constant'
1>c:winddk7600.16385.1incapiusb200.h(93): error C2334: unexpected token(s) preceding '{'; skipping apparent function body

它所抱怨的结构是

typedef union _USB_HIGH_SPEED_MAXPACKET {
    struct _MP {
        USHORT   MaxPacket:11;  /* 0..10 */
        USHORT   HSmux:2;        /* 11..12 */
        USHORT   Reserved:3;    /* 13..15 */
    };
    USHORT us;
  } USB_HIGH_SPEED_MAXPACKET, *PUSB_HIGH_SPEED_MAXPACKET;

, IDE用红色下划线标注_MP

问题的根源在于当应用被配置为使用MBCS时。在选择MBCS时包含的mbctype.h内部是一组#define语句

/* bit masks for MBCS character types */
#define _MS     0x01    /* MBCS single-byte symbol */
#define _MP     0x02    /* MBCS punct */
#define _M1     0x04    /* MBCS 1st (lead) byte */
#define _M2     0x08    /* MBCS 2nd byte*/
#define _SBUP   0x10    /* SBCS upper char */
#define _SBLOW  0x20    /* SBCS lower char */

发生在usb200.h之前。您可以不选择MBCS(通过使用Unicode),也可以按照Lynn所说的做,并在上面一行中对其进行#undef。但是,将#include放在列表的底部,以避免意外的后果。

您需要添加:

#undef _MP

在包含Winusb.h之前。

我相信这是一个MFC特定的问题,MFC将其定义为预处理器符号,唯一的解决方法是明确地不定义它,以便正确地写出结构体。据推测,这有一些其他的副作用,所以请谨慎行事。

实际上,我的错误不是#包括在WinUsb.h之前

我做了一个错误的假设,MFC头文件将最终包括Windows.h,但由于这是一个设备相关的应用程序显然没有。