windows 驱动与内核调试 学习5

Posted 不会写代码的丝丽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows 驱动与内核调试 学习5相关的知识,希望对你有一定的参考价值。

前言

本文学习如何自己写一个服务装载一个内核驱动。

类似如下程序

驱动类似如饭店的后厨,而点菜的前台则是window的服务。由服务去进行驱动装载卸载等。

= =好懒直接贴出代码把

// DriverService.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<Windows.h>
#include<tchar.h>

#include<Windows.h>
#include<stdlib.h>
#define MY_CTL(NUM) CTL_CODE(FILE_DEVICE_UNKNOWN,0x800+NUM, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define CTL_ENUM_1 MY_CTL(2)
#define CTL_ENUM_2 MY_CTL(3)


#define DEVICE_NAME "HelloWordKDriver2Service"
#define DRIVER_PATH "C:\\\\Users\\\\Nick\\\\Desktop\\\\HelloWordKDriver2.sys"

using std::cout;
using std::endl;
// Formats a message string using the specified message and variable
// list of arguments.
char* GetFormattedMessage(DWORD code)

	TCHAR buf[256] =  0 ;
	FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, code, NULL,
		buf, (sizeof(buf) / sizeof(TCHAR)), NULL);

	return buf;


DWORD Install(const char const* szDriverName, const char const* szDriverPath) 

	DWORD dwRet = 0;
	SC_HANDLE hService = nullptr;
	SC_HANDLE hSCM = nullptr;

	hSCM = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);

	if (hSCM == NULL)
	

		dwRet = GetLastError();
		cout << "OpenSCManager failure" << endl;
		goto SAVE_EXIT;
	
	cout << "OpenSCManager done" << endl;
	hService = CreateService(hSCM, szDriverName, szDriverName,
		SERVICE_ALL_ACCESS,
		SERVICE_KERNEL_DRIVER,
		SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
		szDriverPath,
		NULL, NULL, NULL, NULL, NULL);

	if (hService == NULL)
	
		dwRet = GetLastError();
		if (dwRet != ERROR_SERVICE_EXISTS)
		
			cout << "CreateService failure" << endl;
			goto SAVE_EXIT;
		
		else 
			dwRet = 0;
			hService = OpenService(hSCM, szDriverName, SERVICE_ALL_ACCESS);
			if (hService == NULL)
			
				dwRet = GetLastError();
				cout << "OpenService failure" << endl;
				goto SAVE_EXIT;
			
		

	
	cout << "CreateService done" << endl;

	if (StartService(hService, 0, NULL))
	
		cout << "StartService done" << endl;

	
	else 
		dwRet = GetLastError();
		cout << "StartService failure" << endl;

	
SAVE_EXIT:
	if (hService != NULL)
	
		CloseServiceHandle(hService);
	
	if (hSCM != NULL)
	
		CloseServiceHandle(hSCM);
	
	return dwRet;



DWORD Uninstall(const char const* szDriverName) 

	DWORD dwRet = 0;
	SC_HANDLE hService = nullptr;
	SC_HANDLE hSCM = nullptr;

	hSCM = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);

	if (hSCM == NULL)
	
		dwRet = GetLastError();
		cout << "OpenSCManager failure" << endl;
		goto SAVE_EXIT;
	
	cout << "OpenSCManager done" << endl;
	hService = OpenService(hSCM, szDriverName, SERVICE_ALL_ACCESS);
	if (hService == NULL)
	
		dwRet = GetLastError();
		cout << "OpenService failure" << endl;
		goto SAVE_EXIT;
	


	cout << "OpenService done" << endl;


	SERVICE_STATUS Status;
	if (ControlService(hSCM, SERVICE_CONTROL_STOP, &Status))
	
		cout << "ControlService done" << endl;
	
	else 
		cout << "ControlService failure" << endl;
	

	if (DeleteService(hService))
	
		cout << "DeleteService done" << endl;
	
	else 
		cout << "DeleteService failure" << endl;
		dwRet = GetLastError();
	
SAVE_EXIT:
	if (hService != NULL)
	
		CloseServiceHandle(hService);
	
	if (hSCM != NULL)
	
		CloseServiceHandle(hSCM);
	
	return dwRet;

void controlDrive() 
	HANDLE hFile;
	hFile = CreateFile("\\\\\\\\\\?\\\\MytestDriver",
		GENERIC_WRITE | GENERIC_READ,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);
	if (hFile == INVALID_HANDLE_VALUE)
	
		std::cout << "INVALID_HANDLE_VALUE  " << GetLastError() << "\\n";

		return ;
	


	char szControlIn[120] = "hello";
	char szControlOut[120] = "hello myboy";
	DWORD out = 0;


	if (DeviceIoControl(hFile, CTL_ENUM_1, szControlIn, sizeof szControlIn, szControlOut, sizeof szControlOut, &out, nullptr))
	
		std::cout << "DeviceIoControl success out" << out << " szControlOut:" << szControlIn << std::endl;

		printf("\\r\\n szControlIn %s\\r\\n", szControlIn);
		printf("\\r\\n szControlOut %s\\r\\n", szControlOut);
	
	else
	
		std::cout << "DeviceIoControl failure " << GetLastError() << std::endl;

	
	CloseHandle(hFile);


int main()

	DWORD ret = 0;
	ret = Install(DEVICE_NAME, DRIVER_PATH);
	if (ret != 0)
	
		printf("Install erro %s\\r\\n", GetFormattedMessage(ret));
	
	controlDrive();
	Uninstall(DEVICE_NAME);
	std::cout << "Hello World!\\n";


以上是关于windows 驱动与内核调试 学习5的主要内容,如果未能解决你的问题,请参考以下文章

windows 驱动与内核调试 学习5

windows 驱动与内核调试 学习

windows 驱动与内核调试 学习

windows 驱动与内核调试 学习4

windows 驱动与内核调试 学习4

windows 驱动与内核调试 学习4