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

📄 test_charsample.cpp

📁 武安河 电子书所副代码,包括windows驱动编程的基本方面
💻 CPP
字号:
// Test_CharSample.cpp
//
// Generated by DriverWizard version DriverStudio 3.1.0 (Build 1722)
//
// 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 "..\CharSampleDeviceinterface.h"

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

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

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

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

void CloseIfOpen(void);

// Global data

// Handle to device opened in driver.
//
HANDLE	hDevice = INVALID_HANDLE_VALUE;

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

////////////////////////////////////////////////////////////////////////
// Exit
//
//		Print a message and exit
//
   void Exit(int res)
{
	printf("Exiting...\n\n");
	CloseIfOpen();
	exit(res);
}


////////////////////////////////////////////////////////////////////////
// Main entry point
//
//
int __cdecl main(int argc, char *argv[])
{
//=== Parameterized IOCTL Example ===
//	int		nVal;
//	ULONG	dwVal;
	DWORD	Error;

	printf("Test application Test_CharSample 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");
	}

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

	printf("请输入三个数字(0-9)\n"); 
l0:	bufInput[0]=_getch();
	if ((bufInput[0]<'0') || (bufInput[0]>'9')) goto l0;
	_putch(bufInput[0]);
l1:	bufInput[1]=_getch();
	if ((bufInput[1]<'0') || (bufInput[1]>'9')) goto l1;
	_putch(bufInput[1]);
l2:	bufInput[2]=_getch();
	if ((bufInput[2]<'0') || (bufInput[2]>'9')) goto l2;
	_putch(bufInput[2]);
	// Call device IO Control interface (CHARSAMPLE_IOCTL_800) in driver
	if (!DeviceIoControl(hDevice,
						 CHARSAMPLE_IOCTL_800,
						 bufInput,
						 3,
						 bufOutput,
						 6,
						 &nOutput,
						 NULL)
	   )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	printf(":"); 
	_putch(bufOutput[0]); 
	_putch(bufOutput[1]); 
	_putch(bufOutput[2]); 
	_putch(bufOutput[3]); 
	_putch(bufOutput[4]); 
	_putch(bufOutput[5]); 
	printf("\n"); 

	CloseIfOpen();
	return 0;
}


////////////////////////////////////////////////////////////////////////
// CloseIfOpen
//
//		Close the device if we previously opened a handle to it.
//
void CloseIfOpen(void)
{
	if (hDevice != INVALID_HANDLE_VALUE)
	{
		// Close the handle to the driver
		if (!CloseHandle(hDevice))
		{
			printf("ERROR: CloseHandle returns %0x.\n", GetLastError());
		}
		hDevice = INVALID_HANDLE_VALUE;
	}
}

⌨️ 快捷键说明

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