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

📄 test_intwdm.cpp

📁 武安河书第一版(vxd)Windows 驱动例子
💻 CPP
字号:
// Test_intwdm.cpp
//
// Generated by DriverWizard version DriverStudio 2.0.1 (Build 36)
//
// This console application demonstrates how to open a handle
// to a device in your driver, and communicate with the driver
// using Read, Write, and DeviceIoControl calls, as appropriate.
//
// This test program attempts to open the device using the
// GUID defined in "..\IntwdmDeviceinterface.h"

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

#include <winioctl.h>
#include "..\intwdmioctl.h"

#include "..\IntwdmDeviceinterface.h"	// Has class GUID definition

// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);

void Test_READ_DATA(void);

// Global data

HANDLE	hDevice = INVALID_HANDLE_VALUE;
HANDLE	hEvent = INVALID_HANDLE_VALUE;

// Class GUID used to open device
//
GUID ClassGuid = IntwdmDevice_CLASS_GUID;

DWORD WINAPI ThreadProc(LPVOID junkola)
{							// ThreadProc
	while(TRUE)	{
		WaitForSingleObject(hEvent, INFINITE);
		puts("Event happened");
		Test_READ_DATA();
	}
	return 0;

}							// ThreadProc
int __cdecl main(int argc, char *argv[])
{
	DWORD	Error;

	printf("Test application Test_intwdm starting...\n");

	hDevice = OpenByInterface( &ClassGuid, 0, &Error);
	if (hDevice == INVALID_HANDLE_VALUE)
	{
		printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError());
		exit(1);
	}
	else
	{
		printf("Device found, handle open.\n");
	}

	DWORD junk;

	hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (!DeviceIoControl(hDevice, EVENT_REGISTER, &hEvent, sizeof(hEvent), NULL, 0, &junk, NULL))
		{
			printf("Error %d in EVENT_REGISTER call\n", GetLastError());
			CloseHandle(hEvent);
			CloseHandle(hDevice);
			exit(1);
		}

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, &junk);

	printf("press any key to exit . . .\n");
	getch();

	CloseHandle(hEvent);
	CloseHandle(hDevice);

	return 0;
}



#define	IOCTL_INBUF_SIZE	512
#define	IOCTL_OUTBUF_SIZE	512

void Test_READ_DATA(void)
{

	CHAR	bufInput[IOCTL_INBUF_SIZE];		// Input to device
	CHAR	bufOutput[IOCTL_OUTBUF_SIZE];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	// Call device IO Control interface (READ_DATA) in driver
	bufOutput[0]='w';
	bufOutput[1]='a';
	bufOutput[2]='h';
	printf("Received data : ");
	if (!DeviceIoControl(hDevice,
						 READ_DATA,
						 bufInput,
						 IOCTL_INBUF_SIZE,
						 bufOutput,
						 IOCTL_OUTBUF_SIZE,
						 &nOutput,
						 NULL)
	   )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		exit(1);
	}
	printf("%s\n", bufOutput);
}

⌨️ 快捷键说明

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