2

I took this code as example to write a service. And I made some changes in my main function in such a way to work with command line parameters and removed

#define UNICODE
#define WINVER 0x502 

Am using "MINGW".

Am getting the following errors:

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)

If I uncomment the unicode and winver there are no errors, but command line parameters are not working.. I included dbt.h too..

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2vision2
  • 4,933
  • 16
  • 83
  • 164

1 Answers1

2

The DEV_BROADCAST_DEVICEINTERFACE structure is only supported on Windows XP and later (as well as some of the other APIs that this code relies upon). It won't be defined in the Windows headers unless you're targeting that version of Windows or later.

To make sure that it's defined, you need to explicitly specify your target version of Windows at the top of your header file before you include Windows.h.

The typical pattern looks something like this:

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

The original version of the code you tried had this line, which you removed:

#define WINVER 0x502

That explicitly set the target Windows version to Windows Server 2003 (Windows NT v5.2). Removing it means that you revert to the lowest common denominator, which is a version of Windows prior to XP, where the DEV_BROADCAST_DEVICEINTERFACE structure is not defined.

It's also not clear why you're removing the UNICODE define. It's 2012—any app you're building should be targeting Unicode. Leave that defined as well.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • thanks....for win7 do I need to define a seperate winver? or is there any other common definition available for all the versions from xp. – 2vision2 May 18 '12 at 11:49
  • but if i define unicode i cant use command line parameters in main... might be sounds silly am sorry am a newbie for this... – 2vision2 May 18 '12 at 11:56
  • http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c this links clears my doubts... thanks stack overflow... really gr8... – 2vision2 May 18 '12 at 12:07
  • Dear Cody, I read the above said link and use _tmain( int argc, TCHAR* argv[]). but still my command line arguments are not detected!!!! – 2vision2 May 18 '12 at 12:25
  • Yes, if you're building a Unicode application, you need to use wide strings. You can either use the `wchar_t` type explicitly, or you can use the `TCHAR` macro to handle it for you. Same for entry points like `main` and `_tmain`. The `_tmain` version is a macro that automatically resolves to the correct symbol, depending on the definition of `UNICODE`. If you're not familiar with this behavior, you're going to have trouble writing Windows code. Perhaps you should pick up a copy of a good Win32 programming guide? I can't tell you what's wrong with code that you haven't posted. @user – Cody Gray - on strike May 18 '12 at 12:27