Êíèãà: Writing Windows WDM Device Drivers

Conclusion

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("nTest %dn",TestNo++);
 HANDLE hWdm1 = GetDeviceViaInterface((LPGUID)&WDM1_GUID,0);
 if (hWdml==NULL) {
  printf("XXX Could not find open Wdm1 devicen");
  return 1;
 }
 printf(" Opened OKn");
 //////////////////////////////////////////////////////////////////////
 // Read first ULONG that's left in buffer
 printf("nTest %dn",TestNo++);
 DWORD TxdBytes;
 ULONG Rvalue =0;
 if (!ReadFile(hWdm1, &Rvalue, 4, &TxdBytes, NULL)) printf("XXX Could not read value %dn", GetLastError());
 else if(TxdBytes==4) printf(" Read successfully read stored value of 0x%Xn",Rvalue);
 else printf("XXX Wrong number of bytes read: %dn",TxdBytes);
 //////////////////////////////////////////////////////////////////////
 // Write 0x12345678
 printf("nTest Mn" ,TestNo++);
 ULONG Wvalue = 0x12345678;
 if (!WriteFile(hWdm1, &Wvalue, 4, &TxdBytes, NULL)) printf("XXX Could not write %Xn",Wvalue);
 else if (TxdBytes==4) printf(" Write 0x12345678 succeededn");
 else printf("XXX Wrong number of bytes written: %dn",TxdBytes);
 //////////////////////////////////////////////////////////////////////
 // Set file pointer
 printf("nTest %dn",TestNo++);
 DWORD dwNewPtr = SetFilePointer(hWdrn1, 3, NULL, FILE_BEGIN);
 if (dwNewPtr==0xFFFFFFFF) printf("XXX SetFilePointer failed %dn", GetLastError());
 else printf(" SetFilePointer workedn");
 //////////////////////////////////////////////////////////////////////
 // Read
 printf("nTest %dn",TestNo++);
 Rvalue = 0;
 if (!ReadFile(hWdm1, &Rvalue, 1, &TxdBytes, NULL)) printf("XXX Could not read valuen");
 else if( TxdBytes==1) printf(" Read successfully read stored value of 0x%Xn",Rvalue);
 else printf("XXX Wrong number of bytes read: %dn",TxdBytes);
 //////////////////////////////////////////////////////////////////////
 // Write
 printf("nTest %dn",TestNo++);
 if (!WriteFile(hWdm1, &Wvalue, 4, &TxdBytes, NULL)) printf("XXX Could not write %Xn" ,Wvalue);
 else if (TxdBytes==4) printf(" Write at new file pointer succeededn");
 else printf("XXX Wrong number of bytes written: %dn",TxdBytes);
 //////////////////////////////////////////////////////////////////////
 // Get buffer size
 printf("nTest %dn",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 sizen");
 else printf(" Buffer size is %i (%d bytes returned)n",BufferSize,BytesReturned);
 //////////////////////////////////////////////////////////////////////
 // Get buffer size
 printf("nTest %dn",TestNo++);
 char* Buffer = new char[BufferSize+1];
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_GET_BUFFER,
  NULL, 0,// Input
  Buffer, BufferSize,// Output
  &BytesReturned, NULL)) printf("XXX Could not get buffern");
 else printf(" First DWORD of buffer is %08X (%d bytes returned)n",*((DWORD*)Buffer),Bytes Returned);
 ///////////////////////////////////////////////////////////////////////
 // Get too big a buffer size
 printf("nTest %dn",TestNo++);
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_GET_BUFFER,
  NULL, 0,// Input
  Buffer, BufferSize+1,// Output
  &BytesReturned, NULL)) printf(" Too big get buffer failed correctly %dn",GetLastError());
 else printf("XXX Too big get buffer unexpectedly succeededn");
 ///////////////////////////////////////////////////////////////////////
 // Zero all buffer bytes
 printf("nTest %dn",TestNo++);
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_ZERO_BUFFER,
  NULL, 0,// Input
  NULL, 0,// Output
  &BytesReturned, NULL)) printf("XXX Zero buffer failed %dn" ,GetLastError());
 else printf(" Zero buffer succeededn");
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_GET_BUFFER,
  NULL, 0,// Input
  Buffer, BufferSize,// Output
  &BytesReturned, NULL)) printf("XXX Could not get buffern");
 else printf(" First DWORD of buffer is %08X (%d bytes returned) n",*((DWORD*)Buffer),BytesReturned);
 ///////////////////////////////////////////////////////////////////////
 // Remove buffer
 printf("nTest %dn",TestNo++);
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_REMOVE_BUFFER,
  NULL, 0,// Input
  NULL, 0,// Output
  &BytesReturned, NULL)) printf("XXX Remove buffer failed %dn",GetLastError());
 else printf(" Remove buffer succeededn");
 if (!DeviceloControl(hWdm1, IOCTL_WDM1_GET_BUFFER_SIZE,
  NULL, 0,// Input
  &BufferSize, sizeof(ULONG),// Output
  &BytesReturned, NULL)) printf("XXX Could not get buffer sizen");
 else printf(" Buffer size is %i (%d bytes returned)n",BufferSize,BytesReturned);
 ///////////////////////////////////////////////////////////////////////
 // Unrecognised IOCTL
 printf("nTest %dn",TestNo++);
 if (!DeviceIoControl(hWdm1, IOCTL_WDM1_UNRECOGNISED,
  NULL, 0,// Input
  NULL, 0,1/ Output
  &BytesReturned, NULL)) printf(" Unrecognised IOCTL correctly failed %dn",GetLastError());
 else printf("XXX Unrecognised IOCTL unexpectedly succeededn");
 ///////////////////////////////////////////////////////////////////////
 // Write 0xabcdef01 to start of buffer
 printf("nTest %dn",TestNo++);
 dwNewPtr = SetFilePointer(hWdm1, 0, NULL, FILE_BEGIN);
 if (dwNewPtr==0xFFFFFFFF) printf("XXX SetFilePointer failed %dn",GetLastError());
 else printf(" SetFilePointer workedn");
 Wvalue = 0xabcdef01;
 if (!WriteFile(hWdm1, &Wvalue, 4, &TxdBytes, NULL)) printf("XXX Could not write %Xn", Wvalue);
 else if( TxdBytes==4) printf(" Write 0xabcdef01 succeededn");
 else printf("XXX Wrong number of bytes written: %dn",TxdBytes);
 /////////////////////////////////////////////////////////////////////////
 // Close device
 printf("nTest %dn",TestNo++);
 if (!CloseHandle(hWdm1)) printf("XXX CloseHandle failed %dn", GetLastError());
 else printf(" CloseHandle workedn");
 /////////////////////////////////////////////////////////////////////////
 delete Buffer;
 printf("nPress enter please");
 char line[80];
 gets(line);
 return 0;
}
//////////////////////////////////////////////////////////////////////////
//GetDeviceViaInterface:Open a handle via a device interface
HANDLE GetDeviceViaInterface(GUID* pGuid, DWORD instance) {
 // Get handle to relevant device information set
 HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
 if (info==INVALID_HANDLE_VALUE) {
  printf("No HDEVINFO available for this GUIDn");
  return NULL;
 }
 // Get interface data for the requested instance
 SP_INTERFACE_DEVICE_DATA ifdata;
 ifdata.cbSize = sizeof(ifdata);
 if (!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata)) {
  printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instancen");
  SetupDiDestroyDeviceInfoList(info);
  return NULL;
 }
 // Get size of symbolic link name
 DWORD ReqLen;
 SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
 PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
 if (ifDetail==NULL) {
  SetupDiDestroyDeviceInfoList(info);
  return NULL;
 }
 // Get symbolic link name
 ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
 if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) {
  SetupDiDestroyDeviceInfoList(info);
  delete ifDetail;
  return NULL;
 }
 printf("Symbolic link is %sn", ifDetail–>DevicePath);
 // Open file
 HANDLE rv = CreateFile(ifDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 delete ifDetail;
 SetupDiDestroyDeviceInfoList(info);
 return rv;
}
///////////////////////////////////////////////////////////////////////

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.036. Çàïðîñîâ Ê ÁÄ/Cache: 0 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç