Click here to Skip to main content
15,885,998 members
Articles / Desktop Programming / MFC

How to install driver dynamically or install driver as a service for Windows

Rate me:
Please Sign up or sign in to vote.
3.85/5 (7 votes)
2 Dec 2011CPOL2 min read 60.1K   4.3K   40   6
Install driver dynamically or install driver as a service for Windows.

Introduction

Usually when we complete a driver compilation, we use the inf file to install the driver. But sometimes we hope to install drivers dynamically or install a driver as a service for Windows. In this article, we assume that the driver has been compiled and used directly. The driver must have 32-bit and 64-bit versions so that we can experiment under 32-bit and 64-bit Operating Systems. How to compile a driver is not the task of this article.

About the path of the driver file which will be installed: usually we may place the installation program and driver file in the same directory. In most cases, it is feasible. But if you want to install the driver as a service for Windows on a 64-bit Operating System environment, it will fail. In this case the directory of the driver file must be "C:\\Windows\\System32\\drivers" so we use "C:\\Windows\\System32\\drivers" instead of the current directory. The driver file is copied to the directory in advance.

Install_ADL_driver_BOOT_START.JPG

Using the code

  1. Install a driver invoke function GJ_Install_ADL_Driver_BOOT_START(). Uninstall a driver invoke function GJ_Uninstall_ADL_Driver().
  2. Code example:

    C++
    //
    #include "install.h"
    void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedIntsall()
    {
     // TODO: Add your control notification handler code here
     //Because if the current OS is 64bit,gjglly.sys must
     //copy to "C:\\Windows\\System32\\drivers",
     //so we use "C:\\Windows\\System32\\drivers" instead of current directory.
     //Refer to function SetupDriverName.
     GJ_Install_ADL_Driver_BOOT_START();
    }
    void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedUninstall()
    {
     // TODO: Add your control notification handler code here
     GJ_Uninstall_ADL_Driver();
    }//
  3. Setting the directory of the driver file refers to the function SetupDriverName. The code for how to set the current path is available in the full source code.
  4. C++
    //
    BOOLEAN
    SetupDriverName(
        __inout_bcount_full(BufferLength) PCHAR DriverLocation,
        __in ULONG BufferLength
        )
    {
        HANDLE fileHandle;
        DWORD driverLocLen = 0;
     //
     //
     //Because if the current OS is 64bit,gjglly.sys must copy to "C:\\Windows\\System32\\drivers",
     //so we use "C:\\Windows\\System32\\drivers" instead of current directory.
     //
     //
     /* //Get the current directory.
        GetModuleFileNameA(GetModuleHandle(NULL), DriverLocation, BufferLength);
     char* pszSlash = strrchr(DriverLocation, '\\');
     pszSlash[0]=0;
     *///Get the current directory.
     //
     GetSystemDirectory(DriverLocation, BufferLength);//"C:\\Windows\\System32\\drivers"
        //
        //
        // Setup path name to driver file.
        //
        //if (FAILED( StringCbCatA(DriverLocation, BufferLength, 
        //    "<a href="file://%22driver_name%22.sys/">\\"DRIVER_NAME".sys</a>") )) //Get the current directory.
        if (FAILED( StringCbCatA(DriverLocation, BufferLength, 
            "<a href="file://drivers//%22DRIVER_NAME%22.sys">\\drivers\\"DRIVER_NAME".sys</a>") )) 
     {
            return FALSE;
        }
     //
     //Following code will failed in 64bit OS
     /*
        //
        // Insure driver file is in the specified directory.
        //
        //
        //
        if ((fileHandle = CreateFileA(DriverLocation,
                                     GENERIC_READ,
                                     0,
                                     NULL,
                                     OPEN_EXISTING,
                                     FILE_ATTRIBUTE_NORMAL,
                                     NULL
                                     )) == INVALID_HANDLE_VALUE) {
            //
            //
            //printf("%s.sys is not loaded.\n", DRIVER_NAME);
            //
            //
            // Indicate failure.
            //
            //
            return FALSE;
        }
        //
        //
        // Close open file handle.
        //
        //
        if (fileHandle) {
            CloseHandle(fileHandle);
        }
        //
        // Indicate success.
        //
     */
        //
        return TRUE;
        //
    }   // SetupDriverName
    
    //
  5. The key to installing drivers dynamically or installing a driver as a service is the API function CreateService.
  6. C++
    //
    schService = CreateService(SchSCManager,          // handle of service control manager database
         DriverName,            // address of name of service to start
         DriverName,            // address of display name
         SERVICE_ALL_ACCESS,    // type of access to service
         SERVICE_KERNEL_DRIVER, // type of service
         SERVICE_BOOT_START,    // when to start service !!! SERVICE_DEMAND_START SERVICE_AUTO_START
         SERVICE_ERROR_NORMAL,  // severity if service fails to start
         ServiceExe,            // address of name of binary file
         NULL,                  // service does not belong to a group
         NULL,                  // no tag requested
         NULL,                  // no dependency names
         NULL,                  // use LocalSystem account
         NULL                   // no password for service account
         );
    //

You can change the sixth parameter "dwStartType" to set the startup type.

C++
dwStartType [in]

The service start options can be one of the following values:

ValueMeaning
SERVICE_AUTO_START 0x00000002A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services.
SERVICE_BOOT_START 0x00000000A device driver started by the system loader. This value is valid only for driver services.
SERVICE_DEMAND_START 0x00000003A service started by the service control manager when a process calls the StartService function. For more information, see Starting Services on Demand.
SERVICE_DISABLED 0x00000004A service that cannot be started. Attempts to start the service results in the error code ERROR_SERVICE_DISABLED.
SERVICE_SYSTEM_START 0x00000001A device driver started by the IoInitSystem function. This value is valid only for driver services.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer AntiDebugLIB Inc
United States United States
There are so many hackers all over the world that no software can escape the doom of being cracked.And even almost everybody believe that it is impossible to protect the applications through the technology means.But we still work hard to find applications protection solution [32-bit] [64-bit] in order to protect our works.

Homepage:

http://www.antidebuglib.com/

http://www.wintsd.com/

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jose A Pascoa27-Jun-16 3:18
Jose A Pascoa27-Jun-16 3:18 
Question//Following code will failed in 64bit OS Pin
Jose A Pascoa27-Jun-16 3:16
Jose A Pascoa27-Jun-16 3:16 
QuestionR u a Chinese-American? Pin
cghao2-Jan-14 21:17
cghao2-Jan-14 21:17 
GeneralMy vote of 1 Pin
HelgeKlein23-Sep-12 10:22
HelgeKlein23-Sep-12 10:22 
GeneralRe: My vote of 1 Pin
idle632-Mar-14 3:47
idle632-Mar-14 3:47 
QuestionIt's not working in Windows 7 Pin
DO KYU KIM7-May-12 16:09
DO KYU KIM7-May-12 16:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.