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

📄 main.cpp

📁 老外写的一个相当不错的虚拟显示器驱动
💻 CPP
字号:
/*
	This sample application asynchronously reads data from a driver, using the IOCP API.
	It uses multiple threads and multiple reads on the driver, making it a robust and fast
	data reader.

	Created by Tamas Karoly 2004.
*/

#include <windows.h>
#include <stdio.h>
#include <conio.h>


#define MAX_THREAD_COUNT		4
#define MAX_READER_INFO			4
#define COMPKEY_STOP_THREAD		1234

struct READER_INFO
{
	CHAR			Buffer[128];
	OVERLAPPED		Overlapped;
};


HANDLE			hDriver = NULL;						// The HANDLE to the driver.
HANDLE			hIOCP = NULL;						// HANDLE of the IO completion port.
HANDLE			hIOThreads[MAX_THREAD_COUNT];		// The threads that read the driver.
READER_INFO		ReaderInfo[MAX_READER_INFO];		// The reader infos into which the driver reads the data.


BOOL ReadMessage(READER_INFO* pReaderInfo);
VOID ProcessData(READER_INFO* pReaderInfo);
DWORD WINAPI IOCPWorkerThread(LPVOID lpParameter);


/**************************************************************************************************/
void main(void)
{
	int i;

	// Open the driver.
	hDriver = CreateFile("\\\\.\\SAMPLEDEV0", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
	if (hDriver == INVALID_HANDLE_VALUE)
	{
		printf("Driver couldn't be opened. Error=%i\n", GetLastError());
		return;
	}

	// Create the IO Completion Port, then create the threads that will handle IO on it.
	hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, MAX_THREAD_COUNT);

	// Create some threads that will read the data.
	for (i = 0; i < MAX_THREAD_COUNT; i++)
		hIOThreads[i] = CreateThread(NULL, 0, IOCPWorkerThread, NULL, 0, NULL);

	// Assign the driver handle to the completion port.
	CreateIoCompletionPort(hDriver, hIOCP, 0, MAX_THREAD_COUNT);

	// Initiate the first read operations.
	for (i = 0; i < MAX_READER_INFO; i++)
		ReadMessage(&ReaderInfo[i]);

	// Wait for a key...
	printf("Press a key to finish...");
	while(1)
	{
		if (kbhit())
		{
			getch();
			break;
		}
	}

	// Tell the worker threads to stop.
	for (i = 0; i < MAX_THREAD_COUNT; i++)
		PostQueuedCompletionStatus(hIOCP, 0, COMPKEY_STOP_THREAD, NULL);

	// Wait for the threads to stop.
	for (i = 0; i < MAX_THREAD_COUNT; i++)
	{
		WaitForSingleObject(hIOThreads[i], INFINITE);
		CloseHandle(hIOThreads[i]);
	}

	CloseHandle(hDriver);
	CloseHandle(hIOCP);
}


/**************************************************************************************************/
DWORD WINAPI IOCPWorkerThread(LPVOID lpParameter)
{
	DWORD NumberOfBytesTransfered;
	LPOVERLAPPED lpOverlapped;
	READER_INFO* ReaderInfo;
	DWORD CompletionKey;
	BOOL bSuccess;

	for (;;)
	{
		// Wait for an IO operation to complete.
		bSuccess = GetQueuedCompletionStatus(hIOCP, &NumberOfBytesTransfered, &CompletionKey, &lpOverlapped, INFINITE);

		if (COMPKEY_STOP_THREAD == CompletionKey)
			break;
		
		ReaderInfo = CONTAINING_RECORD(lpOverlapped, READER_INFO, Overlapped);

		// Process completed operation if it was successfull.
		if (bSuccess)
		{
			// Do something with the data read.
			ProcessData(ReaderInfo);

			// Initiate next read operation, if the previous one successfully finished.
			ReadMessage(ReaderInfo);
		}
	}

	return 0;
}


/**************************************************************************************************/
BOOL ReadMessage(READER_INFO* pReaderInfo)
{
	BOOL Res = TRUE;
	DWORD BytRet;

	printf("Reading data from the driver...\n");

	if (FALSE == ReadFile(hDriver, pReaderInfo->Buffer, 10, &BytRet, &pReaderInfo->Overlapped))
		Res = (GetLastError() == ERROR_IO_PENDING);

	return Res;
}


/**************************************************************************************************/
VOID ProcessData(READER_INFO* pReaderInfo)
{
	// Data is in pReaderInfo->Buffer.
	// TODO: do something with the data.
	printf("Processing data...\n");
}

⌨️ 快捷键说明

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