2014年6月29日 星期日

Windows Driver 2 _ HelloWorld Driver

It's time to write my first driver "HelloWorld"
I write this code in VS2013 and here's my steps

A. Create a new project
    (1) [File] menu->[New]->[Project], in the left pane, select [Installed]->[WDF], in the middle pane, select [Kernel Mode Driver, Empty(KMDF)]. Fill in "HelloWorld" in the name field and choose the project's location in the location field. Check out Create directory for solution. Finally click OK ! 
(Note that the name field can only be equal or less than 32 characters string, mentioned in Microsoft Official Doc)


    (2) In the Solution Explorer(方案總管), right click [HelloWorld]->add->new Item(新增項目). In the middle pane, select C++ File, and name "main.c" in the name field, then click add. Finally, you will see main.c under the Source Files
(Note that the file name extension is .c !!)



Here's the code:
-------------------------------------------------------------------------------------------------------
#include <ntddk.h>  

VOID DriverUnload(PDRIVER_OBJECT driver)
{
DbgPrint("first:HelloWorld End!");
}

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pUnicodeString)
{
DbgPrint("first:HelloWorld Begin!");
pDriverObject->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}
-------------------------------------------------------------------------------------------------------

     (3) Right click Solution'HelloWorld'(2 Solutions)(方案'HelloWorld')-> configuration manager(組態管理員). Choose configuration and platform both HelloWorld and HelloWorld Package


     (4) Cancel Wpp Tracing
Solution Explorer->right click HelloWorld -> Properties -> set Run Wpp tracing to No -> OK


      (5) also in Properties->C/C++ -> middle pane 警告視為錯誤->否

    (6) Build project
Choose Build Solution->Build to build driver and create a driver package, the ouput window will show the build progress.


      (7) Navigate in Windows Explorer to your HelloWorld folder(..\Visual Studio 2013\Projects\HelloWorld), then go into win7Debug\HelloWorld Package folder, there will have several files:
HelloWorld.sys : the kernel mode driver file
HelloWorld.inf : an information file that Windows uses when driver is installed
HelloWorld.cat : a catalog file that the installer uses to verify the test signature for the driver package
WdfCoinstaller01011.dll : A co-installer for the Windows Driver Frameworks(WDF)


B. After building your project, it's time to test it on target computer.
Before testing your driver, you must copy your [HelloWorld Package]folder into your target computer.
Normally, you will have 4 files in your HelloWorld folder that I mentioned in last article.

     (1) Let's start from setting the DebugView 
          Run -> regedit -> open key :[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager] -> create a key [Debug Print Filter] under this folder -> add a DEFAULT DWORD value and set a value 0x0f on it -> reboot

     (2) open DebugView and check out [capture]->[capture kernel]

     (3) open osrloader and select the sys file you want to load. Then click [Register Service] and [Start Service] then you will see "first:HelloWorld Begin" in DebugView.
Click [Stop Service] then you will see "first:HelloWorld End". If you want to remove the driver, click [Unregister Service].






     P.s You don't need to install the osrloader to load the driver, just find out your inf file, and right click on it, click [install]. However, it's up to you to use osrloader or not.
Another way to load your driver is using command, just entering sc create HelloWorld binPath= "your_sys_file's_location" type= "kernel" start= "demand" error= "normal" Displayname= "HelloWorld", press Enter
then entering sc start HelloWorld


     If you use remote debug, you will see your string at Debugger Immediate Window in VS2013 

[Reference]
1. http://home.educities.edu.tw/fushiyun2000/driver_nt_style_c_tutorial_hello_world.htm
2. http://eagle-sw.blogspot.tw/2011/10/win7debugview.html
3. http://blogs.microsoft.co.il/sasha/2011/06/04/baby-steps-in-windows-device-driver-development-part-2-hello-world-driver/
4. http://blog.csdn.net/wsye88/article/details/26375459

沒有留言:

張貼留言

全國推廣動物認領養平台串聯貼紙

全國推廣動物認領養平台串聯貼紙