📄 test_usbcounter.cpp
字号:
// Test_USBCounter.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 "..\USBCounterDeviceinterface.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include "..\USBCounterioctl.h"
#include "..\USBCounterDeviceinterface.h" // Has class GUID definition
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
void Test_READ_COUNTER(void);
void Test_RESET_COUNTER(void);
void CloseIfOpen(void);
// Global data
// Handle to device opened in driver.
//
HANDLE m_hDevice = INVALID_HANDLE_VALUE;
// Class GUID used to open device
//
GUID ClassGuid = USBCounterDevice_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[])
{
DWORD Error;
printf("Test application Test_USBCounter starting...\n");
m_hDevice = OpenByInterface( &ClassGuid, 0, &Error);
if (m_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_RESET_COUNTER();
Test_READ_COUNTER();
return 0;
}
////////////////////////////////////////////////////////////////////////
// CloseIfOpen
//
// Close the device if we previously opened a handle to it.
//
void CloseIfOpen(void)
{
if (m_hDevice != INVALID_HANDLE_VALUE)
{
// Close the handle to the driver
if (!CloseHandle(m_hDevice))
{
printf("ERROR: CloseHandle returns %0x.\n", GetLastError());
}
m_hDevice = INVALID_HANDLE_VALUE;
}
}
#define IOCTL_INBUF_SIZE 8
#define IOCTL_OUTBUF_SIZE 8
//=== Parameterized IOCTL Example ===
//void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal)
//{
// Function body same as other IOCTL handlers, with command line
// parameters 'nVal' and 'dwVal' available as input.
//}
////////////////////////////////////////////////////////////////////////
// Test_READ_COUNTER
//
// Test one Io Control Code
//
// TODO:
// Pass appropriate arguments to your device and check
// the return value
//
void Test_READ_COUNTER(void)
{
ULONG bufInput; // Input to device
UCHAR bufOutput; // Output from device
ULONG nOutput; // Count written to bufOutput
printf("Get counter, hit Ctrl_C to stop.\n");
while( 1 )
{
// Call device IO Control interface (THERMOMETER_READ_DATA) in driver
printf("Issuing Ioctl to device - ");
if (!DeviceIoControl(m_hDevice,
READ_COUNTER,
&bufInput,
sizeof(bufInput),
&bufOutput,
sizeof(bufOutput),
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.\n", GetLastError());
Exit(1);
}
printf("The counter is %d .\n", bufOutput);
// Sleep for a second
Sleep(1000);
}
}
////////////////////////////////////////////////////////////////////////
// Test_RESET_COUNTER
//
// Test one Io Control Code
//
// TODO:
// Pass appropriate arguments to your device and check
// the return value
//
void Test_RESET_COUNTER(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 (USBCOUNTER_RESET) in driver
printf("Issuing Ioctl to device - Reset counter\n");
if (!DeviceIoControl(m_hDevice,
RESET_COUNTER,
bufInput,
IOCTL_INBUF_SIZE,
bufOutput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
Exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -