Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.2 KiB

  1. //
  2. // Kernel-mode utility classes for minidrivers
  3. //
  4. #ifndef _UTIL_H_
  5. #define _UTIL_H_
  6. //
  7. // _purecall must be defined in the main compile unit, not in a library
  8. //
  9. // the driver must define DECLARE_PURECALL once exactly somewhere
  10. #ifdef DECLARE_PURECALL
  11. // to handle pure virtual functions
  12. // -- this is needed to complete the link, and can be
  13. // called eg when a base class destructor calls a method
  14. // that is only defined in a derived class.
  15. //
  16. // Alas it does not seem to be loaded correctly from a library
  17. // and thus must be in the main module
  18. extern "C" int _cdecl _purecall()
  19. {
  20. ASSERT(FALSE);
  21. return 0;
  22. }
  23. #endif
  24. #ifndef NO_GLOBAL_FUNCTION
  25. extern BOOL StartThread(HANDLE hHandle, void(*p_Function)(void *), PVOID p_Context);
  26. extern BOOL StopThread();
  27. extern void Delay(int iTime);
  28. extern PVOID AllocateFixedMemory(UINT uiSize);
  29. extern void FreeFixedMemory(PVOID p_vBuffer);
  30. extern void MemoryCopy(VOID *p_Destination, CONST VOID *p_Source, ULONG ulLength);
  31. extern NTSTATUS GetRegistryValue(IN HANDLE Handle, IN PWCHAR KeyNameString,
  32. IN ULONG KeyNameStringLength, IN PWCHAR Data, IN ULONG DataLength);
  33. extern BOOL StringsEqual(PWCHAR pwc1, PWCHAR pwc2);
  34. extern BOOL ConvertToNumber(PWCHAR sLine, PULONG pulNumber);
  35. extern BOOL ConvertNumberToString(PWCHAR sLine, ULONG ulNumber) ;
  36. #endif // NO_GLOBAL_FUNCTION
  37. // mutex wrapper and auto lock/unlock class
  38. class CMutex
  39. {
  40. public:
  41. CMutex(ULONG level = 1)
  42. {
  43. KeInitializeMutex(&m_Mutex, level);
  44. }
  45. void Lock() {
  46. KeWaitForSingleObject(&m_Mutex, Executive, KernelMode, false, NULL);
  47. }
  48. void Unlock() {
  49. KeReleaseMutex(&m_Mutex, false);
  50. }
  51. private:
  52. KMUTEX m_Mutex;
  53. };
  54. // use c++ ctor/dtor framework to ensure unlocking
  55. class CAutoMutex
  56. {
  57. public:
  58. CAutoMutex(CMutex* pLock)
  59. : m_pLock(pLock)
  60. {
  61. m_pLock->Lock();
  62. }
  63. ~CAutoMutex()
  64. {
  65. m_pLock->Unlock();
  66. }
  67. private:
  68. CMutex* m_pLock;
  69. };
  70. class CPhilTimer
  71. {
  72. public:
  73. CPhilTimer();
  74. ~CPhilTimer();
  75. BOOL Set(int iTimePeriod);
  76. void Cancel();
  77. BOOL Wait(int iTimeOut);
  78. private:
  79. KTIMER m_Timer;
  80. };
  81. #endif // _UTIL_H_