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

📄 test_dmasample.cpp

📁 Windows 2000/XP WDM設備驅動程式開發 使用 Numega 公司出版的軟體快速建置驅動程式
💻 CPP
字号:
// Test_DMASample.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 "..\DMASampleDeviceinterface.h"

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

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

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

void CloseIfOpen(void);
void doRead(void);
void doWrite(void);

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

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


void Exit(int res)
{
	printf("Exiting...\n\n");
	CloseIfOpen();
	exit(res);
}


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

	printf("Test application Test_DMASample 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

	doWrite();
	doRead();

	return 0;
}

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

void doRead(void)
{
	char	*buf,*f;
	ULONG	nRead;
	int i,j;

	buf = (char *) malloc(32);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for read");
		Exit(1);
	}

	// Read data from driver
	printf("Reading from device - ");
	ReadFile(hDevice, buf, 32, &nRead, NULL);
	printf("%d bytes read from device (%d requested).\n", nRead, 32);

	// Print what was read
	f=buf;
	for (i=0;i<2;i++) {
	for (j=0;j<16;j++) {
        fprintf(stderr, "%02x ",*f++);
	};
        fprintf(stderr, "\n");
	};
	
	free(buf);
}

void doWrite(void)
{
	char	*buf,*f;
	ULONG	nWritten;
	int		i,j;

	buf = (char *) malloc(32);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for write");
		Exit(1);
	}

	for (i=0; i<32; i++)
	{
		buf[i] = i;
	}
	
	// Write data to driver
	printf("Writing to device - ");
	WriteFile(hDevice, buf, 32, &nWritten, NULL);
	printf("%d bytes written to device (%d attempted).\n", nWritten, 32);

	// Print what was written
	f=buf;
	for (i=0;i<2;i++) {
	for (j=0;j<16;j++) {
        fprintf(stderr, "%02x ",*f++);
	};
        fprintf(stderr, "\n");
	};

	free(buf);
}

⌨️ 快捷键说明

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