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

📄 test_regsample.cpp

📁 windows 2000/XP WDM设备驱动程序开发 附书光盘 武安河著
💻 CPP
字号:
// Test_RegSample.cpp
//
// Generated by DriverWizard version DriverStudio 2.6.0 (Build 336)
//
// 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 "..\RegSampleDeviceinterface.h"

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

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

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

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

typedef void VOIDFUNC();

void Test_READ_DWORD(void);
void Test_READ_STRING(void);
void Test_READ_BOOLEAN(void);

void CloseIfOpen(void);

// Global data

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

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

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

////////////////////////////////////////////////////////////////////////
// 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;
	}
}

////////////////////////////////////////////////////////////////////////
// Main entry point
int __cdecl main(int argc, char *argv[])
{
	DWORD	Error;

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

	// Parse the command line
	Test_READ_DWORD();
	Test_READ_STRING();
	Test_READ_BOOLEAN();

	CloseIfOpen();
	return 0;
}

void Test_READ_DWORD(void)
{
	ULONG	bufOutput;	// Output from device
	ULONG	nOutput;	// Count written to bufOutput

	// Call device IO Control interface (READ_DWORD) in driver
	if (!DeviceIoControl(hDevice,
						 READ_DWORD,
						 NULL,
						 0,
						 &bufOutput,
						 sizeof(ULONG),
						 &nOutput,
						 NULL)
	   )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	else printf("READ_DWORD:0x%x\n",bufOutput);
}

void Test_READ_STRING(void)
{
	CHAR	bufOutput[512];	// Output from device
	ULONG	nOutput;		// Count written to bufOutput

	// Call device IO Control interface (READ_STRING) in driver
	if (!DeviceIoControl(hDevice,
						 READ_STRING,
						 NULL,
						 0,
						 bufOutput,
						 512,
						 &nOutput,
						 NULL)
	   )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	else wprintf(L"READ_STRING:%s\n",bufOutput);
}

void Test_READ_BOOLEAN(void)
{
	ULONG	bufOutput;	// Output from device
	ULONG	nOutput;	// Count written to bufOutput

	// Call device IO Control interface (READ_BOOLEAN) in driver
	if (!DeviceIoControl(hDevice,
						 READ_BOOLEAN,
						 NULL,
						 0,
						 &bufOutput,
						 sizeof(ULONG),
						 &nOutput,
						 NULL)
	   )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	else printf("READ_BOOLEAN:%x\n",bufOutput);
}

⌨️ 快捷键说明

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