WinIo.cpp
上传用户:ason123
上传日期:2010-03-31
资源大小:177k
文件大小:3k
源码类别:

并口编程

开发平台:

C++ Builder

  1. // ---------------------------------------------------- //
  2. //                      WinIo v2.0                      //
  3. //  Direct Hardware Access Under Windows 9x/NT/2000/XP  //
  4. //           Copyright 1998-2002 Yariv Kaplan           //
  5. //               http://www.internals.com               //
  6. // ---------------------------------------------------- //
  7. #include <windows.h>
  8. #include <winioctl.h>
  9. #include "k32call.h"
  10. #include "phys32.h"
  11. #include "..drvntwinio_nt.h"
  12. #include "winio.h"
  13. HANDLE hDriver = INVALID_HANDLE_VALUE;
  14. bool IsNT;
  15. bool IsWinIoInitialized = false;
  16. char szWinIoDriverPath[MAX_PATH];
  17. bool IsWinNT()
  18. {
  19.   OSVERSIONINFO OSVersionInfo;
  20.   OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  21.   GetVersionEx(&OSVersionInfo);
  22.   return OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT;
  23. }
  24. bool GetDriverPath()
  25. {
  26.   PSTR pszSlash;
  27.   if (!GetModuleFileName(GetModuleHandle(NULL), szWinIoDriverPath, sizeof(szWinIoDriverPath)))
  28.     return false;
  29.   pszSlash = strrchr(szWinIoDriverPath, '\');
  30.   if (pszSlash)
  31.     pszSlash[1] = 0;
  32.   else
  33.     return false;
  34.   strcat(szWinIoDriverPath, "winio.sys");
  35.   return true;
  36. }
  37. bool _stdcall InitializeWinIo()
  38. {
  39.   bool bResult;
  40.   DWORD dwBytesReturned;
  41.   IsNT = IsWinNT();
  42.   if (IsNT)
  43.   {
  44.     hDriver = CreateFile("\\.\WINIO",
  45.                          GENERIC_READ | GENERIC_WRITE,
  46.                          0,
  47.                          NULL,
  48.                          OPEN_EXISTING,
  49.                          FILE_ATTRIBUTE_NORMAL,
  50.                          NULL);
  51.     // If the driver is not running, install it
  52.     if (hDriver == INVALID_HANDLE_VALUE)
  53.     {
  54.       GetDriverPath();
  55.       bResult = InstallWinIoDriver(szWinIoDriverPath, true);
  56.       if (!bResult)
  57.         return false;
  58.       bResult = StartWinIoDriver();
  59.       if (!bResult)
  60.         return false;
  61.       hDriver = CreateFile("\\.\WINIO",
  62.                            GENERIC_READ | GENERIC_WRITE,
  63.                            0,
  64.                            NULL,
  65.                            OPEN_EXISTING,
  66.                            FILE_ATTRIBUTE_NORMAL,
  67.                            NULL);
  68.       if (hDriver == INVALID_HANDLE_VALUE)
  69.         return false;
  70.     }
  71.     // Enable I/O port access for this process
  72.     if (!DeviceIoControl(hDriver, IOCTL_WINIO_ENABLEDIRECTIO, NULL,
  73.                          0, NULL, 0, &dwBytesReturned, NULL))
  74.       return false;
  75.   }
  76.   else
  77.   {
  78.     VxDCall = (DWORD (WINAPI *)(DWORD,DWORD,DWORD))GetK32ProcAddress(1);
  79.     hDriver = CreateFile("\\.\WINIO.VXD", 0, 0, 0, CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0);
  80.     if (hDriver == INVALID_HANDLE_VALUE)
  81.       return false;
  82.   }
  83.   IsWinIoInitialized = true;
  84.   return true;
  85. }
  86. void _stdcall ShutdownWinIo()
  87. {
  88.   DWORD dwBytesReturned;
  89.   if (IsNT)
  90.   {
  91.     if (hDriver != INVALID_HANDLE_VALUE)
  92.     {
  93.       // Disable I/O port access
  94.       DeviceIoControl(hDriver, IOCTL_WINIO_DISABLEDIRECTIO, NULL,
  95.                       0, NULL, 0, &dwBytesReturned, NULL);
  96.       CloseHandle(hDriver);
  97.     }
  98.     RemoveWinIoDriver();
  99.   }
  100.   else
  101.     CloseHandle(hDriver);
  102.   IsWinIoInitialized = false;
  103. }