util.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #ifndef _UTIL_H_
  2. #define GUID_STR_LEN 40
  3. #define BOOL unsigned int
  4. BOOL DeleteKeyAndSubKeys(HKEY hkIn, LPSTR pszSubKey);
  5. int StringFromGuid(const CLSID* piid, LPTSTR pszBuf);
  6. void * _cdecl operator new(size_t size);
  7. void   _cdecl operator delete(void *ptr);
  8. void * malloc(size_t n);
  9. void * calloc(size_t n, size_t s);
  10. void * realloc(void* p, size_t n);
  11. void   free(void* p);
  12. extern HANDLE g_hHeap;
  13. //
  14. // helper macros
  15. //
  16. #define RegCreate(hk, psz, phk) if (ERROR_SUCCESS != RegCreateKeyEx((hk), psz, 0, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, (phk), &dwDummy)) goto CleanUp
  17. #define RegSetStr(hk, psz) if (ERROR_SUCCESS != RegSetValueEx((hk), NULL, 0, REG_SZ, (BYTE*)(psz), lstrlen(psz)+1)) goto CleanUp
  18. #define RegSetStrValue(hk, pszStr, psz)    if(ERROR_SUCCESS != RegSetValueEx((hk), (const char *)(pszStr), 0, REG_SZ, (BYTE*)(psz), lstrlen(psz)+1)) goto CleanUp
  19. #define RegCloseK(hk) RegCloseKey(hk); hk = NULL
  20. #define RegOpenK(hk, psz, phk) if (ERROR_SUCCESS != RegOpenKeyEx(hk, psz, 0, KEY_ALL_ACCESS, phk)) return FALSE
  21. //=--------------------------------------------------------------------------=
  22. // allocates a temporary buffer that will disappear when it goes out of scope
  23. // NOTE: be careful of that -- make sure you use the string in the same or
  24. // nested scope in which you created this buffer. people should not use this
  25. // class directly.  use the macro(s) below.
  26. //
  27. class TempBuffer {
  28.   public:
  29.     TempBuffer(ULONG cBytes) {
  30.         m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : LocalAlloc(LMEM_FIXED, cBytes);
  31.         m_fHeapAlloc = (cBytes > 120);
  32.     }
  33.     ~TempBuffer() {
  34.         if (m_pBuf && m_fHeapAlloc) LocalFree(m_pBuf);
  35.     }
  36.     void *GetBuffer() {
  37.         return m_pBuf;
  38.     }
  39.   private:
  40.     void *m_pBuf;
  41.     // we'll use this temp buffer for small cases.
  42.     //
  43.     char  m_szTmpBuf[120];
  44.     unsigned m_fHeapAlloc:1;
  45. };
  46. void CopyWideStr(LPWSTR pwszTarget, LPWSTR pwszSource);
  47. //=--------------------------------------------------------------------------=
  48. // string helpers.
  49. //
  50. // given and ANSI String, copy it into a wide buffer.
  51. // be careful about scoping when using this macro!
  52. //
  53. // how to use the below two macros:
  54. //
  55. //  ...
  56. //  LPSTR pszA;
  57. //  pszA = MyGetAnsiStringRoutine();
  58. //  MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  59. //  MyUseWideStringRoutine(pwsz);
  60. //  ...
  61. //
  62. // similarily for MAKE_ANSIPTR_FROMWIDE.  note that the first param does not
  63. // have to be declared, and no clean up must be done.
  64. //
  65. #define MAKE_WIDEPTR_FROMANSI(ptrname, ansistr) 
  66.     long __l##ptrname = (lstrlen(ansistr) + 1) * sizeof(WCHAR); 
  67.     TempBuffer __TempBuffer##ptrname(__l##ptrname); 
  68.     MultiByteToWideChar(CP_ACP, 0, ansistr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); 
  69.     LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  70. #define MAKE_ANSIPTR_FROMWIDE(ptrname, widestr) 
  71.     long __l##ptrname = (lstrlenW(widestr) + 1) * sizeof(char); 
  72.     TempBuffer __TempBuffer##ptrname(__l##ptrname); 
  73.     WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); 
  74.     LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  75. LPWSTR MakeWideStrFromAnsi(LPSTR);
  76. #define _UTIL_H_
  77. #endif // _UTIL_H_