util.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "main.h"
  2. #include "resource.h"
  3. #include <stdio.h>
  4. #include <strsafe.h>
  5. //static const GUID INVALID_GUID =
  6. //{ 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
  7. void WaitForEvent(HANDLE hEvent, DWORD msMaxWaitTime)
  8. {
  9. // DWORD i;
  10. MSG msg;
  11. const unsigned long eachWait = 10;
  12. unsigned long totalWait = 0;
  13. while (WaitForSingleObject(hEvent, eachWait) == WAIT_TIMEOUT)
  14. {
  15. while (PeekMessage(&msg, (HWND) NULL, 0, 0, PM_REMOVE))
  16. {
  17. //TranslateMessage(&msg);
  18. DispatchMessage(&msg);
  19. }
  20. totalWait += eachWait;
  21. if (totalWait >= msMaxWaitTime)
  22. break;
  23. }
  24. }
  25. char *HRErrorCode(HRESULT hr)
  26. {
  27. #define HR_ERROR_CODE(x) case x: return #x
  28. switch (hr)
  29. {
  30. HR_ERROR_CODE(E_OUTOFMEMORY);
  31. HR_ERROR_CODE(E_UNEXPECTED);
  32. HR_ERROR_CODE(S_OK);
  33. HR_ERROR_CODE(S_FALSE);
  34. HR_ERROR_CODE(E_NOINTERFACE);
  35. HR_ERROR_CODE(NS_E_PROTECTED_CONTENT);
  36. HR_ERROR_CODE(E_INVALIDARG);
  37. HR_ERROR_CODE(NS_E_DRM_NO_RIGHTS);
  38. HR_ERROR_CODE(NS_E_DRM_LICENSE_NOTACQUIRED);
  39. HR_ERROR_CODE(NS_E_DRM_ACQUIRING_LICENSE);
  40. HR_ERROR_CODE(NS_S_DRM_ACQUIRE_CANCELLED);
  41. HR_ERROR_CODE(NS_E_LICENSE_OUTOFDATE);
  42. HR_ERROR_CODE(NS_E_LICENSE_INCORRECT_RIGHTS);
  43. HR_ERROR_CODE(NS_E_DRM_REOPEN_CONTENT);
  44. HR_ERROR_CODE(NS_E_DRM_LICENSE_APPSECLOW);
  45. HR_ERROR_CODE(E_ABORT);
  46. HR_ERROR_CODE(NS_E_INVALID_REQUEST);
  47. HR_ERROR_CODE(NS_S_DRM_LICENSE_ACQUIRED);
  48. HR_ERROR_CODE(NS_S_DRM_MONITOR_CANCELLED);
  49. HR_ERROR_CODE(NS_E_FILE_NOT_FOUND);
  50. HR_ERROR_CODE(NS_E_FILE_OPEN_FAILED);
  51. HR_ERROR_CODE(NS_E_SERVER_NOT_FOUND);
  52. HR_ERROR_CODE(NS_E_UNRECOGNIZED_STREAM_TYPE);
  53. HR_ERROR_CODE(NS_E_NO_STREAM);
  54. HR_ERROR_CODE(E_ACCESSDENIED);
  55. HR_ERROR_CODE(NS_S_DRM_BURNABLE_TRACK);
  56. HR_ERROR_CODE(NS_S_DRM_BURNABLE_TRACK_WITH_PLAYLIST_RESTRICTION);
  57. HR_ERROR_CODE(NS_E_DRM_TRACK_EXCEEDED_PLAYLIST_RESTICTION);
  58. HR_ERROR_CODE(NS_E_DRM_TRACK_EXCEEDED_TRACKBURN_RESTRICTION);
  59. }
  60. static char temp[50];
  61. StringCchPrintfA(temp, 50, WASABI_API_LNGSTRING(IDS_UNKNOWN_ERROR), hr);
  62. return temp;
  63. }
  64. void GuidString(GUID guid, wchar_t *target, size_t len)
  65. {
  66. StringCchPrintf( target, len, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  67. (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  68. (int)guid.Data4[0], (int)guid.Data4[1],
  69. (int)guid.Data4[2], (int)guid.Data4[3],
  70. (int)guid.Data4[4], (int)guid.Data4[5],
  71. (int)guid.Data4[6], (int)guid.Data4[7] );
  72. }
  73. const wchar_t *UserTextDescription(unsigned char *binary, size_t size)
  74. {
  75. WM_USER_TEXT *userText = (WM_USER_TEXT *)binary;
  76. return userText->pwszDescription;
  77. }
  78. const wchar_t *UserTextString(unsigned char *binary, size_t size)
  79. {
  80. WM_USER_TEXT *userText = (WM_USER_TEXT *)binary;
  81. return userText->pwszText;
  82. }
  83. void BinaryString(unsigned char *binary, size_t size, wchar_t *final, size_t len)
  84. {
  85. wchar_t * const start = new wchar_t[2 + size*2 + 1]; // 0x + 2 hex per byte + null terminator
  86. wchar_t *target = start;
  87. *target++='0';
  88. *target++='x';
  89. size_t i;
  90. for (i = 0;i!=size;i++)
  91. {
  92. wchar_t temp[3] = {0};
  93. _itow(binary[i], temp, 16);
  94. if (!temp[0])
  95. {
  96. *target++ = '0';
  97. *target++ = '0';
  98. }
  99. else if (!temp[1])
  100. {
  101. *target++ = '0';
  102. *target++ = temp[0];
  103. }
  104. else
  105. {
  106. *target++ = temp[0];
  107. *target++ = temp[1];
  108. }
  109. }
  110. *target = 0;
  111. lstrcpyn(final, start, len);
  112. delete [] start;
  113. }
  114. GUID StringGUID(const wchar_t *source)
  115. {
  116. if (source == NULL) return INVALID_GUID;
  117. GUID guid = GUID_NULL;
  118. int Data1, Data2, Data3;
  119. int Data4[8] = {0};
  120. // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  121. int n = swscanf(source, L" { %08x - %04x - %04x - %02x%02x - %02x%02x%02x%02x%02x%02x } ",
  122. &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1,
  123. Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  124. if (n != 11) return INVALID_GUID;
  125. // Cross assign all the values
  126. guid.Data1 = Data1;
  127. guid.Data2 = Data2;
  128. guid.Data3 = Data3;
  129. guid.Data4[0] = Data4[0];
  130. guid.Data4[1] = Data4[1];
  131. guid.Data4[2] = Data4[2];
  132. guid.Data4[3] = Data4[3];
  133. guid.Data4[4] = Data4[4];
  134. guid.Data4[5] = Data4[5];
  135. guid.Data4[6] = Data4[6];
  136. guid.Data4[7] = Data4[7];
  137. return guid;
  138. }
  139. int DoAboutMessageBox(HWND parent, wchar_t* title, wchar_t* message)
  140. {
  141. MSGBOXPARAMS msgbx = {sizeof(MSGBOXPARAMS),0};
  142. msgbx.lpszText = message;
  143. msgbx.lpszCaption = title;
  144. msgbx.lpszIcon = MAKEINTRESOURCE(102);
  145. msgbx.hInstance = GetModuleHandle(0);
  146. msgbx.dwStyle = MB_USERICON;
  147. msgbx.hwndOwner = parent;
  148. return MessageBoxIndirect(&msgbx);
  149. }