Leaked source code of windows server 2003
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.

258 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. nwshmisc.cxx
  5. Abstract:
  6. This module implements misc methods used in the shell extension classes.
  7. Author:
  8. Yi-Hsin Sung (yihsins) 25-Oct-1995
  9. --*/
  10. #include <windows.h>
  11. #include <shellapi.h>
  12. #include <shlobj.h>
  13. #define DONT_WANT_SHELLDEBUG
  14. #include <shlobjp.h>
  15. #include "nwshcmn.h"
  16. #define MAX_RESOURCE_STRING_LENGTH 256
  17. VOID HideControl( HWND hwndDlg, WORD wID )
  18. {
  19. HWND hwndTmp = ::GetDlgItem( hwndDlg, wID );
  20. ::EnableWindow( hwndTmp, FALSE );
  21. ::ShowWindow( hwndTmp, FALSE );
  22. }
  23. VOID UnHideControl( HWND hwndDlg, WORD wID )
  24. {
  25. HWND hwndTmp = ::GetDlgItem( hwndDlg, wID );
  26. ::EnableWindow( hwndTmp, TRUE );
  27. ::ShowWindow( hwndTmp, TRUE );
  28. }
  29. VOID EnableDlgItem( HWND hwndDlg, WORD wID, BOOL fEnable)
  30. {
  31. HWND hwndTmp = ::GetDlgItem( hwndDlg, wID );
  32. ::EnableWindow( hwndTmp, fEnable);
  33. }
  34. /*
  35. * LoadErrorPrintf
  36. * -------------
  37. *
  38. * Uses normal printf style format string
  39. */
  40. DWORD
  41. LoadMsgErrorPrintf(
  42. LPWSTR *ppszMessage,
  43. UINT uiMsg,
  44. DWORD errNum
  45. )
  46. {
  47. DWORD nLen = 0;
  48. DWORD err = NO_ERROR;
  49. LPWSTR pszError = NULL;
  50. WCHAR szError[20];
  51. *ppszMessage = NULL;
  52. //
  53. // Try to get the error string associated with the given number
  54. // from the system. If we cannot find the string, then
  55. // just show the number.
  56. //
  57. nLen = ::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
  58. | FORMAT_MESSAGE_IGNORE_INSERTS
  59. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  60. NULL, // ignored
  61. errNum, // Message resource id
  62. 0, // Language id
  63. (LPWSTR) &pszError,
  64. // Return pointer to formatted text
  65. 256, // Min.length
  66. NULL );
  67. if ( nLen == 0 || pszError == NULL )
  68. {
  69. wsprintf( szError, L"%d", errNum );
  70. }
  71. err = LoadMsgPrintf( ppszMessage, uiMsg, pszError? pszError : szError );
  72. if ( pszError )
  73. ::LocalFree( pszError );
  74. return err;
  75. }
  76. /*
  77. * LoadMsgPrintf
  78. * -------------
  79. *
  80. * Uses normal printf style format string
  81. */
  82. DWORD
  83. LoadMsgPrintf(
  84. LPWSTR *ppszMessage,
  85. UINT uiMsg,
  86. ...
  87. )
  88. {
  89. WCHAR szMessage[512];
  90. DWORD err = NO_ERROR;
  91. DWORD nLen = 0;
  92. va_list start;
  93. va_start( start, uiMsg );
  94. *ppszMessage = NULL;
  95. if ( ::LoadString( ::hmodNW, uiMsg, szMessage,
  96. sizeof(szMessage)/sizeof(szMessage[0])))
  97. {
  98. nLen = ::FormatMessage( FORMAT_MESSAGE_FROM_STRING
  99. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  100. szMessage,
  101. 0, // Message resource id, ignored
  102. 0, // Language id
  103. (LPWSTR) ppszMessage,
  104. // Return pointer to formatted text
  105. 256, // Min.length
  106. &start );
  107. if ( nLen == 0 || *ppszMessage == NULL )
  108. err = GetLastError();
  109. }
  110. va_end(start);
  111. return err;
  112. }
  113. /*
  114. * MsgBoxErrorPrintf
  115. * ------------
  116. *
  117. * Message box routine
  118. *
  119. */
  120. DWORD
  121. MsgBoxErrorPrintf(
  122. HWND hwnd,
  123. UINT uiMsg,
  124. UINT uiTitle,
  125. UINT uiFlags,
  126. DWORD errNum,
  127. LPWSTR pszInsertStr
  128. )
  129. {
  130. DWORD nLen = 0;
  131. DWORD err = NO_ERROR;
  132. LPWSTR pszError = NULL;
  133. WCHAR szError[20];
  134. nLen = ::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
  135. | FORMAT_MESSAGE_IGNORE_INSERTS
  136. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  137. NULL, // ignored
  138. errNum, // Message resource id
  139. 0, // Language id
  140. (LPWSTR) &pszError,
  141. // Return pointer to formatted text
  142. 256, // Min.length
  143. NULL );
  144. if ( nLen == 0 || pszError == NULL )
  145. wsprintf( szError, L"%d", errNum );
  146. if ( pszInsertStr )
  147. {
  148. err = MsgBoxPrintf( hwnd, uiMsg, uiTitle, uiFlags,
  149. pszError? pszError : szError, pszInsertStr );
  150. }
  151. else
  152. {
  153. err = MsgBoxPrintf( hwnd, uiMsg, uiTitle, uiFlags,
  154. pszError? pszError : szError );
  155. }
  156. if ( pszError )
  157. ::LocalFree( pszError );
  158. return err;
  159. }
  160. /*
  161. * MsgBoxPrintf
  162. * ------------
  163. *
  164. * Message box routine
  165. *
  166. */
  167. DWORD
  168. MsgBoxPrintf(
  169. HWND hwnd,
  170. UINT uiMsg,
  171. UINT uiTitle,
  172. UINT uiFlags,
  173. ...
  174. )
  175. {
  176. WCHAR szTitle[MAX_RESOURCE_STRING_LENGTH];
  177. WCHAR szMessage[MAX_RESOURCE_STRING_LENGTH];
  178. LPWSTR lpFormattedMessage = NULL;
  179. DWORD err = NO_ERROR;
  180. va_list start;
  181. va_start(start,uiFlags);
  182. if ( ::LoadString( ::hmodNW, uiMsg, szMessage, sizeof(szMessage)/sizeof(szMessage[0]))
  183. && ::LoadString( ::hmodNW, uiTitle, szTitle, sizeof(szTitle)/sizeof(szTitle[0]))
  184. )
  185. {
  186. DWORD nLen = ::FormatMessage( FORMAT_MESSAGE_FROM_STRING
  187. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  188. szMessage,
  189. 0, // Resource Id, ignored
  190. NULL, // Language Id, ignored
  191. (LPWSTR) &lpFormattedMessage,
  192. // Return pointer to formatted text
  193. 256, // Min.length
  194. &start );
  195. if ( nLen == 0 || lpFormattedMessage == NULL )
  196. {
  197. err = GetLastError();
  198. goto CleanExit;
  199. }
  200. err = ::MessageBox( hwnd,
  201. lpFormattedMessage,
  202. szTitle,
  203. uiFlags );
  204. ::LocalFree( lpFormattedMessage );
  205. }
  206. CleanExit:
  207. va_end(start);
  208. return err;
  209. }