Click here to Skip to main content
Click here to Skip to main content

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

, 2 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

    //
    #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. //
    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. //
    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.

dwStartType [in]

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

Value Meaning
SERVICE_AUTO_START 0x00000002 A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services.
SERVICE_BOOT_START 0x00000000 A device driver started by the system loader. This value is valid only for driver services.
SERVICE_DEMAND_START 0x00000003 A service started by the service control manager when a process calls the StartService function. For more information, see Starting Services on Demand.
SERVICE_DISABLED 0x00000004 A service that cannot be started. Attempts to start the service results in the error code ERROR_SERVICE_DISABLED.
SERVICE_SYSTEM_START 0x00000001 A 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)

Share

About the Author

Jim Charles
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

 
QuestionR u a Chinese-American? Pinmembercghao2-Jan-14 21:17 
GeneralMy vote of 1 PinmemberHelgeKlein23-Sep-12 10:22 
GeneralRe: My vote of 1 Pinmemberidle632-Mar-14 3:47 
QuestionIt's not working in Windows 7 PinmemberDO KYU KIM7-May-12 16:09 
Thanks your good source !!
 
I rebuild the source by Visual Studio 2008.
And test in Windows 7.
But because of SC_MANAGER_ALL_ACCESS in OpenSCManager, it not working properly.
Even though OpenSCManager returns handle, the next problem is SERVICE_ALL_ACCESS in CreateService function.
I think this is because of security in Windows 7.
Do you have any solution for this problem in Windows 7.

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 2 Dec 2011
Article Copyright 2011 by Jim Charles
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid