Alter.Org.UA
 << Back Home UK uk   Donate Donate

Building NT kernel mode drivers in MS Visual Studio 8.0 (VS 2005)

Let's continue. I'm publishing detailed step-by-step instruction for creating NT kernel-mode driver project in MS Visual Studio 8.0

Contents

  • set environment variables
  • create project
  • adding files.
    Note: add files before configuring project. Othervise VS may not show some important options.
  • change project settings
  • sample:
    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html//../../..//docs/nt_kernel/vc6_proj/driver_template_v7.rar in /usr/home/alter/html/common.inc.php on line 524

    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html//../../..//docs/nt_kernel/vc6_proj/driver_template_v7.tgz in /usr/home/alter/html/common.inc.php on line 525
    driver_template_v7.rar/tgz ( bytes/ bytes) in pch_cpp subdirectory

Set environment variables

Set BASEDIRNT4, BASEDIR2K, BASEDIRXP, BASEDIR2K3 environment variables (I would recommend system ones, not user-specific) like this:

BASEDIRNT4 = C:\Develop\DDKNT4
BASEDIR2K  = C:\Develop\DDK2000
BASEDIRXP  = C:\Develop\DDKXP
BASEDIR2K3 = C:\Develop\DDK2003

Set BASEDIR environment variable to point to your favorite DDK (I prefer NT4, because I'm writing highly compatible drivers). But latest time DDK 2003 is oftenly used. So we shall tune template project for DDK 2003. See migrating VC6 driver project to VC8 with DDK 2003 for DDK 2003 specific details.

BASEDIR = %BASEDIR2K3%

Create project

  • File -> New... -> Project
  • Chose Visual C++ -> Win32 in Project Types tree.
  • Chose Win32 Project in Templates list.
  • Enter project name, directory path and solution name. As usually. You can also uncheck Create directory for solution box. Then both project file (.VCPROJ) and .SLN will be created in same directory. By default Visual Studio 8 creates separate subdirectory for project.
  • OK -> DLL, Empty project -> Finish

Adding files

  • Just add files to your project. Project -> Add Existing Item or Project -> Add Item
  • If you decided to use .PCH, do not forget to insert the following line in all added *.C and *.CPP files
    #include "stdafx.h"
    
    instead of
    #include <ntddk.h>
    .....
    

Change project settings

  • In order to have access to all necessary options make sure that your project has at least 1 .C/.CPP file. A would recommend to use .PCH and initially add the following files
    driver_template.cpp
    drv_common.h
    StdAfx.cpp
    StdAfx.h
    
    All these нou can find in samples:
    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.rar in /usr/home/alter/html/common.inc.php on line 524

    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.tgz in /usr/home/alter/html/common.inc.php on line 525
    driver_template_v7.rar/tgz ( bytes/ bytes)
  • Project -> Properties
  • Chose "All Configurations" in "Settings for" combo (top-left corner)
  • Configuration Properties -> General
    • It worth replacing $(SolutionDir) for $(ProjectDir) in "Output Directory".
    • "Character Set" = Not Set
    • "Whole Program Optimization" = No Whole Program Optimization
  • Configuration Properties -> C/C++
    • -> General
      • Enter paths to DDK INCLUDE directories into "Additional include directories" edit:
        $(BASEDIR2K3)\inc\ddk\wxp,$(BASEDIR2K3)\inc\ifs\wxp,$(BASEDIR2K3)\inc\wxp,
            $(BASEDIR2K3)\inc\crt
        
        Note 1: here the separator character is comma. Do not take paths in quotation marks.
      • "Debug Information Format" = "Program Database (/Zi)".
      • "Detect 64-bit Portability Issues" - as you like. Affects additional compile-time checks for operations with pointers.
    • -> Optimization
      • "Whole Program Optimization" = No
    • -> Preprocessor
      • Enter the following predefined constants into "Preprocessor definitions" edit (thanks to Emusic from http://www.rsdn.ru). This will make all .H being compiled properly:
        WIN32, _WINDOWS, i386=1, _X86_=1, STD_CALL, CONDITION_HANDLING=1,
        NT_INST=0, _NT1X_=100, WINNT=1,
        _WIN32_WINNT=0x0400 /* minimum required OS version */
        WIN32_LEAN_AND_MEAN=1, DEVL=1, FPO=1
        
        Add the following defines to "Win32 Debug" configuration:
        DBG, DEBUG, _DEBUG
        
        Note 1: here the separator character is comma. Do not take items in quotation marks.
        Note 2: If you use precompiled headers, you can place these defines to your .H used for building .PCH or some common .H file. Look for example with *.cpp files in pch_cpp folder and with *.c files in pch_c inside
        Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.rar in /usr/home/alter/html/common.inc.php on line 524

        Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.tgz in /usr/home/alter/html/common.inc.php on line 525
        driver_template_v7.rar/tgz ( bytes/ bytes) archive.
    • -> Code Generation
      • "Enable String Pooling" = "Yes (/GF)"
      • "Enable C++ Exceptions" = "Yes (/EHsc)"
      • "Basic Runtime Checks" = "Default"
      • "Struct member alignment" = "8 Bytes (/Zp8)"
      • "Buffer Security Check" = "No (/GS-)"
    • -> Language
      • "Enable Run-Time Type Info" = "No (/GR-)"
    • -> Precompiled Headers
      • "Create/Use Precompiled Header" = "Create Precompiled Header (/Yc)"
      • "Create/Use PCH Through File" = "StdAfx.h"
    • -> Advanced
      • "Calling convention" = "__stdcall (/Gz)"
    • -> Command Line Remove "/GZ" from "Win32 Debug" configuration. In most cases this will save us from such trouble as "Debug build works, but Release doesn't". Builds will not differ in bug reproducing (uninitialized variables and memory blocks, overflows, etc.). In addition, this pretty switch instructs compiler to include stack consistency check code into output. This check leads to unresolved external _chkesp on link stage.
  • Configuration Properties -> Linker
    • "General"
      • Enter driver binary'es name (e.g. "driver_template.sys") in "Output File" edit.
      • "Enable Incremental Linking" = "No (/INCREMENTAL:NO)"
      • "Ignore Import Library" = "Yes"
      • Enter paths to DDK libraries in "Additional Library Directories". For example
        $(BASEDIR2K3)\lib\wxp\i386,
        
      • Press button [...] right to "Additional Library Directories" and uncheck "Inherit from parent or project defaults" box.
    • "Input"
      • Set list of import libraries for DDK libraires in "Additional Dependencies":
        ntoskrnl.lib
        int64.lib
        Hal.lib
        
        Note: here the separator character is space character. Do not take paths in quotation marks.
      • Press button [...] right to "Additional Dependencies" and uncheck "Inherit from parent or project defaults" box.
      • "Ignore all default libraries" = "Yes (/NODEFAULTLIB)"
    • "Debugging"
      • "Generate Debug Info" = "Yes (/DEBUG)"
    • "System"
      • "SubSystem" = "Native (/SUBSYSTEM:NATIVE)"
      • "Driver" = "Driver (/DRIVER)"
    • "Advanced"
      • "Entry point symbol" = "DriverEntry"
    • -> Command Line
      • Separately for each configuration add "/safeseh:no" switch.
      • Separately for each configuration change "/subsystem:native" for "/subsystem:native,4.00" if you need older OS support.
  • OK
  • File -> Save All
  • And now copy this empty project somewhere for future use as template for your driver projects :) For example:
    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.rar in /usr/home/alter/html/common.inc.php on line 524

    Warning: filesize() [function.filesize]: stat failed for /usr/home/alter/html/../../..//docs/nt_kernel/vc6_proj/driver_template_v7.tgz in /usr/home/alter/html/common.inc.php on line 525
    driver_template_v7.rar/tgz ( bytes/ bytes)

2006.12.07

See also:

<< Back designed by Alter aka Alexander A. Telyatnikov powered by Apache+PHP under FBSD © 2002-2024