ZZ: VS2005 + DDK6001 环境搭配及编译全过程 (编译错误分析)

来源:互联网 发布:生产数据统计岗位职责 编辑:程序博客网 时间:2024/05/16 23:35

一.DDKWirzard+VS2005开发驱动环境配置

DDKWirzard是Oliver Schneider写的一个驱动开发辅助插件,支持windows2000 XP 2003,VS.net 2003 VS2005等。前提是你安装了VS和DDK或IFS
具体使用方法:
1.下载DDKWirzard,网址http://ddkwizard.assarbad.net/
ddkwizard_setup.exe
ddkbuild_bat.zip
这两个文件都要下,如果你的英语还可以的话就可以直接看用户手册ddkwiz_manual.pdf而不用往下看了。
2.将ddkbuild_bat.zip解压缩,将ddkbuild.bat放到随便一个路径下,这里假设是C:/winddk/。
3.VS2005-Tools-Options-Projects and Solutions-VC++ Directories-新建一个C:/winddk/
4.右键我的电脑-属性-高级-环境变量-添加
  变量名:WXPBASE
  变量值:C:/WINDDK/3790.1830
  变量值根据自己的情况修改。变量名情况如下:
  Windows 2000 DDK:W2KBASE
  Windows XP DDK:WXPBASE
  Windows 2003 Server DDK:WNETBASE
  Windows Longhorn/Vista DDK:WLHBASE
5.设置完毕就可以打开VS2005了。新建-项目-可以看到有了个DDK project,选择Driver,根据选择选定需要的DDK。Finish
6.项目属性-NMAKE-Included Search Path-添加C:/WINDDK/3790.1830/inc/ddk/wxp 这里根据自己需求选择
7.OK,F7吧。进入项目目录就能看到生成的objchk_wnet_x86目录以及里面的.sys了。

 

二.使用VS2005 来编译驱动的客户端

我们要做一个窗体程序来和驱动交互,所以要使用DDK里面的库和头文件,使用VS的编译器来编译。

1.tools->options->include dir 里面设置6001/inc/api 和 6001/inc/ddk (注意,api这个目录一定要在ddk目录的前面)

/否则要出错:e:/winddk/6001/inc/ddk/fltuserstructures.h(23) : fatal error C1012: unmatched parenthesis : missing ')'

2.定义NTDDI_VERSION NTDDI_WINXPSP2
  #define NTDDI_VERSION NTDDI_WINXPSP2

 

--------------------------------------------------------------------------------------------------------------------------------------------------

之所以出错的原因,一下这封信写得很清楚:

Hi, many thanks for this. Here's the outcome of my further investigations, 
indicating how I finally got everything to build in Visual Studio! 

As you pointed out - since OSVER, SPVER and NTDDI_VERSION survived 
preprocessing, it indicated that these are all undefined – and that _was_ the 
primary reason behind my problem. 

So I did a search for NTDDI_VERSION on Google, and found the following 
interesting PowerPoint presentation and Word document. 

WDK Build Environment Refactoring: 
http://download.microsoft.com/download/f/0/5/f05a42ce-575b-4c60-82d6-208d3754b2d6/WDK_BE-Refactoring.ppt 

Header File Changes in the Windows Driver Kit: 
http://download.microsoft.com/download/5/D/6/5D6EAF2B-7DDF-476B-93DC-7CF0072878E6/headers.doc 

These describe the changes between the old DDK and the WDK, including the 
header file reorganisation that has taken place and led to my build problems. 

In the release notes for version 5112 WDK, there’s the following brief 
mention of the changes: 
Header Versioning Activated 
This release of the WDK implements a versioned header system. The overall 
effect of this new system is that the WDK no longer contains operating 
system-specific include directories under /inc. The WDK build environments 
are configured for this change, but customized build environments that have 
hard-coded include paths might break. 

Workaround: Use the WDK build environments, or update customized build 
environments appropriately. 

>From the above 2 URLs, I found the following essential information: 

 

1) The WDK build environment defines the constant NTDDI_VERSION to match the 
driver’s build environment. 
- Since various projects in Tony’s updated Application Manager solution use 
the DDK, and I’m building them in Microsoft Visual Studio .NET, this is the 
crux of the problem. If I was to build all the projects using the WDK build 
environment, in theory, this build problem shouldn’t exist. 

2) In order to still be able to build in Visual Studio, the solution is to 
define NTDDI_VERSION with a target operating system value 

Table 1: Windows Operating System Version Constants 
Constant Version of operating system 
NTDDI_WIN2K Windows 2000 
NTDDI_WINXP Windows XP 
NTDDI_WS03 Windows Server 2003 
NTDDI_LONGHORN Windows Longhorn 

(declared in the new sdkddkver.h header file) 


DO be sure that you build your drivers within the build environment 
provided by the DDK, or that you define the necessary environment variables 
to ensure that NTDDI_VERSION is correctly defined. 


Taking into account this information, as a test, I added this line at the 
top of RulesInfo.cpp 

#define NTDDI_VERSION NTDDI_WINXP 

The resulting RulesInfo.i file then contained: 
(OSVER(NTDDI_WINXP) == NTDDI_WIN2K && SPVER(NTDDI_WINXP) >= 
5) || (OSVER(NTDDI_WINXP) == NTDDI_WINXP && SPVER(NTDDI_WINXP) >=2) || 
(OSVER(NTDDI_WINXP) == NTDDI_WS03 && SPVER(NTDDI_WINXP) >=1) || 
(OSVER(NTDDI_WINXP) >= NTDDI_WINLH) 

Indicating that OSVER and SPVER were still undefined. 

I found that these are defined in the new “sdkddkver.h” header file, and 
that is #included in the DDK version of “windows.h”, found at: 
C:/WINDDK/5112/inc/api 

So, after all that, the main thing I needed to do was to ensure that this 
include file directory is listed at the top of the Include Directories in 
Visual Studio’s Tools -> Options… -> Projects -> VC++ Directories settings. 

It then turned out that NTDDI_VERSION gets #defined in “sdkddkver.h” too, so 
#define'ing it wasn’t necessary in my source file. 

After this primary reason for my build problem, I then had another 3 - 
here's how I resolved them: 

1) Even though everything in the project compiled, no .obj files were being 
created, and it couldn’t link. 

- It turns out that that having the “C/C++ -> Preprocessor -> Generate 
Processed File“ project setting set to “With Line Numbers (/P)” caused no 
..obj files to be created!?! Turning it off fixed the problem. 

2) There was then a problem with another project in the solution against the 
WDK: 


c:/WINDDK/5112/inc/api/usb200.h(85) : error C2332: 'struct' : missing tag name 
c:/WINDDK/5112/inc/api/usb200.h(85) : error C2011: '__unnamed' : 'enum' type 
redefinition 
C:/WINDDK/5112/inc/api/shlwapi.h(1482) : see declaration of 
'__unnamed' 
c:/WINDDK/5112/inc/api/usb200.h(85) : error C2059: syntax error : 'constant' 
c:/WINDDK/5112/inc/api/usb200.h(85) : error C2334: unexpected token(s) 
preceding '{'; skipping apparent function body 

In usb200.h, the following was defined: 
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; 

But this caused a problem because _MP is already a #defined constant in 
mbctype.h: 
/* bit masks for MBCS character types */ 
#define _MS 0x01 /* MBCS single-byte symbol */ 
#define _MP 0x02 /* MBCS punct */ 
…. 

So my inelegant fix for this to put #undef _MP above the #include 
<afxtempl.h> line in the project's stdafx.h file. 

3) A simple one – the PUSB_NODE_CONNECTION_INFORMATION_EX used in this 
project is defined within “#if (_WIN32_WINNT >= 0x0501)” in 
C:/WINDDK/5112/inc/api/usbioctl.h 
It wasn't inside a #ifdef in the previous DDK version. 

So I simply updated the _WIN32_WINNT #define in the project's stdafx.h file 
as follows: 

#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to 
target Windows 2000 or later. 


Thanks for all your help, I hope my post helps others who may encounter a 
similar problem when building projects written against an older DDK, and they 
then upgrade from DDK to Windows Driver Kit (WDK) for Windows Longhorn. 

 

zz from : http://blog.csdn.net/Zealot_Chen/archive/2008/11/19/3336084.aspx