Sunday, October 12, 2008

C2061, a common compiler error with ntddk

This is a MS Windows application. A USB Camcorder with a USB port. Identify this device when the camcorder linked to computer and list the special directory's files.

I downloaded the Win2K3 DDK and want to use the "STORAGE_DEVICE_DESCRIPTOR" that defined in the file "ntddsdk.h".

I use the WTL library for Windows message driven framework. I try to use ::DeviceIoControl to query the device's properties, this Win32 function takes 2 struct STORAGE_DEVICE_DESCRIPTOR and STORAGE_PROPERTY_QUERY, they are defined in the ddk include file
"ntddsdk.h". When I compile this cpp file, a compile error C2061 come out and tell me that has a syntax error arround the 2 struct declaration. I can not believe, because the defination is really existed in the header file!

Then, after "googled" for a long time, actually 2-3 hours later, I found a lot of people suffered this compiler error, but no one gave any useful suggestions. I gave up the search engine and try to drill into the header files.

I found the standard Platform SDK header include many symbols that define in the , there is a part copy of
"ntddsdk.h" but exclude some DDK special symbols just like STORAGE_DEVICE_DESCRIPTOR etc. The use the macro define to avoid the symbols duplicate. The macro "_NTDDSTOR_H_" is the problem. I found the "winioctl.h" always included first. The "windows.h", line 198, #include "winscard.h", in this winscard.h line 31 is the "winioctl.h". The "winioctl.h" defines the "_NTDDSTOR_H_", so when I try to include the "ntddstor.h", the macro had been defined, the all symbols in the "ntddstor.h" will not be included in again!

Because the DDK symbols is defined in this file and not included in the "winioctl.h", I have to modify the stdafx.h to tell the compiler donot include the "winscard.h". I added #define NOCRYPT before the #include "atlbase.h". This macro will exclude the "wincrypt.h", "winefs.h" and "winscard.h". And the "ntddstor.h" also depends on some symbols from the "devioctl.h", this is another ddk header file. So the cpp file should include
"devioctl.h" first and then include "ntddstor.h". OK! It's work now.

No comments: