| 1 | /* $Id: testdrv.cpp,v 1.3 2001-09-05 12:52:27 bird Exp $ */ | 
|---|
| 2 | //****************************************************************************** | 
|---|
| 3 | //****************************************************************************** | 
|---|
| 4 | // Test sample device driver dll | 
|---|
| 5 | // | 
|---|
| 6 | // The dll needs to be registered: | 
|---|
| 7 | // HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\Driver_Name | 
|---|
| 8 | //     string key 'DllName' with path of driver dll | 
|---|
| 9 | //****************************************************************************** | 
|---|
| 10 | //****************************************************************************** | 
|---|
| 11 | #define INCL_DOSPROFILE | 
|---|
| 12 | #define INCL_DOSDEVICES | 
|---|
| 13 | #define INCL_DOSDEVIOCTL | 
|---|
| 14 | #define INCL_GPI | 
|---|
| 15 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 16 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 17 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 18 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 19 | #include <os2wrap.h>             //Odin32 OS/2 api wrappers | 
|---|
| 20 |  | 
|---|
| 21 | #include <winconst.h> | 
|---|
| 22 | #include <win32type.h> | 
|---|
| 23 | #include <win32api.h> | 
|---|
| 24 | #include <misc.h> | 
|---|
| 25 | #include <win\winioctl.h> | 
|---|
| 26 |  | 
|---|
| 27 | #define TESTDRV_CATEGORY    0x40 | 
|---|
| 28 |  | 
|---|
| 29 | //****************************************************************************** | 
|---|
| 30 | //****************************************************************************** | 
|---|
| 31 | HANDLE WIN32API DrvOpen(DWORD dwAccess, DWORD dwShare) | 
|---|
| 32 | { | 
|---|
| 33 | APIRET rc; | 
|---|
| 34 | HFILE  hfFileHandle   = 0L;     /* Handle for file being manipulated */ | 
|---|
| 35 | ULONG  ulAction       = 0;      /* Action taken by DosOpen */ | 
|---|
| 36 | ULONG  sharetype = 0; | 
|---|
| 37 |  | 
|---|
| 38 | if(dwAccess & (GENERIC_READ_W | GENERIC_WRITE_W)) | 
|---|
| 39 | sharetype |= OPEN_ACCESS_READWRITE; | 
|---|
| 40 | else | 
|---|
| 41 | if(dwAccess & GENERIC_WRITE_W) | 
|---|
| 42 | sharetype |= OPEN_ACCESS_WRITEONLY; | 
|---|
| 43 |  | 
|---|
| 44 | if(dwShare == 0) | 
|---|
| 45 | sharetype |= OPEN_SHARE_DENYREADWRITE; | 
|---|
| 46 | else | 
|---|
| 47 | if(dwShare & (FILE_SHARE_READ_W | FILE_SHARE_WRITE_W)) | 
|---|
| 48 | sharetype |= OPEN_SHARE_DENYNONE; | 
|---|
| 49 | else | 
|---|
| 50 | if(dwShare & FILE_SHARE_WRITE_W) | 
|---|
| 51 | sharetype |= OPEN_SHARE_DENYREAD; | 
|---|
| 52 | else | 
|---|
| 53 | if(dwShare & FILE_SHARE_READ_W) | 
|---|
| 54 | sharetype |= OPEN_SHARE_DENYWRITE; | 
|---|
| 55 |  | 
|---|
| 56 | tryopen: | 
|---|
| 57 | rc = DosOpen( "TESTDRV$",                     /* File path name */ | 
|---|
| 58 | &hfFileHandle,                  /* File handle */ | 
|---|
| 59 | &ulAction,                      /* Action taken */ | 
|---|
| 60 | 0, | 
|---|
| 61 | FILE_NORMAL, | 
|---|
| 62 | FILE_OPEN, | 
|---|
| 63 | sharetype, | 
|---|
| 64 | 0L);                            /* No extended attribute */ | 
|---|
| 65 |  | 
|---|
| 66 | if(rc == ERROR_TOO_MANY_OPEN_FILES) { | 
|---|
| 67 | ULONG CurMaxFH; | 
|---|
| 68 | LONG  ReqCount = 32; | 
|---|
| 69 |  | 
|---|
| 70 | rc = DosSetRelMaxFH(&ReqCount, &CurMaxFH); | 
|---|
| 71 | if(rc) { | 
|---|
| 72 | dprintf(("DosSetRelMaxFH returned %d", rc)); | 
|---|
| 73 | return rc; | 
|---|
| 74 | } | 
|---|
| 75 | dprintf(("DosOpen failed -> increased nr open files to %d", CurMaxFH)); | 
|---|
| 76 | goto tryopen; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if(rc == NO_ERROR) { | 
|---|
| 80 | return hfFileHandle; | 
|---|
| 81 | } | 
|---|
| 82 | else  return -1; | 
|---|
| 83 | } | 
|---|
| 84 | //****************************************************************************** | 
|---|
| 85 | //****************************************************************************** | 
|---|
| 86 | void WIN32API DrvClose(HANDLE hDevice) | 
|---|
| 87 | { | 
|---|
| 88 | DosClose(hDevice); | 
|---|
| 89 | } | 
|---|
| 90 | //****************************************************************************** | 
|---|
| 91 | //****************************************************************************** | 
|---|
| 92 | BOOL WIN32API DrvIOCtl(HANDLE hDevice, DWORD dwIoControlCode, | 
|---|
| 93 | LPVOID lpInBuffer, DWORD nInBufferSize, | 
|---|
| 94 | LPVOID lpOutBuffer, DWORD nOutBufferSize, | 
|---|
| 95 | LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped) | 
|---|
| 96 | { | 
|---|
| 97 | APIRET rc; | 
|---|
| 98 | ULONG ioctl; | 
|---|
| 99 |  | 
|---|
| 100 | switch(dwIoControlCode) { | 
|---|
| 101 | default: | 
|---|
| 102 | return FALSE; | 
|---|
| 103 | } | 
|---|
| 104 | dprintf(("DrvIOCtl func %x: %x %d %x %d %x %x", dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped)); | 
|---|
| 105 |  | 
|---|
| 106 | *lpBytesReturned = nOutBufferSize; | 
|---|
| 107 |  | 
|---|
| 108 | rc = DosDevIOCtl(hDevice, TESTDRV_CATEGORY, ioctl, | 
|---|
| 109 | lpInBuffer, nInBufferSize, &nInBufferSize, | 
|---|
| 110 | lpOutBuffer, nOutBufferSize, lpBytesReturned); | 
|---|
| 111 |  | 
|---|
| 112 | dprintf(("DrvIOCtl returned %d bytes returned %d", rc , (lpBytesReturned) ? *lpBytesReturned : 0)); | 
|---|
| 113 | return rc == NO_ERROR; | 
|---|
| 114 | } | 
|---|
| 115 | //****************************************************************************** | 
|---|
| 116 | //****************************************************************************** | 
|---|
| 117 |  | 
|---|