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.

173 lines
4.2 KiB

  1. /*
  2. * ole - Random OLE stuff
  3. *
  4. * Random shell stuff is here, too.
  5. */
  6. #include "tweakui.h"
  7. /*
  8. * InOrder - checks that i1 <= i2 < i3.
  9. */
  10. #define fInOrder(i1, i2, i3) ((unsigned)((i2)-(i1)) < (unsigned)((i3)-(i1)))
  11. LPMALLOC pmalloc;
  12. /*****************************************************************************
  13. *
  14. * Ole_Free
  15. *
  16. * Free memory via OLE.
  17. *
  18. *****************************************************************************/
  19. void PASCAL
  20. Ole_Free(LPVOID pv)
  21. {
  22. pmalloc->Free(pv);
  23. }
  24. /*****************************************************************************
  25. *
  26. * Ole_ToUnicode
  27. *
  28. * Convert an ANSI string to Unicode.
  29. *
  30. *****************************************************************************/
  31. int PASCAL
  32. Ole_ToUnicode(LPOLESTR lpos, LPCSTR psz)
  33. {
  34. return MultiByteToWideChar(CP_ACP, 0, psz, -1, lpos, MAX_PATH);
  35. }
  36. /*****************************************************************************
  37. *
  38. * Ole_FromUnicode
  39. *
  40. * Convert to an ANSI string from Unicode.
  41. *
  42. *****************************************************************************/
  43. int PASCAL
  44. Ole_FromUnicode(LPSTR psz, LPOLESTR lpos)
  45. {
  46. return WideCharToMultiByte(CP_ACP, 0, lpos, -1, psz, MAX_PATH, NULL, NULL);
  47. }
  48. /*****************************************************************************
  49. *
  50. * Ole_Init
  51. *
  52. * Initialize the OLE stuff.
  53. *
  54. *****************************************************************************/
  55. HRESULT PASCAL
  56. Ole_Init(void)
  57. {
  58. HRESULT hres;
  59. hres = SHGetMalloc(&pmalloc);
  60. if (SUCCEEDED(hres)) {
  61. return SHGetDesktopFolder(&psfDesktop);
  62. } else {
  63. return hres;
  64. }
  65. }
  66. /*****************************************************************************
  67. *
  68. * Ole_Term
  69. *
  70. * Clean up the OLE stuff.
  71. *
  72. *****************************************************************************/
  73. void PASCAL
  74. Ole_Term(void)
  75. {
  76. if (pmalloc) Ole_Release(pmalloc);
  77. if (psfDesktop) Ole_Release(psfDesktop);
  78. }
  79. /*****************************************************************************
  80. *
  81. * Ole_ParseHex
  82. *
  83. * Parse a hex string encoding cb bytes (at most 4), then
  84. * expect the tchDelim to appear afterwards. If chDelim is 0,
  85. * then no delimiter is expected.
  86. *
  87. * Store the result into the indicated LPBYTE (using only the
  88. * size requested), updating it, and return a pointer to the
  89. * next unparsed character, or 0 on error.
  90. *
  91. * If the incoming pointer is also 0, then return 0 immediately.
  92. *
  93. *****************************************************************************/
  94. LPCTSTR PASCAL
  95. Ole_ParseHex(LPCTSTR ptsz, LPBYTE *ppb, int cb, TCH tchDelim)
  96. {
  97. if (ptsz) {
  98. int i = cb * 2;
  99. DWORD dwParse = 0;
  100. do {
  101. DWORD uch;
  102. uch = (unsigned char)*ptsz - '0';
  103. if (uch < 10) { /* a decimal digit */
  104. } else {
  105. uch = (*ptsz | 0x20) - 'a';
  106. if (uch < 6) { /* a hex digit */
  107. uch += 10;
  108. } else {
  109. return 0; /* Parse error */
  110. }
  111. }
  112. dwParse = (dwParse << 4) + uch;
  113. ptsz++;
  114. } while (--i);
  115. if (tchDelim && *ptsz++ != tchDelim) return 0; /* Parse error */
  116. for (i = 0; i < cb; i++) {
  117. (*ppb)[i] = ((LPBYTE)&dwParse)[i];
  118. }
  119. *ppb += cb;
  120. }
  121. return ptsz;
  122. }
  123. /*****************************************************************************
  124. *
  125. * Ole_ParseGuid
  126. *
  127. * Parse a guid. The format is
  128. *
  129. * { <dword> - <word> - <word> - <byte> <byte> -
  130. * <byte> <byte> <byte> <byte> <byte> <byte> }
  131. *
  132. *****************************************************************************/
  133. BOOL PASCAL
  134. Ole_ClsidFromString(LPCTSTR ptsz, LPCLSID pclsid)
  135. {
  136. if (lstrlen(ptsz) == ctchClsid - 1 && *ptsz == '{') {
  137. ptsz++;
  138. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 4, TEXT('-'));
  139. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 2, TEXT('-'));
  140. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 2, TEXT('-'));
  141. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  142. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, TEXT('-'));
  143. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  144. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  145. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  146. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  147. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, 0 );
  148. ptsz = Ole_ParseHex(ptsz, (LPBYTE *)&pclsid, 1, TEXT('}'));
  149. return ptsz ? TRUE : FALSE;
  150. } else {
  151. return 0;
  152. }
  153. }