load_sysfile;CreateService

// test7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


bool load_sysfile(char *theDriverName)
{
       char aPath[1024];
       char aCurrentDirectory[515];
       SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
       if(!sh)
       {
             return false;
       }
       GetCurrentDirectory( 512, aCurrentDirectory);
       _snprintf(aPath,
                 1022,
                 "%s\\%s.sys",
                 aCurrentDirectory,
                 theDriverName);
	   printf("加载路径:%s\n",aPath);
      
       SC_HANDLE rh = CreateService(sh,
                                    theDriverName,
                                    theDriverName,
                                    SERVICE_ALL_ACCESS,
                                    SERVICE_KERNEL_DRIVER,
                                    SERVICE_DEMAND_START,
                                    SERVICE_ERROR_NORMAL,
                                    aPath,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL);
       if(!rh)
 
       {
             if (GetLastError() == ERROR_SERVICE_EXISTS)
             {
                   // service exists
                   rh = OpenService(sh,
                                    theDriverName,
                                    SERVICE_ALL_ACCESS);
                   if(!rh)
                   {
                         CloseServiceHandle(sh);
                         return false;
                   }
             }
             else
             {
                   CloseServiceHandle(sh);
                   return false;
             }
       }
       // start the drivers
 
       if(rh)
       {
             if(0 == StartService(rh, 0, NULL))
             {
                   if(ERROR_SERVICE_ALREADY_RUNNING == GetLastError())
                   {
 
                         // no real problem
                   }
                   else
                   {
                         CloseServiceHandle(sh);
                         CloseServiceHandle(rh);
                         return false;
                   }
             }
             CloseServiceHandle(sh);
             CloseServiceHandle(rh);
       }
       return true;
}


bool unload_sys(char* theDriverName){
       char aPath[1024];
       char aCurrentDirectory[515];
       SERVICE_STATUS SvrSta; 
       SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
       if(!sh)
       {
             return false;
       }
       GetCurrentDirectory( 512, aCurrentDirectory);
       _snprintf(aPath,
                 1022,
                 "%s\\%s.sys",
                 aCurrentDirectory,
                 theDriverName);
     //MessageBox(NULL,aPath,"",0);

	   printf("卸载路径:%s\n",aPath);
 
     SC_HANDLE hService=    OpenService(sh,theDriverName, SERVICE_STOP | DELETE); 
     if (hService==NULL){
         CloseServiceHandle(sh);
         return false;
     }
 
     if (!ControlService(hService, SERVICE_CONTROL_STOP, &SvrSta)){    
 
     }else{
 
     }
     DeleteService(hService);
     CloseServiceHandle(hService);
     CloseServiceHandle(sh);
 
     return true;
 
 
}


int main(int argc, char* argv[])
{

	char sSysName[]="HelloDDK";

	printf("%s.sys\n",sSysName);

	if(load_sysfile( sSysName ) == TRUE)
	{
		printf("加载驱动%s成功!\n", ( sSysName ));
	}
	else
	{
		printf("加载驱动%s失败!\n", ( sSysName ));

	}
	printf("按任意键卸载驱动!\n");

	getchar();

	if(unload_sys( sSysName ) == TRUE)
	{
		printf("卸载驱动%s成功!\n", ( sSysName ));
	}
	else
	{
		printf("卸载驱动%s失败!\n", ( sSysName ));

	}

	printf("\n\n");

	return 0;
}

你可能感兴趣的:(load_sysfile;CreateService)