Conclusion
This chapter shows how to open a connection to a Wdm1 device using device interfaces. The Wdm1Test user mode program puts the Wdm1 driver through its paces.
Chapter 7 shows how Wdm1 implements the read, write, and IOCTL calls. However, before I continue looking at the driver, the next chapter describes how to test and debug drivers. It explains how this book uses the DebugPrint software to see what is going on in a driver.
Listing 5.7 Wdm1Test.cpp
///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1Test example
///////////////////////////////////////////////////////////////////////
//WdmlTest.cpp:Win32 console application to exercise Wdm1 devices
///////////////////////////////////////////////////////////////////////
//mainProgram main line
//GetDeviceVialnterfaceOpen a handle via a device interface
///////////////////////////////////////////////////////////////////////
//Version history
//27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#include 'windows.h'
#include 'C:98ddkincwin98setupapi.h'// VC++ 5 one is out of date
#include 'stdio.h'
#include 'initguid.h'
#include '..sysGUIDs.h'
#include 'winioctl.h'
#include '..sysIoctl.h'
HANDLE GetDeviceViaInterface(GUID* pGuid, DWORD instance);
int main(int argc, char* argv[]) {
int TestNo = 1;
//////////////////////////////////////////////////////////////////////
// Open device
printf('
Test %d
',TestNo++);
HANDLE hWdm1 = GetDeviceViaInterface((LPGUID)&WDM1_GUID,0);
if (hWdml==NULL) {
printf('XXX Could not find open Wdm1 device
');
return 1;
}
printf(' Opened OK
');
//////////////////////////////////////////////////////////////////////
// Read first ULONG that's left in buffer
printf('
Test %d
',TestNo++);
DWORD TxdBytes;
ULONG Rvalue =0;
if (!ReadFile(hWdm1, &Rvalue, 4, &TxdBytes, NULL)) printf('XXX Could not read value %d
', GetLastError());
else if(TxdBytes==4) printf(' Read successfully read stored value of 0x%X
',Rvalue);
else printf('XXX Wrong number of bytes read: %d
',TxdBytes);
//////////////////////////////////////////////////////////////////////
// Write 0x12345678
printf('
Test M
' ,TestNo++);
ULONG Wvalue = 0x12345678;
if (!WriteFile(hWdm1, &Wvalue, 4, &TxdBytes, NULL)) printf('XXX Could not write %X
',Wvalue);
else if (TxdBytes==4) printf(' Write 0x12345678 succeeded
');
else printf('XXX Wrong number of bytes written: %d
',TxdBytes);
//////////////////////////////////////////////////////////////////////
// Set file pointer
printf('
Test %d
',TestNo++);
DWORD dwNewPtr = SetFilePointer(hWdrn1, 3, NULL, FILE_BEGIN);
if (dwNewPtr==0xFFFFFFFF) printf('XXX SetFilePointer failed %d
', GetLastError());
else printf(' SetFilePointer worked
');
//////////////////////////////////////////////////////////////////////
// Read
printf('
Test %d
',TestNo++);
Rvalue = 0;
if (!ReadFile(hWdm1, &Rvalue, 1, &TxdBytes, NULL)) printf('XXX Could not read value
');
else if( TxdBytes==1) printf(' Read successfully read stored value of 0x%X
',Rvalue);
else printf('XXX Wrong number of bytes read: %d
',TxdBytes);
//////////////////////////////////////////////////////////////////////
// Write
printf('
Test %d
',TestNo++);
if (!WriteFile(hWdm1, &Wvalue, 4, &TxdBytes, NULL)) printf('XXX Could not write %X
' ,Wvalue);
else if (TxdBytes==4) printf(' Write at new file pointer succeeded
');
else printf('XXX Wrong number of bytes written: %d
',TxdBytes);
//////////////////////////////////////////////////////////////////////
// Get buffer size
printf('
Test %d
',TestNo++);
ULONG BufferSize;
DWORD BytesReturned;
if (!DeviceIoControl(hWdm1, IOCTL_WDM1_GET_BUFFER_SIZE,
NULL, 0,// Input
&BufferSize, sizeof(ULONG),// Output
&BytesReturned, NULL)) printf('XXX Could not get buffer size
');
else printf(' Buffer size is %i (%d bytes returned)
',BufferSize,BytesReturned);
//////////////////////////////////////////////////////////////////////