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.

75 lines
1.8 KiB

  1. //
  2. // MODULE: ComGlobals.cpp
  3. //
  4. // PURPOSE: Global functions that are handy to have.
  5. //
  6. // PROJECT: Local Troubleshooter Launcher for the Device Manager
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-633-4743 [email protected]
  9. //
  10. // AUTHOR: Richard Meadows
  11. //
  12. // ORIGINAL DATE: 2-26-98
  13. //
  14. //
  15. // Version Date By Comments
  16. //--------------------------------------------------------------------
  17. // V0.1 - RM Original
  18. ///////////////////////
  19. #include "stdafx.h"
  20. #include "ComGlobals.h"
  21. bool BSTRToTCHAR(LPTSTR szChar, BSTR bstr, int CharBufSize)
  22. {
  23. int x = 0;
  24. while(x < CharBufSize)
  25. {
  26. szChar[x] = (TCHAR) bstr[x];
  27. if (NULL == szChar[x])
  28. break;
  29. x++;
  30. }
  31. return x < CharBufSize;
  32. }
  33. bool ReadRegSZ(HKEY hRootKey, LPCTSTR szKey, LPCTSTR szValue, LPTSTR szBuffer, DWORD *pdwBufSize)
  34. {
  35. HKEY hKey;
  36. DWORD dwType = REG_SZ;
  37. DWORD dwBufSize = *pdwBufSize;
  38. LPTSTR szUnExpanded = new TCHAR[dwBufSize];
  39. if (NULL == szUnExpanded)
  40. return false;
  41. __try
  42. {
  43. if(ERROR_SUCCESS != RegOpenKeyEx(hRootKey, szKey, NULL, KEY_READ, &hKey))
  44. return false;
  45. if (ERROR_SUCCESS != RegQueryValueEx(hKey, szValue, NULL, &dwType,
  46. (PBYTE) szUnExpanded, pdwBufSize))
  47. {
  48. RegCloseKey(hKey);
  49. return false;
  50. }
  51. RegCloseKey(hKey);
  52. if (REG_EXPAND_SZ == dwType || dwType == REG_SZ) // NT 5.0 beta bug requires all strings to be expanded.
  53. {
  54. DWORD dwBytesUsed;
  55. dwBytesUsed = ExpandEnvironmentStrings(szUnExpanded, szBuffer, dwBufSize); // The value returned by ExpandEnviromentStrings is larger than the required size.
  56. if (0 == dwBytesUsed)
  57. return false;
  58. *pdwBufSize = dwBytesUsed;
  59. if (dwBytesUsed > dwBufSize)
  60. return false;
  61. }
  62. else
  63. {
  64. _tcsncpy(szBuffer, szUnExpanded, dwBufSize);
  65. }
  66. }
  67. __finally
  68. {
  69. if (NULL != szUnExpanded)
  70. delete [] szUnExpanded;
  71. }
  72. return true;
  73. }