全部博文(930)
分类: 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.
#include
int
__cdecl main(void)
{
printf("Hello World\n");
return 0;
}
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)
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>
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.
#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;
}
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
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.
#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;
}
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
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.
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