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.

191 lines
5.0 KiB

  1. #include "framecommon.h"
  2. ////////////
  3. // Globals
  4. //
  5. const TCHAR g_tszMutexName[] = _T("DIActionConfigFrameworkPreventSecondInstance");
  6. const CHAR g_szAppName[] = "DCONFIG"; // application name
  7. HANDLE g_hMutex = NULL; // prevent second instance mutex
  8. HINSTANCE g_hInst = NULL; // global instance handle
  9. HWND g_hwndMain = NULL; // handle to the main window
  10. LPDIACTIONFORMATW lpDiActFormMaster = NULL; //master copy of the DIACTIONFORMAT
  11. DIACTIONFORMATW diActForLocal; //local copy of DIACTIONFORMAT
  12. LPCWSTR lpUserName = NULL; //user name
  13. // {FD4ACE13-7044-4204-8B15-095286B12EAD}
  14. static const GUID GUID_DIConfigAppEditLayout =
  15. { 0xfd4ace13, 0x7044, 0x4204, { 0x8b, 0x15, 0x9, 0x52, 0x86, 0xb1, 0x2e, 0xad } };
  16. /////////////////
  17. // Functions...
  18. //
  19. //////////////////////////////////////////
  20. // Actually runs the DConfig dialog app.
  21. // retures EXIT_FAILURE or EXIT_SUCCESS.
  22. //
  23. int RunDConfig(HINSTANCE hInstance, HWND hWnd, LPVOID lpDDSurf, LPDICONFIGUREDEVICESCALLBACK pCallback, DWORD dwFlags)
  24. {
  25. // quit (with errmsg) if we're not at least 256 colors / 8 bit
  26. HDC hMemDC = CreateCompatibleDC(NULL);
  27. int bpp = GetDeviceCaps(hMemDC, BITSPIXEL);
  28. DeleteDC(hMemDC);
  29. if (bpp < 8)
  30. {
  31. ErrorBox(IDS_ERROR_COLORS);
  32. return EXIT_FAILURE;
  33. }
  34. // set global instance handle
  35. g_hInst = hInstance;
  36. // create and run the configuration window
  37. GUID check = lpDiActFormMaster ? lpDiActFormMaster->guidApplication : diActForLocal.guidApplication;
  38. BOOL bEditLayout = IsEqualGUID(check, GUID_DIConfigAppEditLayout);
  39. CFlexWnd::RegisterWndClass(hInstance);
  40. CConfigWnd cfgWnd(lpDDSurf, pCallback, bEditLayout, dwFlags);
  41. BOOL bSuccess = cfgWnd.Create(hWnd);
  42. if (bSuccess)
  43. {
  44. g_hwndMain = cfgWnd.m_hWnd;
  45. // enter message loop
  46. MSG msg;
  47. while (GetMessage(&msg, NULL, 0, 0))
  48. {
  49. TranslateMessage(&msg);
  50. DispatchMessage(&msg);
  51. }
  52. }
  53. CFlexWnd::UnregisterWndClass(hInstance);
  54. return bSuccess ? EXIT_SUCCESS : EXIT_FAILURE;
  55. }
  56. ///////////////////////////////////////////
  57. // Allocates and loads a resource string.
  58. // Returns it or NULL if unsuccessful.
  59. //
  60. TCHAR *AllocLoadString(UINT strid)
  61. {
  62. // allocate
  63. const int STRING_LENGTHS = 1024;
  64. TCHAR *str = (TCHAR *)malloc(STRING_LENGTHS * sizeof(TCHAR));
  65. // use if allocated succesfully
  66. if (str != NULL)
  67. {
  68. // load the string, or free and null if we can't
  69. if (LoadString(g_hInst, strid, str, STRING_LENGTHS) == NULL)
  70. {
  71. free(str);
  72. str = NULL;
  73. }
  74. }
  75. return str;
  76. }
  77. void ErrorBox(UINT strid, HRESULT hr)
  78. {
  79. LPCTSTR s;
  80. switch (hr)
  81. {
  82. case DIERR_BETADIRECTINPUTVERSION: s = _T("DIERR_BETADIRECTINPUTVERSION"); break;
  83. case DIERR_INVALIDPARAM: s = _T("DIERR_INVALIDPARAM"); break;
  84. case DIERR_OLDDIRECTINPUTVERSION: s = _T("DIERR_OLDDIRECTINPUTVERSION"); break;
  85. case DIERR_OUTOFMEMORY: s = _T("DIERR_OUTOFMEMORY"); break;
  86. }
  87. ErrorBox(strid, s);
  88. }
  89. /////////////////////////////////////////////////////////
  90. // Shows a message box with the specified error string.
  91. // (automatically handles getting and freeing string.)
  92. //
  93. void ErrorBox(UINT strid, LPCTSTR postmsg)
  94. {
  95. // alloc 'n load
  96. TCHAR *errmsg = AllocLoadString(strid);
  97. TCHAR *errdlgtitle = AllocLoadString(IDS_ERROR_DLG_TITLE);
  98. // put english dummy error text for strings that couldn't load
  99. if (NULL == errmsg)
  100. errmsg = _tcsdup(_T("Error! (unspecified: could not load error string!)"));
  101. if (NULL == errdlgtitle)
  102. errdlgtitle = _tcsdup(_T("Error! (could not load error box title string!)"));
  103. // append post message if provided
  104. if (postmsg != NULL)
  105. {
  106. TCHAR catstr[] = _T("\n\n");
  107. int len = _tcslen(errmsg) + _tcslen(catstr) + _tcslen(postmsg);
  108. TCHAR *fullmsg = (TCHAR *)malloc(len * sizeof(TCHAR));
  109. if (fullmsg != NULL)
  110. {
  111. _tcscpy(fullmsg, errmsg);
  112. _tcscat(fullmsg, catstr);
  113. _tcscat(fullmsg, postmsg);
  114. free(errmsg);
  115. errmsg = fullmsg;
  116. }
  117. }
  118. // Show error
  119. MessageBox(g_hwndMain, errmsg, errdlgtitle, MB_OK | MB_ICONSTOP);
  120. // free which ever strings loaded
  121. if (NULL != errmsg)
  122. free(errmsg);
  123. if (NULL != errdlgtitle)
  124. free(errdlgtitle);
  125. }
  126. /////////////////////////////////////////////////////////
  127. // Shows a message box based on GetLastError().
  128. //
  129. void LastErrorBox()
  130. {
  131. DWORD lasterr = GetLastError();
  132. LPVOID lpMsgBuf = NULL;
  133. TCHAR *errdlgtitle = AllocLoadString(IDS_ERROR_DLG_TITLE);
  134. // format an error message from GetLastError().
  135. DWORD result = FormatMessage(
  136. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  137. FORMAT_MESSAGE_FROM_SYSTEM |
  138. FORMAT_MESSAGE_IGNORE_INSERTS,
  139. NULL,
  140. lasterr,
  141. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  142. (LPTSTR) &lpMsgBuf,
  143. 0,
  144. NULL);
  145. // put english dummy error text for dlg title if it couldn't load
  146. if (NULL == errdlgtitle)
  147. errdlgtitle = _tcsdup(_T("Error! (could not load error box title string!)"));
  148. // if format message fails, show an error box without string id
  149. if (0 == result)
  150. ErrorBox(NULL);
  151. else // otherwise, show the message we just formatted
  152. {
  153. assert(lpMsgBuf != NULL);
  154. MessageBox(g_hwndMain, (LPCTSTR)lpMsgBuf, errdlgtitle, MB_OK | MB_ICONINFORMATION);
  155. }
  156. // free whichever strings were allocated
  157. if (NULL != lpMsgBuf)
  158. LocalFree(lpMsgBuf);
  159. if (NULL != errdlgtitle)
  160. free(errdlgtitle);
  161. }