2

我以这段代码 为例来编写一个服务。我对我的主要功能进行了一些更改,以便使用命令行参数并删除

#define UNICODE
#define WINVER 0x502 

我正在使用“MINGW”。

我收到以下错误:

usb_detect.c: In function 'ServiceMain':
usb_detect.c:123:16: error: unknown type name 'DEV_BROADCAST_DEVICEINTERFACE'
usb_detect.c:132:41: error: request for member 'dbcc_size' in something not a structure or union
usb_detect.c:132:61: error: 'DEV_BROADCAST_DEVICEINTERFACE' undeclared (first use in this function)
usb_detect.c:132:61: note: each undeclared identifier is reported only once for each function it appears in
usb_detect.c:133:41: error: request for member 'dbcc_devicetype' in something not a structure or union
usb_detect.c:133:60: error: 'DBT_DEVTYP_DEVICEINTERFACE' undeclared (first use in this function)
usb_detect.c:136:117: error: 'DEVICE_NOTIFY_SERVICE_HANDLE' undeclared (first use in this function)
usb_detect.c:136:148: error: 'DEVICE_NOTIFY_ALL_INTERFACE_CLASSES' undeclared (first use in this function)

如果我取消注释 unicode 和 winver,则没有错误,但命令行参数不起作用..我也包括 dbt.h..

4

1 回答 1

2

DEV_BROADCAST_DEVICEINTERFACE结构仅在 Windows XP 及更高版本(以及此代码所依赖的一些其他 API)上受支持。它不会在 Windows 标头中定义,除非您针对的是该版本的 Windows 或更高版本。

为确保已定义它,您需要包含Windows.h.

典型的模式看起来像这样:

#include <WinSDKVer.h>
#define _WIN32_WINNT    _WIN32_WINNT_WINXP
#include <SDKDDKVer.h>

您尝试的代码的原始版本有这一行,您将其删除:

#define WINVER 0x502

这将目标 Windows 版本明确设置为 Windows Server 2003 (Windows NT v5.2)。删除它意味着您恢复到最低公分母,即 XP 之前的 Windows 版本,其中DEV_BROADCAST_DEVICEINTERFACE未定义结构。

也不清楚你为什么要删除UNICODE定义。现在是 2012 年——你正在构建的任何应用程序都应该以 Unicode 为目标。保留定义为好。

于 2012-05-18T11:39:44.110 回答