Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4825995
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: WINDOWS

2010-04-24 10:27:40

I don’t use the WDK/DDK on a daily basis.  However, once in a while, I do not have access to the platform SDK and still need to use it to build a console/win32 app.  If you have never used the WDK build environment before, it can be painful and frustrating.  I always forget how to do it myself so I am going to document it here for myself and others who might find it useful.  Here it goes…

The WDK includes all of the libraries and header files needed to build everything from a simple console/GUI app to various types of Windows drivers in C/C++.  This means that you do not really need to download the Platform/Windows SDK for simple development needs.  The WDK and SDK are around 600mb and 1.3GB, respectively.  For most cases, we will be using the “sources” file to tell the WDK build environment what we want.  Basically, the “sources” file is just a plaintext file where each line is a “variable=value” format; each variable is like an option to the build tool.  In most cases, you just need to know a few of the variable names and you can build your app.

There are many target types that people want to build so I am going to try to enumerate and go through each one.  The list will be updated as I go along (or people can contribute).  The list will include these target types: console app, GUI app, DLL, and driver.

  • Building a CONSOLE APP with WDK’s build environment
  1. Create your .c file.  I am just going to do a simple “hello world” app.  I made a “hello.c” file and saved it in ”c:\blah” and its content is like thus:

    #include
    int
    __cdecl main(void)
    {
      printf("Hello World\n");
      return 0;
    }

  2. I went to the build prompt (I used “Windows XP Free Build Environment”); then change to the c:\blah folder
  3. I made a new file called “sources” in that folder and its content is like thus:


    TARGETNAME=hello   # your program name (it'll compile to "hello.exe")
    TARGETTYPE=PROGRAM # PROGRAM means that it will be a normal executable program

    # other common choices for TARGETTYPE are: DYNLINK, LIBRARY, DRIVER
    UMTYPE=console     # usermode target type CONSOLE. this is a 32bit user-mode console app

    # the other common choice for UMTYPE is: WINDOWS (gui app)
    UMENTRY=main       # default entry point shoudl be "main"

    # other choices for UMENTRY are: WINMAIN, WMAIN, ...

    USE_MSVCRT=1        # use the multithreaded runtime library
    USER_C_FLAGS = /FAs # compiler flags (/FAs = produce a .asm file as well)

    # reading the .asm file is a good way to learn assembly
    MSC_OPTIMIZATION = /Od /Oi # optimization flags (/Od = disable all optimization)

    # i normally disable all optimization for debugging purposes

    SOURCES=hello.c     # the name of the source file

    You can read more about the details of these variables at (or the WDK help file)

  4. Type “build” while inside the “c:\blah” dir and you should see the following:


    C:\blah>build
    BUILD: Compile and Link for x86
    BUILD: Loading c:\winddk\6001.18002\build.dat...
    BUILD: Computing Include file dependencies:
    BUILD: Start time: Sun Apr 19 17:54:50 2009
    BUILD: Examining c:\blah directory for files to compile.
    BUILD: Saving c:\winddk\6001.18002\build.dat...
    BUILD: Compiling and Linking c:\blah directory
    _NT_TARGET_VERSION SET TO WINXP
    Compiling - hello.c
    Linking Executable - objfre_wxp_x86\i386\hello.exe
    BUILD: Finish time: Sun Apr 19 17:55:02 2009
    BUILD: Done

    3 files compiled
    1 executable built

    C:\blah>

  5. Now your executable files are in “C:\blah\objfre_wxp_x86\i386″ (this can be different, depending on what platform you selected… here, I picked XP FREE BUILD). That’s it!

That’s all there is to it. You can just modify that template accordingly for your situation. If you have more than one source file, then just add them to the “SOURCE=” line, separated by spaces or tabs; for example, “SOURCES = hello.c file1.c file2.c”.

Now, we will move on to building a GUI APP. I am just going to make a simple app that displays a messagebox with an OK button. It will use the MessageBox function from USER32.

  • Building a GUI APP with WDK’s build environment
  1. I create a “msg.c” file in the “c:\blah” folder; the content is like thus:


    #include
    #include 

    int
    WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
      int rval;
      rval = MessageBox(NULL, TEXT("this is text"), TEXT("this is caption"), MB_OK);
      return 0;
    }

  2. Now I create the “sources” file for it; the content is like thus:


    TARGETNAME=msg
    TARGETTYPE=PROGRAM
    UMTYPE=windows #changed from "console"

    UMENTRY=winmain #changed from "main"
    USE_MSVCRT=1

    SOURCES=msg.c
    USER_C_FLAGS = /FAs
    MSC_OPTIMIZATION = /Od /Oi

  3. Now I type “build” and see that it is successful. That’s it. Note that all of the linking dependencies (USER32.LIB) are taken care for us. USER32.LIB is default for WINDOWS app; for other libraries like WS2_32.LIB, MPR.LIB, etc., we will need to explicitly specify it. I will cover that in the next section.

Now we are going to write a small app that uses WS2_32.LIB to get our hostname. The point of doing this app is to show how to tell the build environment that you need to link against some libraries. It’s simple, but I am just going to do it step by step to make sure it is clear.

  • Building a normal app that links against a library with the WDK’s build environment
  1. I am going to create a file called “getname.c” in the “c:\blah” folder as before. The content is like thus:


    #include
    #include
    #include

    int
    __cdecl main(void)
    {
    char buf[128];
    int retval;
    WSADATA wsd;
    WSAStartup(MAKEWORD(2,2), &wsd);
    retval = gethostname(buf, 128);
    printf("hostname is %s\n", buf);
    return 0;
    }

  2. The “sources” file is like thus:


    TARGETNAME=getname
    TARGETTYPE=PROGRAM
    UMTYPE=console

    UMENTRY=main
    USE_MSVCRT=1

    SOURCES=getname.c
    USER_C_FLAGS = /FAs
    MSC_OPTIMIZATION = /Od /Oi
    TARGETLIBS = $(SDK_LIB_PATH)\ws2_32.lib # the only new line...
    # if you need to link against more libraries, you can do it like this $(SDK_LIB_PATH)\blah2.lib $(SDK_LIB_PATH)\blah3.lib

  3. Now type “build” and see that it succeeds.

    What we did here was simply told “build” to link against WS2_32.LIB because gethostname() is part of that library. If you used routines in other libraries, you can add the .LIB file here as well (separated by spaces/tabs).

That’s all there is to it.

阅读(5678) | 评论(4) | 转发(0) |

chinaunix网友2010-07-26 11:09:54

In recent years, with the introduction of air max ltd, air max nike ace of running shoes in this family have given up years are named. But this change has not been authorized shoe fans, nike air max ltd generations of the market performance can only be said to be unsatisfactory. Nike finally entered in 2009 decided

chinaunix网友2010-07-26 11:09:06

As scientific study show that a person’s life, not less than 1 third are situated in a various sports such as running and play basketball.As this, wearing a comfortable pair of air max shoes to exercise is significant.The my-nikeshoes.com online store have many kinds of nike max shoes,this nike air max shoes are all high quality but low price.If you a female,you may co

chinaunix网友2010-06-01 06:25:32

MBT is based on Africa's indigenous people to create innovative walking barefoot, and wear this shoe can stimulate the body to relax the muscles so that the muscles of heat to be released, consumed calories, thus to achieve weight loss results. MBT's pursuit of beauty in order to satisfy customers, design a variety of styles, such as Mbt Chapa shoe, MBT Lami Shoe