Writing drivers for different versions of Windows

Source: Internet
Author: User

MSDN Original: https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554887 (v=vs.85). aspx

When you create a driver project, you specify a basic target operating system that is the basic version of Windows that is running the driver. For example, you can specify Windows 7 as the base target operating system. In this scenario, the driver runs on Windows 7 and later versions of Windows.

NoteIf you are developing a driver for a specific basic version of Windows and you want the driver to run on a later version of Windows, you must not use any of the functions that are not logged, or use the recorded functions in any way other than the methods described in the documentation. Otherwise, the driver may not be able to run on a later version of Windows. Even if you are careful to use only recorded functions, you should test the driver on each new version of Windows when it is released. Write a multi-version driver that uses only common features

The simplest way to design a driver that will run on multiple versions of Windows is to allow the driver to use only Windows common DDI functions and structs that run all versions of the driver. In this case, set the most basic target operating system to the earliest version of Windows that the driver supports.

For example, to support all versions of Windows starting with Windows 7, you should do the following:

    1. Design and implement a driver so that the driver uses only those features that are available in Windows 7.

    2. When you build the driver, you designate Windows 7 as the most basic target operating system.

Although this process is simple, it may restrict the driver to use only a subset of the features available on a later version of Windows.

Write a multi-version driver that uses version-related features

The kernel-mode driver can dynamically determine which version of Windows it is running and choose to use the features provided on that version. For example, you must support Windows 7-based drivers for all versions of Windows that can determine the version of Windows that it is running at run time. If the driver is running on Windows 7, it can only use DDI functions that are supported by Windows 7. However, the same driver can use other DDI functions that apply only to Windows 8, for example, when its run-time check feature determines that it is running on Windows 8.

Determine the Windows version

rtlisntddiversionavailable is a function that the driver can use at run time to determine whether a particular version of Windows provides functionality that is available. The prototype of the function is as follows:

BOOLEAN rtlisntddiversionavailable (in ULONG Version)

In this prototype,version is a value that indicates the desired version of Windows DDI. This value must be one of the DDI version constants, defined in Sdkddkver.h, such as Ntddi_win8 or ntddi_win7.

rtlisntddiversionavailable returns TRUE when the calling program runs on version . specified on the same version of Windows or later.

The driver can also check a specific Service Pack by calling the rtlisservicepackversioninstalled function. The prototype of the function is as follows:

BOOLEAN rtlisservicepackversioninstalled (in ULONG Version)

In this prototype,version is a value that indicates the desired version of Windows and Service Pack. This value must be one of the DDI version constants, defined in Sdkddkver.h, such as NTDDI_WS08SP3.

Note thatrtlisservicepackversioninstalled returns TRUE only if the operating system version exactly matches the specified version. Therefore, if the driver is not running on Windows Server SP4, the call is set to Version ntddi_ws08sp3 rtlisservicepackversioninstalled< /c6> will fail.

Conditional calls to Windows version-related functions

After the driver determines the specific operating system version available on the computer, the driver can use the mmgetsystemroutineaddress function to dynamically find the routine and invoke the routine through the pointer. This function is available on Windows 7 and later versions of the operating system.

NoteTo help keep typing checks and prevent unintended errors, you should create a typedef that maps the original function type. Example: Determining the version of Windows and conditionally invoking version-related functions

This code example (the header file from the driver) defines the PAISQSL type as a pointer to the keacquireinstackqueuedspinlock function. The example then declares a variable of this type AcquireInStackQueuedSpinLock .

This code example (the driver's initialization code) determines whether the driver is running on a Windows 7 or later operating system. If it is, the code retrieves a pointer to Keacquireinstackqueuedspinlock .

...////is we running on Windows 7 or later?//if (rtlisntddiversionavailable (ntddi_win7)) {/////  Yes ... Windows 7 or later it is!     rtlinitunicodestring (&funcname,                  L "Keacquireinstackqueuedspinlock");  //Get A pointer to Windows implementation  //for keacquireinstackqueuedspinlock into our  //variable "Acqui reinstackqueued "     acquireinstackqueued = (PAISQSL)                  mmgetsystemroutineaddress (&funcname);} ...// Acquire a spin lock.  if (NULL! = acquireinstackqueued) {  (acquireinstackqueued) (&spinlock, &lockhandle);} else {    

In this example, the driver calls rtlisntddiversionavailable to determine whether the driver is running on Windows 7 or later. If the version is Windows 7 or later, the driver will call mmgetsystemroutineaddress to get a pointer to Keacquireinstackqueuedspinlock A pointer to the function and stores the pointer in a AcquireInStackQueued variable named PAISQSL, which is declared as a type.

Then, when the driver must acquire a rotation lock, it checks to see if it receives a pointer to the keacquireinstackqueuedspinlock function. If the driver has received this pointer, the driver uses the pointer to call Keacquireinstackqueuedspinlock. If the pointer to Keacquireinstackqueuedspinlock is Null, the driver uses Keacquirespinlock to obtain the rotation lock.

Writing drivers for different versions of Windows

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.