📄 vbuscon.cpp
字号:
// vbuscon.cpp
//
// Generated by DriverWizard version DriverStudio 2.7.0 (Build 554)
//
// 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.
//
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include "..\vbusioctl.h"
#include <initguid.h>
#include "..\vinterface.h" // Has class GUID definition
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(const GUID* pClassGuid, DWORD instance, PDWORD pError);
// Prototypes
void Usage(void);
void AddDevice(ULONG id);
void DeleteDevice(ULONG id);
void ReadDevice();
void WriteDevice(char* sz);
////////////////////////////////////////////////////////////////////////
// Main entry point
//
//
int __cdecl main(int argc, char* argv[])
{
printf("Test application vbuscon starting...\n");
do
{
if ( argc == 2 )
{
if ( _stricmp("-r", argv[1]) == 0 )
{
ReadDevice();
break;
}
}
else if ( argc == 3 )
{
DWORD id;
if ( _stricmp("-a", argv[1]) == 0 )
{
id = atoi(argv[2]);
AddDevice(id);
break;
}
else if ( _stricmp("-d", argv[1]) == 0 )
{
id = atoi(argv[2]);
DeleteDevice(id);
break;
}
else if ( _stricmp("-w", argv[1]) == 0 )
{
WriteDevice(argv[2]);
break;
}
}
Usage();
} while ( false );
return 0;
}
////////////////////////////////////////////////////////////////////////
// Usage
//
// Print a usage message describing arguments to this program
//
void Usage(void)
{
printf(" -a <id> add a new device\n");
printf(" -d <id> remove an existing device\n");
printf(" -r read data from an existing device\n");
printf(" -w write data to an existing device\n");
}
////////////////////////////////////////////////////////////////////////
// AddDevice
//
// Sends IOCTL to a driver to create a new device with specified id
//
void AddDevice(ULONG id)
{
printf("AddDevice %d\n", id);
DWORD dwError;
HANDLE hDevice = OpenByInterface(&VBUSDEVICE_CLASS_GUID, 0, &dwError);
if ( hDevice != INVALID_HANDLE_VALUE )
{
ULONG nOutput;
if ( !DeviceIoControl(hDevice, VBUS_IOCTL_ADD_DEVICE, &id, sizeof(ULONG), NULL, 0, &nOutput,NULL) )
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
CloseHandle(hDevice);
}
else
{
printf("Failed to open a device %0x.\n", dwError);
}
}
////////////////////////////////////////////////////////////////////////
// DeleteDevice
//
// Sends IOCTL to a driver to remove an existing device with specified id
//
void DeleteDevice(ULONG id)
{
printf("DeleteDevice %d\n", id);
DWORD dwError;
HANDLE hDevice = OpenByInterface(&VBUSDEVICE_CLASS_GUID, 0, &dwError);
if ( hDevice != INVALID_HANDLE_VALUE )
{
ULONG nOutput;
if ( !DeviceIoControl(hDevice, VBUS_IOCTL_DELETE_DEVICE, &id, sizeof(ULONG), NULL, 0, &nOutput,NULL) )
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
CloseHandle(hDevice);
}
else
{
printf("Failed to open a device %0x.\n", dwError);
}
}
////////////////////////////////////////////////////////////////////////
// ReadDevice
//
// Reads data from a device
//
void ReadDevice()
{
printf("ReadDevice\n");
DWORD dwError;
HANDLE hDevice = OpenByInterface(&VREADDEVICE_CLASS_GUID, 0, &dwError);
if ( hDevice != INVALID_HANDLE_VALUE )
{
char buffer[16];
ULONG nRead = 0;
if ( ReadFile(hDevice, buffer, sizeof(buffer), &nRead, NULL) )
{
for (ULONG i = 0; i < nRead; i++)
{
printf("%2.2x ", (ULONG)buffer[i]);
}
printf("\n");
}
else
{
printf("ERROR: ReadFile returns %0x.", GetLastError());
}
CloseHandle(hDevice);
}
else
{
printf("Failed to open a device %0x.\n", dwError);
}
}
////////////////////////////////////////////////////////////////////////
// WriteDevice
//
// Write data to a device
//
void WriteDevice(char* sz)
{
printf("WriteDevice\n");
DWORD dwError;
HANDLE hDevice = OpenByInterface(&VWRITEDEVICE_CLASS_GUID, 0, &dwError);
if ( hDevice != INVALID_HANDLE_VALUE )
{
ULONG nWritten = 0;
if ( !WriteFile(hDevice, sz, (ULONG)strlen(sz) + 1, &nWritten, NULL) )
{
printf("ERROR: WriteFile returns %0x.", GetLastError());
}
CloseHandle(hDevice);
}
else
{
printf("Failed to open a device %0x.\n", dwError);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -