⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scm.cpp

📁 加载驱动服务的程序
💻 CPP
字号:
#include <stdio.h>
#include <Windows.h>
#include <WinError.h>

void showErrorMessage(LPTSTR errStr, DWORD errNO);

void _cdecl main(void)
{
    SC_HANDLE hSCManager;
    SC_HANDLE hService;
    SERVICE_STATUS ss;
    BOOL StartServiceResualt;
    char serName[10];
	//char sysPath[50];
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
    
    if(hSCManager)
    {
		printf("Load Driver success ~\n");
        printf("Create Service,hScManager=%ld\n",hSCManager);

		printf("pls input sys Service name:\n");
        scanf("%s",serName);
		//printf("pls input sys full path&file name :\n");
		//scanf("%s",sysPath);
        fflush(stdin);
        hService = CreateServiceA(hSCManager, 
									serName,
									serName,
									SERVICE_ALL_ACCESS,
									SERVICE_KERNEL_DRIVER,
									SERVICE_DEMAND_START,
									SERVICE_ERROR_NORMAL,
									"C:\\xxx\\passthru\\driver\\objchk_wnet_x86\\i386\\passthru.sys",
									NULL,
									NULL,
									NULL,
									NULL,
									NULL
								 );

		if(0 != GetLastError())
			//printf("CreateError:%d\n",GetLastError());
			showErrorMessage((LPTSTR)("CreateError:"+'\0'),GetLastError());
    
		if(0 != hService)
			printf("hService:%d\n",hService);
    
		if(!hService)
		{
			hService = OpenServiceA(hSCManager, serName, SERVICE_ALL_ACCESS);

			if(0 != GetLastError())
				//printf("OpenError:%d\n",GetLastError());
				showErrorMessage((LPTSTR)("OpenError:"+'\0'),GetLastError());
		}


		if(hService)
		{
			printf("Start Service\n");
			StartServiceResualt = StartService(hService, 0, NULL);
			printf("StartServiceResualt:%ld\n",StartServiceResualt);
			if(!StartServiceResualt)
			{
				//printf("StartError:%d\,hServicen=%ld,StartServiceResualt=%ld\n",GetLastError(),hService,StartServiceResualt);
				showErrorMessage((LPTSTR)("StartError:"+'\0'), GetLastError());
			}
			else
			{
				printf("Start Service Success \n");
				printf(".\n.\n");
/*///=======================================================================================================================================
				HANDLE hProcessEvent = ::OpenEvent(SYNCHRONIZE, FALSE, "NTProcessEvent");
				CALLBACK_INFO callbackInfo, callbackTemp = {0};
				while(::WaitForSingleObject(hProcessEvent, INFINITE) == WAIT_OBJECT_0)
				{
					DWORD nBytesTeturn;

				}
/*///=======================================================================================================================================
				printf(".\n.\n");
			}			
			printf("Press Enter to close service\r\n");
			getchar();
			fflush(stdin);
			if(!ControlService(hService, SERVICE_CONTROL_STOP, &ss))
			{
				//printf("Service Stop Error:%d\n",GetLastError());
				showErrorMessage((LPTSTR)("Service Stop Error:"+'\0'), GetLastError());
			}
			else 
			{
				printf("Service Stop Success ~\n");
			}
			if(!DeleteService(hService))
			{
				//printf("Service Delete Error:%d\n",GetLastError());
				showErrorMessage((LPTSTR)("Service Delete Error:"+'\0'), GetLastError());
			}
			else 
			{
				printf("Service Delete Success~\n");
			}
			
			if(CloseServiceHandle(hService))
			{
				printf("Service has be Closed !\n");
			}
			else
			{
				//printf("CloseError:%d\n",GetLastError());
				showErrorMessage((LPTSTR)("CloseError:"+'\0'), GetLastError());
			}
			
		}
        CloseServiceHandle(hSCManager);
    }
	else
	{
		printf("Load Driver failed~\n ");
	}
	getchar();
}

void showErrorMessage(LPTSTR errStr, DWORD errNO)
{
	LPVOID errBuf;
	
	::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
               NULL, errNO, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
		(LPTSTR)&errBuf, 0, NULL);

	printf("%s",errStr);
	printf("0x0%x:",errNO); 
	printf("%s\n",(LPCTSTR)errBuf); 

	LocalFree(errBuf);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -