📄 test_timersample.cpp
字号:
// Test_TimerSample.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 "..\TimerSampleDeviceinterface.h"
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include "..\TimerSampleioctl.h"
#include "..\TimerSampleDeviceinterface.h" // Has class GUID definition
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
void Test_Start_1HzTimer(void);
void Test_Start_KTimer(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 = TimerSampleDevice_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_TimerSample 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");
}
Test_Start_1HzTimer();
Test_Start_KTimer();
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;
}
}
#define IOCTL_INBUF_SIZE 512
#define IOCTL_OUTBUF_SIZE 512
//=== 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_Start_1HzTimer
//
// Test one Io Control Code
//
// TODO:
// Pass appropriate arguments to your device and check
// the return value
//
void Test_Start_1HzTimer(void)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
// bufInput supplies data to the device
// bufOutput is written by the device to return data to this application
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 (Start_1HzTimer) in driver
printf("Start_1HzTimer\n");
if (!DeviceIoControl(hDevice,
Start_1HzTimer,
bufInput,
IOCTL_INBUF_SIZE,
bufOutput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
Exit(1);
}
}
////////////////////////////////////////////////////////////////////////
// Test_Start_KTimer
//
// Test one Io Control Code
//
// TODO:
// Pass appropriate arguments to your device and check
// the return value
//
void Test_Start_KTimer(void)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
// bufInput supplies data to the device
// bufOutput is written by the device to return data to this application
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 (Start_KTimer) in driver
printf("Start_KTimer\n");
if (!DeviceIoControl(hDevice,
Start_KTimer,
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 + -