这一部分将介绍如何构建一个native应用程序,如果你看到这个部分,请确保你已经阅读了前两部分
你需要这两者或两者之一来创建你的native应用程序:
(1)Visual C++ 2005 Express(or higher)
(2)Windows Driver Kit
我推荐这两个是因为:
(1)可以免费下载到
(2)我正在使用它们
我们将从基础开始,首先你需要创建你的应用程序的头文件,precomp.h,ntddk.h
#include "ntndk.h"
Now that that's done, create your main initialization file, which we will call init.c. In this file, add precomp.h like this:
#include "precomp.h"
And define your entrypoint:
NTSTATUS
__cdecl
main(INT argc,
PCHAR argv[],
PCHAR envp[],
ULONG DebugFlag OPTIONAL)
{
// Entry code here
}
Hopefully you are familiar with this entrypoint, it's the typical one used by C programs, except with an addon: the "DebugFlag". Right now, we don't need to care about this. We'll keep this entry simple, and turn this into a "Hello World":
NTSTATUS
__cdecl
main(INT argc,
PCHAR argv[],
PCHAR envp[],
ULONG DebugFlag OPTIONAL)
{
UNICODE_STRING HelloMsg = RTL_CONSTANT_STRING(L"Hello World!\n");
//
// Say hello
//
NtDisplayString(&HelloMsg);
}
Now, if you're wondering what NTSTATUS is, what NtDisplayString is or what RTL_CONSTANT_STRING and UNICODE_STRINGs are, then you'll need to read all the DDK documentation you can swallow, as well as Nebett's Undocumented Native API book. Although it's outdated, the information about the APIs is still pretty valid. I also plan to possibly give a fully-fledged lesson on this if lots of people are interested.
So now that we have our simple program, we need to build it.
I prefer using the WDK myself, because it's much simpler and doesn't require changing 100 of MSVC's default settings.
Assuming you've properly installed the WDK and entered the Windows build environment for your OS (from the Start Menu), you'll need to create two files in the directory where init.c and precomp.h are: sources and makefile.
Sources should look something like this:
--
TARGETNAME=native
TARGETTYPE=PROGRAM
UMTYPE=nt
INCLUDES=\
$(DDK_INC_PATH); \
$(NDK_INC_PATH);
SOURCES=init.c
PRECOMPILED_INCLUDE=precomp.h
--
Note that you'll have to set NDK_INC_PATH as an environment variable yourself, to where the NDK is installed (DDK_INC_PATH is already setup by the WDK).
Finally, you'll need a makefile:
--
INCLUDE $(NTMAKEENV)\makefile.def
--
That's all you really need for our purposes.
So now you should have init.c, precomp.h, sources and makefile.
The only thing left is to write "build", and the WDK should do the magic and create your first native application. Unforunately, you can't really test it for now, unless you do the following:
Open registry editor and browse to
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager
Edit the "BootExecute" key and write "native" instead of what's currently in it, then copy native.exe to your system32 directory and restart the computer. You should see the message appear on the screen for a little while. Make sure that you do NOT boot with /NOGUIBOOT, or else you will never see it.