www.pudn.com > windows2000XP_WDM_DeviceDriverDevelopment_WuAnHe_C > Test_CharSample.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  
#include  
#include  
#include  
 
#include  
#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; 
	} 
}