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

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "initguid.h"
  4. #include "util.h"
  5. //=--------------------------------------------------------------------------=
  6. // miscellaneous [useful] numerical constants
  7. //=--------------------------------------------------------------------------=
  8. // the length of a guid once printed out with -'s, leading and trailing bracket,
  9. // plus 1 for NULL
  10. //
  11. #define GUID_STR_LEN    40
  12. HANDLE g_hHeap;
  13. //=--------------------------------------------------------------------------=
  14. // DeleteKeyAndSubKeys
  15. //=--------------------------------------------------------------------------=
  16. // delete's a key and all of it's subkeys.
  17. //
  18. // Parameters:
  19. //    HKEY                - [in] delete the descendant specified
  20. //    LPSTR               - [in] i'm the descendant specified
  21. //
  22. // Output:
  23. //    BOOL                - TRUE OK, FALSE baaaad.
  24. //
  25. // Notes:
  26. //    - I don't feel too bad about implementing this recursively, since the
  27. //      depth isn't likely to get all the great.
  28. //    - RegDeleteKey() works recursivly on Win9x, but not NT
  29. //
  30. BOOL DeleteKeyAndSubKeys
  31. (
  32.     HKEY    hkIn,
  33.     LPSTR   pszSubKey
  34. )
  35. {
  36.     HKEY  hk;
  37.     TCHAR szTmp[MAX_PATH];
  38.     DWORD dwTmpSize;
  39.     long  l;
  40.     BOOL  f;
  41.     int   x;
  42.     l = RegOpenKeyEx(hkIn, pszSubKey, 0, KEY_ALL_ACCESS, &hk);
  43.     if (l != ERROR_SUCCESS) return FALSE;
  44.     // loop through all subkeys, blowing them away.
  45.     //
  46.     f = TRUE;
  47.     x = 0;
  48.     while (f) {
  49.         dwTmpSize = MAX_PATH;
  50.         l = RegEnumKeyEx(hk, x, szTmp, &dwTmpSize, 0, NULL, NULL, NULL);
  51.         if (l != ERROR_SUCCESS) break;
  52.         f = DeleteKeyAndSubKeys(hk, szTmp);
  53.         x++;
  54.     }
  55.     // there are no subkeys left, [or we'll just generate an error and return FALSE].
  56.     // let's go blow this dude away.
  57.     //
  58.     RegCloseKey(hk);
  59.     l = RegDeleteKey(hkIn, pszSubKey);
  60.     return (l == ERROR_SUCCESS) ? TRUE : FALSE;
  61. }
  62. //=--------------------------------------------------------------------------=
  63. // StringFromGuid
  64. //=--------------------------------------------------------------------------=
  65. // returns an ANSI string from a CLSID or GUID
  66. //
  67. // Parameters:
  68. //    REFIID               - [in]  clsid to make string out of.
  69. //    LPSTR                - [in]  buffer in which to place resultant GUID.
  70. //
  71. // Output:
  72. //    int                  - number of chars written out.
  73. //
  74. // Notes:
  75. //
  76. int StringFromGuid
  77. (
  78.     const CLSID*   piid,
  79.     LPTSTR   pszBuf
  80. )
  81. {
  82.     return wsprintf(pszBuf, TEXT("{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"), piid->Data1,
  83.             piid->Data2, piid->Data3, piid->Data4[0], piid->Data4[1], piid->Data4[2],
  84.             piid->Data4[3], piid->Data4[4], piid->Data4[5], piid->Data4[6], piid->Data4[7]);
  85. }
  86. // Copy WideStr to another on Win95
  87. void CopyWideStr(LPWSTR pwszTarget, LPWSTR pwszSource)
  88. {
  89.    while(*pwszSource != 0)
  90.    {
  91.       *pwszTarget = *pwszSource;   
  92.       pwszSource++;
  93.       pwszTarget++;
  94.    }
  95.    *pwszTarget = 0;
  96. }
  97. //=--------------------------------------------------------------------------=
  98. // MakeWideFromAnsi
  99. //=--------------------------------------------------------------------------=
  100. // given a string, make a BSTR out of it.
  101. //
  102. // Parameters:
  103. //    LPSTR         - [in]
  104. //    BYTE          - [in]
  105. //
  106. // Output:
  107. //    LPWSTR        - needs to be cast to final desired result
  108. //
  109. // Notes:
  110. //
  111. LPWSTR MakeWideStrFromAnsi(LPSTR psz)
  112. {
  113.     LPWSTR pwsz;
  114.     int i;
  115.     // arg checking.
  116.     //
  117.     if (!psz) return NULL;
  118.     // compute the length of the required BSTR
  119.     //
  120.     if ((i = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0)) <= 0)
  121.         return NULL;
  122.     // allocate the widestr, +1 for terminating null
  123.     //
  124.     pwsz = (LPWSTR) CoTaskMemAlloc(i * sizeof(WCHAR));
  125.     
  126.     if (!pwsz) return NULL;
  127.     MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i);
  128.     pwsz[i - 1] = 0;
  129.     return pwsz;
  130. }
  131. LPSTR MakeAnsiStrFromWide(LPWSTR pwsz)
  132. {
  133.     LPSTR psz;
  134.     int i;
  135.     // arg checking.
  136.     //
  137.     if (!pwsz)
  138.         return NULL;
  139.     // compute the length of the required BSTR
  140.     //
  141.     if ((i = WideCharToMultiByte(CP_ACP, 0, pwsz, -1, NULL, 0, NULL, NULL)) <= 0)
  142.         return NULL;
  143.     
  144.     // allocate the ansistr, +1 for terminating null
  145.     //
  146.     psz = (LPSTR) CoTaskMemAlloc(i * sizeof(CHAR));
  147.     if (!psz) return NULL;
  148.     WideCharToMultiByte(CP_ACP, 0, pwsz, -1, psz, i, NULL, NULL);
  149.     psz[i - 1] = 0;
  150.     return psz;
  151. }