The Windows Driver calls the function exported from the Assembly file.

Source: Internet
Author: User
The Windows Driver calls the function exported from the Assembly file.

This document describes how to call the Assembly functions exported from the ASM file in the C file of the driver.

Introduction

    The windows driver uses ddk1_ifsddk(compile and link the source file with the build.exe program in the simple name ddk1_. during the operation, you only need to start the corresponding DDK command line program, enter the source file directory to be compiled, and run the build command to obtain the. SYS file.

    A nmakeprogram is used on the buildcommand. cl.exeand link.exe are used as the nmakeprogram and all compilation and link options are specified. Different from the application, programmers cannot modify the compilation link options when using the build command, therefore, the build command cannot intervene in the compilation or link process.

    The 64-bit platform no longer supports embedded assembly code. To enable the driver to still use the assembly language, the usual practice is to write the assembly code into an independent function and put it into a separate Assembly file, compile the Assembly file to form an OBJ file, and then link all the target files (including the target files driving the C code) during the link period. However, the built-in build program of DDK does not directly support Compilation and linking of assembly files (or I have not found such a method ). Therefore, you need to use other means to link the Assembly Code target file with the C Code target file. The method described in this article is to call an independent assembler function in a Windows Driver by modifying the source file and forcing the specified external function call convention.

Notes

Differences in the call conventions between the 1.32-bit platform and the 64-bit Platform

   On the 32-bit platform, the driver uses the _ stdcall call Convention by default. The function names generated by the compilation Convention are suffixed with @ xxx, and the Assembly Code uses the _ cdecl call convention, the function name compiled by the assembler does not have a suffix, so that C code cannot reference the function in the Assembly file (the linker reports the error unresolvedexternalasm_rng_available @ xxx ). The method to solve this problem is to forcibly specify the calling of external assembler functions as the _ cdecl call Convention. That is, if the asm_rng_available function in the Assembly file is called in the C file, the declaration method should be as follows:

   Externint        _ Cdecl asm_rng_available ();

After the _ cdecl call convention is forcibly specified, the asm_rng_available function generated by the C compiler and the function name generated in the Assembly file are the same.

   In the 64-bit architecture, only one local call convention and one _ cdecl convention can be ignored by the compiler. Other Call conventions have been cleared, therefore, the name of the function generated by the 64-bit DDK compiler is the same as that generated by the assembler. The function name does not have a suffix and therefore does not need to be modified. That is, if the asm_rng_available function in the Assembly file is called in the C file, the declaration method is as follows:

   Externint        Asm_rng_available ();

2. Modify the source file

   Because the build command does not automatically associate the target file of the Assembly file, you can specify it in the targetlibs macro of the source file to link the compiled compilation target file to the C target file. The specific method is as follows:

   Targetlibs =. \ instr_32.obj

In this way, you can use the build command to directly link all the target files.

Differences between some system calls in 3.64 bits and 32 bits

   The LIB library in the 64-bit DDK does not export the keinitializespinlock and kequeryinterrupttime functions. Therefore, it cannot be called directly and must be passed through mmgetsystemroutineaddres.S gets the function pointer and calls it using the function pointer.

Call assembly functions in Driver

   The following describes the entire process:

   1. Place the Assembly file and the c file of the driver in the same directory (for short, the driver directory );

   2. Modify the source file and add targetlibs = *. OBJ to the source file. * indicates the name of the Assembly file (without the extension );

    3. Declare the function name of the external Assembly file in the driver C file. If it is a 32-bit architecture, the Assembly function must be specified as _ cdecl when declaring the function;

   4. Compile the Assembly file and generate the OBJ target file under the driver directory (the compiler can use NASM or MASM );

   5. Run the build command on the entire driver project in the DDK compiling environment to generate the. SYS file.

OK...

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.