驅動開發:應用DeviceIoContro模板精講

2023-06-29 12:01:55

在筆者上一篇文章《驅動開發:應用DeviceIoContro開發模板》簡單為大家介紹瞭如何使用DeviceIoContro模板快速建立一個驅動開發通訊案例,但是該案例過於簡單也無法獨立載入執行,本章將繼續延申這個知識點,通過封裝一套標準通用模板來實現驅動通訊中的常用傳遞方式,這其中包括瞭如何傳遞字串,傳遞整數,傳遞陣列,傳遞結構體等方法。可以說如果你能掌握本章模板精講的內容基本上市面上的功能都可以使用本方法進行通訊。

首先定義驅動功能號和名字,提供介面給應用程式呼叫。

#define IOCTL_IO_Msg            CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_TEST           CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_Array          CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_STRUCT         CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_String         CTL_CODE(FILE_DEVICE_UNKNOWN, 0x804, METHOD_BUFFERED, FILE_ANY_ACCESS)

IOCTL_IO_TEST 傳遞整數: 派遣例程用於傳遞整數型別,首先使用者端通過DriveControl.IoControl將資料通過變數input傳入到核心中,並等待返回,如果返回了結果則outpot裡儲存的就是返回結果,ref_len則是返回長度。

看看使用者端如何接收這個資料的傳遞。

// 傳入x引數,返回到y中,返回長度為z
DWORD input = 100, output = 0, ref_len = 0;
DriveControl.IoControl(IOCTL_IO_TEST, &input, sizeof(input), &output, sizeof(output), &ref_len);

std::cout << "傳入引數: " << input << std::endl;
std::cout << "輸出引數: " << output << std::endl;
std::cout << "引數長度: " << ref_len << std::endl;

對於驅動中我們不需要做任何操作只需要通過memcpy(&dw, pIoBuffer, sizeof(DWORD))得到緩衝區內的資料,對該資料dw++遞增,最後通過memcpy(pIoBuffer, &dw, sizeof(DWORD))再將資料寫回到應用層。

case IOCTL_IO_TEST:
{
  DWORD dw = 0;

  // 得到輸入引數
  memcpy(&dw, pIoBuffer, sizeof(DWORD));

  // 對輸入引數進行處理
  dw++;

  // 設定輸出引數
  memcpy(pIoBuffer, &dw, sizeof(DWORD));

  // 返回通訊狀態
  status = STATUS_SUCCESS;
  break;
}

IOCTL_IO_Array 傳遞陣列: 派遣例程用於傳遞陣列型別,首先定義陣列MyArray將陣列首地址以及陣列長度傳遞到核心中,核心收到首地址以及長度後通過uInSize / sizeof(int)得到每一個元素的長度,最後迴圈輸出元素即可。

// --------------------------------------------------------------------------
// 應用層
// --------------------------------------------------------------------------
// 傳入陣列
int MyArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int array_count = 10 * sizeof(int);

DriveControl.IoControl(IOCTL_IO_Array, &MyArray, array_count, 0, 0, 0);

// --------------------------------------------------------------------------
// 核心層
// --------------------------------------------------------------------------
// 輸出一個陣列
case IOCTL_IO_Array:
{
	int *ArrayPtr = (int *)pIoBuffer;
	int count = uInSize / sizeof(int);

	for (int x = 0; x < count; x++)
	{
		DbgPrint("計數器: %d \n", ArrayPtr[x]);
	}

	status = STATUS_SUCCESS;
	break;
}

