Driver development in windows is not difficult

Source: Internet
Author: User
/******************** This article can be forwarded, but the forwarder must retain the author's signature permission
* *** Li Hao
* *** MSN: lihao_nx@hotmail.com
****
* Email: lihaoyxj@gmail.com

* *** Source: lihaoyxj.cublog.cn

* *** Www.cnblogs.com/lihaoyxj

**************************************** ***************/
I have been writing application software for the past few years. Because the environment does not meet special requirements, I started to understand the concept of a certain driver from Win95 to 99 years in 1997, it is an existence that does not dare to think about driving this thing. during previous development, a keyboard driver was needed, so I wanted to learn and develop a keyboard driver. first, we need to build a development environment. 1. windows XP 2. vs2008 3. winxp ddk 4. http://ddkwizard.assarbad.net/download ddkwizrd 5. run ddkbuild. CMD or ddkbuild. copy BAT to winddk "ver. For example, if my name is C:" winddk "3790.1830. add the C: "winddk to executable files in the VC directory environment of vs2008. Follow these steps. You can see the project with the DDK project when creating a new project. the package I uploaded is the first time I used the above environment to drive the Hello, world version based on others' methods and some source code.

File: Driver1.rar
Size: 14kb
Download: Download
Now that you have the environment and program, you need to test it on your computer. Secondly, the debugging environment of the driver. I strongly recommend that you use Vm and snapshot for testing. the following software is also required. 1. debugview to view debugging information 2. devicetree: view the device details of the driver. 3. osrloader is a very good tool for loading and starting drivers in real time. After debugging the above project, the source code has very clear comments. 1. the program will include driver1.sys 2 in driver1 "driver1" objchk_wnet_x86 "i386. upload the three tools mentioned above and driver1.sys to XP in VM 3. use osrloader to load the generated driver1.sys and start it. Then you can use devicetree to see the helloworld driver. I just said that I want to debug the environment. Why didn't I debug it? Don't worry. Here, it is because the helloworld driver won't be accessed and debugging information will naturally not be generated. In order to access helloworld, you need to develop another program to call the driver. Because there is only one file, it is a command line program, so the source code of the source author is released # define debugmsg # include <windows. h>
# Include <winioctl. h>
# Include <stdio. h> # define device_hello_index 0x860 # define start_hellpworld ctl_code (file_device_unknown, device_hello_index, method_buffered, file_any_access)
# Define stop_hellpworld ctl_code (file_device_unknown, device_hello_index + 1, method_buffered, file_any_access) # define erron getlasterror () # define my_device_name """"". "" helloworld "# define my_device_start"-start"
# Define my_device_stop "-Stop" bool drivercontrol (tchar * maik); void usage (tchar * paramerter );
Int main (INT argc, tchar * argv [])
{
If (argc! = 2)
{
Usage (argv [0]);
Return 0;
} If (strcmpi (argv [1], my_device_start) = 0 | strcmpi (argv [1], my_device_stop) = 0)
Drivercontrol (argv [1]);
Else
{
Usage (argv [0]);
Return 0;
} Return 0;
}
Bool drivercontrol (tchar * maik)
{
Handle hdevice = NULL; // device handle
DWORD retbytes = 0; // get the device handle
Hdevice = createfile (my_device_name, generic_read | generic_write, 0, null, open_existing, file_attribute_normal, null); If (hdevice = invalid_handle_value)
{
# Ifdef debugmsg
Printf ("createfile () getlasterror reports % d" N ", erron );
# Endif
Return false;
} // Start
If (strcmpi (maik, my_device_start) = 0)
{
// Pass the started I/O control code
If (! (Deviceiocontrol (hdevice, start_hellpworld, null, 0, null, 0, & retbytes, null )))
{
# Ifdef debugmsg
Printf ("deviceiocontrol () getlasterror reports % d" N ", erron );
# Endif
Closehandle (hdevice );
Return false;
}
} // Stop
If (strcmpi (maik, my_device_stop) = 0)
{
// Pass the stopped I/O control code
If (! (Deviceiocontrol (hdevice, stop_hellpworld, null, 0, null, 0, & retbytes, null )))
{
# Ifdef debugmsg
Printf ("deviceiocontrol () getlasterror reports % d" N ", erron );
# Endif
Closehandle (hdevice );
Return false;
}
} If (hdevice)
Closehandle (hdevice); // close the handle and return true;
}
Void usage (tchar * paramerter)
{
Fprintf (stderr, "============================================== ============================================ "N"
"Driver edition Hello World" N"
"Author: dahubaobao [est]" N"
"Homepage: www.eviloctal.com or www.ringz.org" N"
"Email: [email] dahubaobao@eviloctal.com [/Email]" N"
"OICQ: 382690" N "N"
"% S-start" t start "N"
"% S-stop" t stop "N" N"
"This program is only used for code communication. Please include any errors! "N"
"============================================== ============================================ "N"
, Paramerter, paramerter );
}

Program

Calldriver-start

Calldriver-stop

Let's take a look at the message printed in the driver using dbuplint in debugview.

Because it is familiar with driver development and has referenced others' source code, please forgive me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.