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.

304 lines
7.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1995.
  5. //
  6. // File: util.cxx
  7. //
  8. // Contents: Misc helper functions
  9. //
  10. // History: 5-Apr-95 BruceFo Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.hxx"
  14. #pragma hdrstop
  15. #include "resource.h"
  16. #include "util.hxx"
  17. //////////////////////////////////////////////////////////////////////////////
  18. #define NETMSG_DLL TEXT("netmsg.dll")
  19. //////////////////////////////////////////////////////////////////////////////
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Function: MyFormatMessageText
  23. //
  24. // Synopsis: Given a resource IDs, load strings from given instance
  25. // and format the string into a buffer
  26. //
  27. // History: 11-Aug-93 WilliamW Created.
  28. //
  29. //--------------------------------------------------------------------------
  30. VOID
  31. MyFormatMessageText(
  32. IN HRESULT dwMsgId,
  33. IN PWSTR pszBuffer,
  34. IN DWORD dwBufferSize,
  35. IN va_list * parglist
  36. )
  37. {
  38. //
  39. // get message from system or app msg file.
  40. //
  41. DWORD dwReturn = FormatMessage(
  42. FORMAT_MESSAGE_FROM_HMODULE,
  43. g_hInstance,
  44. dwMsgId,
  45. LANG_USER_DEFAULT,
  46. pszBuffer,
  47. dwBufferSize,
  48. parglist);
  49. if (0 == dwReturn) // couldn't find message
  50. {
  51. appDebugOut((DEB_IERROR,
  52. "FormatMessage failed, 0x%08lx\n",
  53. GetLastError()));
  54. WCHAR szText[200];
  55. LoadString(g_hInstance, IDS_APP_MSG_NOT_FOUND, szText, ARRAYLEN(szText));
  56. wsprintf(pszBuffer,szText,dwMsgId);
  57. }
  58. }
  59. //+-------------------------------------------------------------------------
  60. //
  61. // Function: MyCommonDialog
  62. //
  63. // Synopsis: Common popup dialog routine - stole from diskadm directory
  64. //
  65. //--------------------------------------------------------------------------
  66. DWORD
  67. MyCommonDialog(
  68. IN HWND hwnd,
  69. IN HRESULT dwMsgCode,
  70. IN PWSTR pszCaption,
  71. IN DWORD dwFlags,
  72. IN va_list arglist
  73. )
  74. {
  75. WCHAR szMsgBuf[500];
  76. MyFormatMessageText(dwMsgCode, szMsgBuf, ARRAYLEN(szMsgBuf), &arglist);
  77. return MessageBox(hwnd, szMsgBuf, pszCaption, dwFlags);
  78. }
  79. //+-------------------------------------------------------------------------
  80. //
  81. // Function: MyErrorDialog
  82. //
  83. // Synopsis: This routine retreives a message from the app or system
  84. // message file and displays it in a message box.
  85. //
  86. // Note: Stole from diskadm directory
  87. //
  88. //--------------------------------------------------------------------------
  89. VOID
  90. MyErrorDialog(
  91. IN HWND hwnd,
  92. IN HRESULT dwErrorCode,
  93. ...
  94. )
  95. {
  96. WCHAR szCaption[100];
  97. va_list arglist;
  98. va_start(arglist, dwErrorCode);
  99. LoadString(g_hInstance, IDS_MSGTITLE, szCaption, ARRAYLEN(szCaption));
  100. MyCommonDialog(hwnd, dwErrorCode, szCaption, MB_ICONSTOP | MB_OK, arglist);
  101. va_end(arglist);
  102. }
  103. //+-------------------------------------------------------------------------
  104. //
  105. // Function: MessageFromError
  106. //
  107. // Synopsis: MessageFromError returns a message ID which is more or
  108. // less appropriate to a return code from a remoted
  109. // NetServerGetInfo.
  110. //
  111. // History: 26-Sep-95 BruceFo Stolen from Win95
  112. //
  113. //--------------------------------------------------------------------------
  114. HRESULT
  115. MessageFromError(
  116. NET_API_STATUS err
  117. )
  118. {
  119. switch (err)
  120. {
  121. case ERROR_ACCESS_DENIED:
  122. case ERROR_NETWORK_ACCESS_DENIED:
  123. return MSG_ACCESSDENIED;
  124. case ERROR_BAD_NETPATH:
  125. return MSG_SERVERNOTFOUND;
  126. default:
  127. return MSG_CANTREMOTE;
  128. }
  129. return 0;
  130. }
  131. //----------------------------------------------------------------------------
  132. CWaitCursor::CWaitCursor(UINT idResCursor)
  133. {
  134. _hcurWait = _hcurOld = NULL;
  135. if (0 != idResCursor)
  136. {
  137. _hcurWait = LoadCursor(g_hInstance, MAKEINTRESOURCE(idResCursor));
  138. _hcurOld = SetCursor(_hcurWait);
  139. }
  140. else
  141. {
  142. _hcurOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
  143. }
  144. }
  145. CWaitCursor::~CWaitCursor()
  146. {
  147. ::SetCursor( _hcurOld );
  148. if (_hcurWait)
  149. {
  150. ::DestroyCursor( _hcurWait );
  151. }
  152. }
  153. //----------------------------------------------------------------------------
  154. NET_API_STATUS
  155. MyNetpGetDomainNameEx (
  156. IN LPWSTR MachineName,
  157. OUT LPWSTR* DomainNamePtr, // alloc and set ptr (free with NetApiBufferFree)
  158. OUT PBOOL IsWorkgroupName
  159. )
  160. /*++
  161. Routine Description:
  162. Returns the name of the domain or workgroup this machine belongs to.
  163. Stolen from netlib and hacked to remote it.
  164. Arguments:
  165. MachineName - The machine in question
  166. DomainNamePtr - The name of the domain or workgroup
  167. IsWorkgroupName - Returns TRUE if the name is a workgroup name.
  168. Returns FALSE if the name is a domain name.
  169. Return Value:
  170. NERR_Success - Success.
  171. NERR_CfgCompNotFound - There was an error determining the domain name
  172. --*/
  173. {
  174. NET_API_STATUS status;
  175. NTSTATUS ntstatus;
  176. LSA_HANDLE PolicyHandle;
  177. PPOLICY_ACCOUNT_DOMAIN_INFO PrimaryDomainInfo;
  178. OBJECT_ATTRIBUTES ObjAttributes;
  179. UNICODE_STRING unicodeMachineName;
  180. //
  181. // Check for caller's errors.
  182. //
  183. if (DomainNamePtr == NULL)
  184. {
  185. return ERROR_INVALID_PARAMETER;
  186. }
  187. //
  188. // Open a handle to the machine's security policy. Initialize the
  189. // objects attributes structure first.
  190. //
  191. InitializeObjectAttributes(
  192. &ObjAttributes,
  193. NULL,
  194. 0L,
  195. NULL,
  196. NULL
  197. );
  198. RtlInitUnicodeString(&unicodeMachineName, MachineName);
  199. ntstatus = LsaOpenPolicy(
  200. &unicodeMachineName,
  201. &ObjAttributes,
  202. POLICY_VIEW_LOCAL_INFORMATION,
  203. &PolicyHandle
  204. );
  205. if (! NT_SUCCESS(ntstatus))
  206. {
  207. appDebugOut((DEB_ERROR,
  208. "NetpGetDomainName: LsaOpenPolicy returned 0x%08lx\n",
  209. ntstatus));
  210. return NERR_CfgCompNotFound;
  211. }
  212. //
  213. // Get the name of the primary domain from LSA
  214. //
  215. ntstatus = LsaQueryInformationPolicy(
  216. PolicyHandle,
  217. PolicyPrimaryDomainInformation,
  218. (PVOID *) &PrimaryDomainInfo
  219. );
  220. if (! NT_SUCCESS(ntstatus))
  221. {
  222. appDebugOut((DEB_ERROR,
  223. "NetpGetDomainName: LsaQueryInformationPolicy failed 0x%08lx\n",
  224. ntstatus));
  225. (void) LsaClose(PolicyHandle);
  226. return NERR_CfgCompNotFound;
  227. }
  228. (void) LsaClose(PolicyHandle);
  229. status = NetApiBufferAllocate(
  230. PrimaryDomainInfo->DomainName.Length + sizeof(WCHAR),
  231. (LPVOID*)DomainNamePtr);
  232. if (status != NERR_Success)
  233. {
  234. (void) LsaFreeMemory((PVOID) PrimaryDomainInfo);
  235. return status;
  236. }
  237. ZeroMemory(
  238. *DomainNamePtr,
  239. PrimaryDomainInfo->DomainName.Length + sizeof(WCHAR)
  240. );
  241. CopyMemory(
  242. *DomainNamePtr,
  243. PrimaryDomainInfo->DomainName.Buffer,
  244. PrimaryDomainInfo->DomainName.Length
  245. );
  246. *IsWorkgroupName = (PrimaryDomainInfo->DomainSid == NULL);
  247. (void) LsaFreeMemory((PVOID) PrimaryDomainInfo);
  248. return NERR_Success;
  249. }