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.

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