IOCTL_IO_STRUCT 傳遞結構: 結構體的傳輸與陣列類似,僅僅只是在接收到資料後對其進行一定的轉換即可,應用層只需要DriveControl.IoControl傳送send_ptr指標並等待回傳recv_ptr即可,最後將得到的結果直接輸出,而核心層收到資料後僅僅也只需要(MyData *)pIoBuffer將資料轉為一個指標即可操作這片區域,當操作完成時以同樣的方式memcpy(pIoBuffer, &send_data, sizeof(MyData))`返回一個結構體給應用層。

// --------------------------------------------------------------------------
// 應用層
// --------------------------------------------------------------------------
// 傳入一個結構體,返回結構體
MyData send_ptr, recv_ptr;
DWORD dwSize = 0;

memset(send_ptr.szUname, 0, 1024);
memset(recv_ptr.szUname, 0, 1024);

send_ptr.uuid = 1001;
strcpy(send_ptr.szUname, "lyshark");

// 派遣命令
DriveControl.IoControl(IOCTL_IO_STRUCT, &send_ptr, sizeof(send_ptr), &recv_ptr, sizeof(recv_ptr), &dwSize);
// DeviceIoControl(hDevice, IOCTL_IO_STRUCT, &send_ptr, sizeof(send_ptr), (LPVOID)&recv_ptr, sizeof(recv_ptr), &dwSize, 0);

std::cout << "核心返回資料: " << recv_ptr.uuid << std::endl;
std::cout << "核心返回資料: " << recv_ptr.szUname << std::endl;
std::cout << "核心返回長度: " << dwSize << std::endl;

// --------------------------------------------------------------------------
// 核心層
// --------------------------------------------------------------------------
// 測試傳遞結構體
case IOCTL_IO_STRUCT:
{
	MyData recv_data, send_data;

	// 獲取到應用層傳入的資料
	/*
	MyData *ptr = (MyData *)pIoBuffer;
	DbgPrint("獲取序列號: %d \n", ptr->uid);
	DbgPrint("獲取名字: %s \n", ptr->szBuf);
	*/

	// 獲取資料
	memcpy(&recv_data, pIoBuffer, sizeof(MyData));
	DbgPrint("[驅動讀入]: UID: %d --> Name: %s \n", recv_data.uuid, recv_data.szUname);

	// 傳送資料
	memset(send_data.szUname, 0, 1024);
	send_data.uuid = 1002;
	strcpy(send_data.szUname, "lyshark");
	memcpy(pIoBuffer, &send_data, sizeof(MyData));

	status = STATUS_SUCCESS;
	break;
}

如上就是核心層與應用層的部分程式碼功能分析,至於傳遞字串也很簡單大家自己學習下即可掌握,接下來我將完整程式碼分享出來,大家可以自行測試效果。

驅動程式WinDDK.sys完整程式碼;

// 署名權
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: [email protected]

#define _CRT_SECURE_NO_WARNINGS
#include <ntifs.h>
#include <windef.h>

// 定義符號連結,一般來說修改為驅動的名字即可
#define DEVICE_NAME        L"\\Device\\WinDDK"
#define LINK_NAME          L"\\DosDevices\\WinDDK"
#define LINK_GLOBAL_NAME   L"\\DosDevices\\Global\\WinDDK"

// 定義驅動功能號和名字,提供介面給應用程式呼叫
#define IOCTL_IO_Msg            CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_TEST           CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_Array          CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_STRUCT         CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_IO_String         CTL_CODE(FILE_DEVICE_UNKNOWN, 0x804, METHOD_BUFFERED, FILE_ANY_ACCESS)

// 儲存一段非分頁記憶體,用於給全域性變數使用
#define FILE_DEVICE_EXTENSION 4096

// 定義傳遞結構體
typedef struct
{
	int uuid;
	char szUname[1024];
}MyData;

// 驅動繫結預設派遣函數
NTSTATUS DefaultDispatch(PDEVICE_OBJECT _pDeviceObject, PIRP _pIrp)
{
	_pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
	_pIrp->IoStatus.Information = 0;
	IoCompleteRequest(_pIrp, IO_NO_INCREMENT);
	return _pIrp->IoStatus.Status;
}

// 驅動解除安裝的處理例程
VOID DriverUnload(PDRIVER_OBJECT pDriverObj)
{
	if (pDriverObj->DeviceObject)
	{
		UNICODE_STRING strLink;

		// 刪除符號連線和裝置
		RtlInitUnicodeString(&strLink, LINK_NAME);
		IoDeleteSymbolicLink(&strLink);
		IoDeleteDevice(pDriverObj->DeviceObject);
		DbgPrint("[kernel] # 驅動已解除安裝 \n");
	}
}

// IRP_MJ_CREATE 對應的處理例程,一般不用管它
NTSTATUS DispatchCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
	DbgPrint("[kernel] # 驅動處理例程載入 \n");
	pIrp->IoStatus.Status = STATUS_SUCCESS;
	pIrp->IoStatus.Information = 0;
	IoCompleteRequest(pIrp, IO_NO_INCREMENT);
	return STATUS_SUCCESS;
}

// IRP_MJ_CLOSE 對應的處理例程,一般不用管它
NTSTATUS DispatchClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
	DbgPrint("[kernel] # 關閉派遣 \n");
	pIrp->IoStatus.Status = STATUS_SUCCESS;
	pIrp->IoStatus.Information = 0;
	IoCompleteRequest(pIrp, IO_NO_INCREMENT);
	return STATUS_SUCCESS;
}

// IRP_MJ_DEVICE_CONTROL 對應的處理例程,驅動最重要的函數
NTSTATUS DispatchIoctl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
	NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
	PIO_STACK_LOCATION pIrpStack;
	ULONG uIoControlCode;
	PVOID pIoBuffer;
	ULONG uInSize;
	ULONG uOutSize;

	// 獲得IRP裡的關鍵資料
	pIrpStack = IoGetCurrentIrpStackLocation(pIrp);

	// 獲取控制碼
	uIoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;

	// 輸入和輸出的緩衝區(DeviceIoControl的InBuffer和OutBuffer都是它)
	pIoBuffer = pIrp->AssociatedIrp.SystemBuffer;

	// EXE傳送傳入資料的BUFFER長度(DeviceIoControl的nInBufferSize)
	uInSize = pIrpStack->Parameters.DeviceIoControl.InputBufferLength;

	// EXE接收傳出資料的BUFFER長度(DeviceIoControl的nOutBufferSize)
	uOutSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;

	// 對不同控制訊號的處理流程
	switch (uIoControlCode)
	{
		// 輸出一段話
	case IOCTL_IO_Msg:
	{
		DbgPrint("[Kernel] --> hello lyshark \n");
		status = STATUS_SUCCESS;
		break;
	}

	// 測試單一引數傳遞
	case IOCTL_IO_TEST:
	{
		DWORD dw = 0;

		// 得到輸入引數
		memcpy(&dw, pIoBuffer, sizeof(DWORD));

		// 對輸入引數進行處理
		dw++;

		// 設定輸出引數
		memcpy(pIoBuffer, &dw, sizeof(DWORD));

		// 返回通訊狀態
		status = STATUS_SUCCESS;
		break;
	}

	// 輸出一個陣列
	case IOCTL_IO_Array:
	{
		int *ArrayPtr = (int *)pIoBuffer;
		int count = uInSize / sizeof(int);

		for (int x = 0; x < count; x++)
		{
			DbgPrint("計數器: %d \n", ArrayPtr[x]);
		}

		status = STATUS_SUCCESS;
		break;
	}

	// 測試傳遞結構體
	case IOCTL_IO_STRUCT:
	{
		MyData recv_data, send_data;

		// 獲取到應用層傳入的資料
		/*
		MyData *ptr = (MyData *)pIoBuffer;
		DbgPrint("獲取序列號: %d \n", ptr->uid);
		DbgPrint("獲取名字: %s \n", ptr->szBuf);
		*/

		// 獲取資料
		memcpy(&recv_data, pIoBuffer, sizeof(MyData));
		DbgPrint("[驅動讀入]: UID: %d --> Name: %s \n", recv_data.uuid, recv_data.szUname);

		// 傳送資料
		memset(send_data.szUname, 0, 1024);
		send_data.uuid = 1002;
		strcpy(send_data.szUname, "lyshark");
		memcpy(pIoBuffer, &send_data, sizeof(MyData));

		status = STATUS_SUCCESS;
		break;
	}
	// 測試傳遞字串
	case IOCTL_IO_String:
	{
		char szString[256] = { 0 };
		char szSendPtr[256] = { 0 };

		// 接收字串
		memcpy(szString, pIoBuffer, sizeof(szString));
		DbgPrint("[接收字串]: %s \n", szString);

		// 傳送字串
		strcpy(szSendPtr, "hi, R3");
		memcpy(pIoBuffer, &szSendPtr, sizeof(szSendPtr));

		status = STATUS_SUCCESS;
		break;
	}
	}

	// 設定DeviceIoControl的*lpBytesReturned的值(如果通訊失敗則返回0長度)
	if (status == STATUS_SUCCESS)
	{
		pIrp->IoStatus.Information = uOutSize;
	}
	else
	{
		pIrp->IoStatus.Information = 0;
	}

	// 設定DeviceIoControl的返回值是成功還是失敗
	pIrp->IoStatus.Status = status;
	IoCompleteRequest(pIrp, IO_NO_INCREMENT);
	return status;
}

// 驅動的初始化工作
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegistryString)
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING ustrLinkName;
	UNICODE_STRING ustrDevName;
	PDEVICE_OBJECT pDevObj;

	// 初始化其他派遣
	for (ULONG i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
	{
		// DbgPrint("初始化派遣: %d \n", i);
		pDriverObj->MajorFunction[i] = DefaultDispatch;
	}

	// 設定分發函數和解除安裝例程
	pDriverObj->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
	pDriverObj->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
	pDriverObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchIoctl;
	pDriverObj->DriverUnload = DriverUnload;

	// 建立一個裝置
	RtlInitUnicodeString(&ustrDevName, DEVICE_NAME);

	// FILE_DEVICE_EXTENSION 建立裝置時,指定裝置擴充套件記憶體的大小,傳一個值進去,就會給裝置分配一塊非頁面記憶體。
	status = IoCreateDevice(pDriverObj, sizeof(FILE_DEVICE_EXTENSION), &ustrDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
	if (!NT_SUCCESS(status))
	{
		return status;
	}

	// 判斷支援的WDM版本,其實這個已經不需要了,純屬WIN9X和WINNT並存時代的殘留物
	if (IoIsWdmVersionAvailable(1, 0x10))
	{
		RtlInitUnicodeString(&ustrLinkName, LINK_GLOBAL_NAME);
	}
	else
	{
		RtlInitUnicodeString(&ustrLinkName, LINK_NAME);
	}

	// 建立符號連線
	status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);
	if (!NT_SUCCESS(status))
	{
		DbgPrint("建立符號連結失敗 \n");
		IoDeleteDevice(pDevObj);
		return status;
	}
	DbgPrint("[ Hello LyShark.com ] # 驅動初始化完畢 \n");

	// 返回載入驅動的狀態(如果返回失敗,驅動講被清除出核心空間)
	return STATUS_SUCCESS;
}

應用層使用者端程式lyshark.exe完整程式碼;

// 署名權
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: [email protected]

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

#pragma comment(lib,"user32.lib")
#pragma comment(lib,"advapi32.lib")

// 定義驅動功能號和名字,提供介面給應用程式呼叫
#define IOCTL_IO_Msg            0x800
#define IOCTL_IO_TEST           0x801
#define IOCTL_IO_Array          0x802
#define IOCTL_IO_STRUCT         0x803
#define IOCTL_IO_String         0x804

class cDrvCtrl
{
public:
	cDrvCtrl()
	{
		m_pSysPath = NULL;
		m_pServiceName = NULL;
		m_pDisplayName = NULL;
		m_hSCManager = NULL;
		m_hService = NULL;
		m_hDriver = INVALID_HANDLE_VALUE;
	}
	~cDrvCtrl()
	{
		CloseServiceHandle(m_hService);
		CloseServiceHandle(m_hSCManager);
		CloseHandle(m_hDriver);
	}

	// 安裝驅動
	BOOL Install(PCHAR pSysPath, PCHAR pServiceName, PCHAR pDisplayName)
	{
		m_pSysPath = pSysPath;
		m_pServiceName = pServiceName;
		m_pDisplayName = pDisplayName;
		m_hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
		if (NULL == m_hSCManager)
		{
			m_dwLastError = GetLastError();
			return FALSE;
		}
		m_hService = CreateServiceA(m_hSCManager, m_pServiceName, m_pDisplayName,
			SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
			m_pSysPath, NULL, NULL, NULL, NULL, NULL);
		if (NULL == m_hService)
		{
			m_dwLastError = GetLastError();
			if (ERROR_SERVICE_EXISTS == m_dwLastError)
			{
				m_hService = OpenServiceA(m_hSCManager, m_pServiceName, SERVICE_ALL_ACCESS);
				if (NULL == m_hService)
				{
					CloseServiceHandle(m_hSCManager);
					return FALSE;
				}
			}
			else
			{
				CloseServiceHandle(m_hSCManager);
				return FALSE;
			}
		}
		return TRUE;
	}

	// 啟動驅動
	BOOL Start()
	{
		if (!StartServiceA(m_hService, NULL, NULL))
		{
			m_dwLastError = GetLastError();
			return FALSE;
		}
		return TRUE;
	}

	// 關閉驅動
	BOOL Stop()
	{
		SERVICE_STATUS ss;
		GetSvcHandle(m_pServiceName);
		if (!ControlService(m_hService, SERVICE_CONTROL_STOP, &ss))
		{
			m_dwLastError = GetLastError();
			return FALSE;
		}
		return TRUE;
	}

	// 移除驅動
	BOOL Remove()
	{
		GetSvcHandle(m_pServiceName);
		if (!DeleteService(m_hService))
		{
			m_dwLastError = GetLastError();
			return FALSE;
		}
		return TRUE;
	}

	// 開啟驅動
	BOOL Open(PCHAR pLinkName)
	{
		if (m_hDriver != INVALID_HANDLE_VALUE)
			return TRUE;
		m_hDriver = CreateFileA(pLinkName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
		if (m_hDriver != INVALID_HANDLE_VALUE)
			return TRUE;
		else
			return FALSE;
	}

	// 傳送控制訊號
	BOOL IoControl(DWORD dwIoCode, PVOID InBuff, DWORD InBuffLen, PVOID OutBuff, DWORD OutBuffLen, DWORD *RealRetBytes)
	{
		DWORD dw;
		BOOL b = DeviceIoControl(m_hDriver, CTL_CODE_GEN(dwIoCode), InBuff, InBuffLen, OutBuff, OutBuffLen, &dw, NULL);
		if (RealRetBytes)
			*RealRetBytes = dw;
		return b;
	}
private:

	// 獲取服務控制程式碼
	BOOL GetSvcHandle(PCHAR pServiceName)
	{
		m_pServiceName = pServiceName;
		m_hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
		if (NULL == m_hSCManager)
		{
			m_dwLastError = GetLastError();
			return FALSE;
		}
		m_hService = OpenServiceA(m_hSCManager, m_pServiceName, SERVICE_ALL_ACCESS);
		if (NULL == m_hService)
		{
			CloseServiceHandle(m_hSCManager);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	// 獲取控制訊號對應字串
	DWORD CTL_CODE_GEN(DWORD lngFunction)
	{
		return (FILE_DEVICE_UNKNOWN * 65536) | (FILE_ANY_ACCESS * 16384) | (lngFunction * 4) | METHOD_BUFFERED;
	}

public:
	DWORD m_dwLastError;
	PCHAR m_pSysPath;
	PCHAR m_pServiceName;
	PCHAR m_pDisplayName;
	HANDLE m_hDriver;
	SC_HANDLE m_hSCManager;
	SC_HANDLE m_hService;
};

void GetAppPath(char *szCurFile)
{
	GetModuleFileNameA(0, szCurFile, MAX_PATH);
	for (SIZE_T i = strlen(szCurFile) - 1; i >= 0; i--)
	{
		if (szCurFile[i] == '\\')
		{
			szCurFile[i + 1] = '\0';
			break;
		}
	}
}

// 定義傳遞結構體
typedef struct
{
	int uuid;
	char szUname[1024];
}MyData;

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

	// 設定驅動名稱
	char szSysFile[MAX_PATH] = { 0 };
	char szSvcLnkName[] = "WinDDK";;
	GetAppPath(szSysFile);
	strcat(szSysFile, "WinDDK.sys");

	// 安裝並啟動驅動
	DriveControl.Install(szSysFile, szSvcLnkName, szSvcLnkName);
	DriveControl.Start();

	// 開啟驅動的符號連結
	DriveControl.Open("\\\\.\\WinDDK");

	// 無引數輸出
	DriveControl.IoControl(IOCTL_IO_Msg, 0, 0, 0, 0, 0);

	// 傳入x引數,返回到y中,返回長度為z
	DWORD input = 100, output = 0, ref_len = 0;
	DriveControl.IoControl(IOCTL_IO_TEST, &input, sizeof(input), &output, sizeof(output), &ref_len);

	std::cout << "傳入引數: " << input << std::endl;
	std::cout << "輸出引數: " << output << std::endl;
	std::cout << "引數長度: " << ref_len << std::endl;

	// 傳入陣列
	int MyArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	int array_count = 10 * sizeof(int);

	DriveControl.IoControl(IOCTL_IO_Array, &MyArray, array_count, 0, 0, 0);

	// 傳入一個結構體,返回結構體
	MyData send_ptr, recv_ptr;
	DWORD dwSize = 0;

	memset(send_ptr.szUname, 0, 1024);
	memset(recv_ptr.szUname, 0, 1024);

	send_ptr.uuid = 1001;
	strcpy(send_ptr.szUname, "lyshark");

	// 派遣命令
	DriveControl.IoControl(IOCTL_IO_STRUCT, &send_ptr, sizeof(send_ptr), &recv_ptr, sizeof(recv_ptr), &dwSize);
	// DeviceIoControl(hDevice, IOCTL_IO_STRUCT, &send_ptr, sizeof(send_ptr), (LPVOID)&recv_ptr, sizeof(recv_ptr), &dwSize, 0);

	std::cout << "核心返回資料: " << recv_ptr.uuid << std::endl;
	std::cout << "核心返回資料: " << recv_ptr.szUname << std::endl;
	std::cout << "核心返回長度: " << dwSize << std::endl;

	// 傳入一個字串,返回一個字串
	char szString[256] = { 0 };
	char szRecvPtr[256] = { 0 };

	// 派遣命令
	strcpy(szString, "hello lyshark");
	DriveControl.IoControl(IOCTL_IO_String, &szString, sizeof(szString), &szRecvPtr, sizeof(szRecvPtr), &dwSize);
	std::cout << "核心返回資料: " << szRecvPtr << std::endl;


	// 關閉符號連結控制程式碼
	CloseHandle(DriveControl.m_hDriver);

	// 停止並解除安裝驅動
	DriveControl.Stop();
	DriveControl.Remove();

	system("pause");
	return 0;
}

手動編譯這兩個程式,將驅動簽名後以管理員身份執行lyshark.exe使用者端,此時螢幕中即可看到捲動輸出效果,如此一來就實現了迴圈傳遞引數的目的。