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.

491 lines
9.7 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: cPathname.cxx
  7. //
  8. // Contents: Pathname object
  9. //
  10. // History: 11-1-95 krishnag Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "winnt.hxx"
  14. #pragma hdrstop
  15. extern "C" {
  16. typedef struct _WKSTA_USER_INFO_1A {
  17. LPSTR wkui1_username;
  18. LPSTR wkui1_logon_domain;
  19. LPSTR wkui1_oth_domains;
  20. LPSTR wkui1_logon_server;
  21. }WKSTA_USER_INFO_1A, *PWKSTA_USER_INFO_1A, *LPWKSTA_USER_INFO_1A;
  22. NET_API_STATUS NET_API_FUNCTION
  23. NetWkstaUserGetInfoA (
  24. IN LPSTR reserved,
  25. IN DWORD level,
  26. OUT LPBYTE *bufptr
  27. );
  28. }
  29. // Class CWinNTSystemInfo
  30. DEFINE_IDispatch_Implementation(CWinNTSystemInfo)
  31. //+---------------------------------------------------------------------------
  32. // Function: CWinNTSystemInfo::CWinNTSystemInfo
  33. //
  34. // Synopsis: Constructor
  35. //
  36. // Arguments:
  37. //
  38. // Returns: HRESULT
  39. //
  40. // Modifies: -
  41. //
  42. // History:
  43. //
  44. //----------------------------------------------------------------------------
  45. CWinNTSystemInfo::CWinNTSystemInfo():
  46. _pDispMgr(NULL)
  47. {
  48. ENLIST_TRACKING(CWinNTSystemInfo);
  49. }
  50. //+---------------------------------------------------------------------------
  51. // Function: CWinNTSystemInfo::CreateWinNTSystemInfo
  52. //
  53. // Synopsis:
  54. //
  55. // Arguments:
  56. //
  57. // Returns: HRESULT
  58. //
  59. // Modifies: -
  60. //
  61. // History:
  62. //
  63. //----------------------------------------------------------------------------
  64. HRESULT
  65. CWinNTSystemInfo::CreateWinNTSystemInfo(
  66. REFIID riid,
  67. void **ppvObj
  68. )
  69. {
  70. CWinNTSystemInfo FAR * pWinNTSystemInfo = NULL;
  71. HRESULT hr = S_OK;
  72. hr = AllocateWinNTSystemInfoObject(&pWinNTSystemInfo);
  73. BAIL_ON_FAILURE(hr);
  74. hr = pWinNTSystemInfo->QueryInterface(riid, ppvObj);
  75. BAIL_ON_FAILURE(hr);
  76. pWinNTSystemInfo->Release();
  77. RRETURN(hr);
  78. error:
  79. delete pWinNTSystemInfo;
  80. RRETURN(hr);
  81. }
  82. //+---------------------------------------------------------------------------
  83. // Function: CWinNTSystemInfo::~CWinNTSystemInfo
  84. //
  85. // Synopsis:
  86. //
  87. // Arguments:
  88. //
  89. // Returns: HRESULT
  90. //
  91. // Modifies: -
  92. //
  93. // History:
  94. //
  95. //----------------------------------------------------------------------------
  96. CWinNTSystemInfo::~CWinNTSystemInfo( )
  97. {
  98. delete _pDispMgr;
  99. }
  100. //+---------------------------------------------------------------------------
  101. // Function: CWinNTSystemInfo::QueryInterface
  102. //
  103. // Synopsis:
  104. //
  105. // Arguments:
  106. //
  107. // Returns: HRESULT
  108. //
  109. // Modifies: -
  110. //
  111. // History:
  112. //
  113. //----------------------------------------------------------------------------
  114. STDMETHODIMP
  115. CWinNTSystemInfo::QueryInterface(
  116. REFIID iid,
  117. LPVOID FAR* ppv
  118. )
  119. {
  120. if (ppv == NULL) {
  121. RRETURN(E_POINTER);
  122. }
  123. if (IsEqualIID(iid, IID_IUnknown))
  124. {
  125. *ppv = (IADsWinNTSystemInfo FAR *) this;
  126. }
  127. else if (IsEqualIID(iid, IID_IADsWinNTSystemInfo))
  128. {
  129. *ppv = (IADsWinNTSystemInfo FAR *) this;
  130. }
  131. else if (IsEqualIID(iid, IID_IDispatch))
  132. {
  133. *ppv = (IADsWinNTSystemInfo FAR *) this;
  134. }
  135. else if (IsEqualIID(iid, IID_ISupportErrorInfo))
  136. {
  137. *ppv = (ISupportErrorInfo FAR *) this;
  138. }
  139. else
  140. {
  141. *ppv = NULL;
  142. return E_NOINTERFACE;
  143. }
  144. AddRef();
  145. return NOERROR;
  146. }
  147. //+---------------------------------------------------------------------------
  148. // Function: CWinNTSystemInfo::AllocateWinNTSystemInfoObject
  149. //
  150. // Synopsis:
  151. //
  152. // Arguments:
  153. //
  154. // Returns: HRESULT
  155. //
  156. // Modifies: -
  157. //
  158. // History:
  159. //
  160. //----------------------------------------------------------------------------
  161. HRESULT
  162. CWinNTSystemInfo::AllocateWinNTSystemInfoObject(
  163. CWinNTSystemInfo ** ppWinNTSystemInfo
  164. )
  165. {
  166. CWinNTSystemInfo FAR * pWinNTSystemInfo = NULL;
  167. CAggregatorDispMgr FAR * pDispMgr = NULL;
  168. HRESULT hr = S_OK;
  169. pWinNTSystemInfo = new CWinNTSystemInfo();
  170. if (pWinNTSystemInfo == NULL) {
  171. hr = E_OUTOFMEMORY;
  172. }
  173. BAIL_ON_FAILURE(hr);
  174. pDispMgr = new CAggregatorDispMgr;
  175. if (pDispMgr == NULL) {
  176. hr = E_OUTOFMEMORY;
  177. }
  178. BAIL_ON_FAILURE(hr);
  179. hr = LoadTypeInfoEntry(
  180. pDispMgr,
  181. LIBID_ADs,
  182. IID_IADsWinNTSystemInfo,
  183. (IADsWinNTSystemInfo *)pWinNTSystemInfo,
  184. DISPID_REGULAR
  185. );
  186. BAIL_ON_FAILURE(hr);
  187. pWinNTSystemInfo->_pDispMgr = pDispMgr;
  188. *ppWinNTSystemInfo = pWinNTSystemInfo;
  189. RRETURN(hr);
  190. error:
  191. delete pDispMgr;
  192. RRETURN_EXP_IF_ERR(hr);
  193. }
  194. //+---------------------------------------------------------------------------
  195. // Function: CWinNTSystemInfo::InterfaceSupportsErrorInfo
  196. //
  197. // Synopsis:
  198. //
  199. // Arguments:
  200. //
  201. // Returns: HRESULT
  202. //
  203. // Modifies: -
  204. //
  205. // History:
  206. //
  207. //----------------------------------------------------------------------------
  208. HRESULT
  209. CWinNTSystemInfo::InterfaceSupportsErrorInfo(THIS_ REFIID riid)
  210. {
  211. if (IsEqualIID(riid, IID_IADsWinNTSystemInfo)) {
  212. RRETURN(S_OK);
  213. } else {
  214. RRETURN(S_FALSE);
  215. }
  216. }
  217. HRESULT
  218. CWinNTSystemInfo::get_UserName(
  219. BSTR * bstrUserName
  220. )
  221. {
  222. PWSTR pszUserName = NULL;
  223. ULONG uLength;
  224. HRESULT hr;
  225. //
  226. // Validate parameters
  227. //
  228. if ( !bstrUserName )
  229. {
  230. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  231. }
  232. //
  233. // Get length of buffer to be allocated
  234. //
  235. uLength = 0;
  236. GetUserName(NULL, &uLength);
  237. if (uLength > 0)
  238. {
  239. //
  240. // Allocate memory and do the real work
  241. //
  242. pszUserName = (PWSTR)AllocADsMem(uLength * sizeof(WCHAR));
  243. if (!pszUserName)
  244. {
  245. RRETURN(E_OUTOFMEMORY);
  246. }
  247. if (GetUserName(pszUserName, &uLength))
  248. {
  249. hr = ADsAllocString(pszUserName, bstrUserName);
  250. BAIL_ON_FAILURE(hr);
  251. }
  252. else
  253. hr = HRESULT_FROM_WIN32(GetLastError());
  254. }
  255. else
  256. hr = HRESULT_FROM_WIN32(GetLastError());
  257. error:
  258. if (pszUserName)
  259. {
  260. FreeADsMem(pszUserName);
  261. }
  262. RRETURN(hr);
  263. }
  264. HRESULT
  265. CWinNTSystemInfo::get_ComputerName(
  266. BSTR * bstrComputerName
  267. )
  268. {
  269. PWSTR pszComputerName = NULL;
  270. ULONG uLength;
  271. HRESULT hr;
  272. //
  273. // Validate parameters
  274. //
  275. if (!bstrComputerName)
  276. {
  277. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  278. }
  279. //
  280. // Get length of buffer to be allocated
  281. //
  282. uLength = 0;
  283. GetComputerName(NULL, &uLength);
  284. if (uLength > 0)
  285. {
  286. //
  287. // Allocated memory and do the real work
  288. //
  289. pszComputerName = (PWSTR)AllocADsMem(uLength * sizeof(WCHAR));
  290. if (!pszComputerName)
  291. {
  292. RRETURN(E_OUTOFMEMORY);
  293. }
  294. if (GetComputerName(pszComputerName, &uLength))
  295. {
  296. hr = ADsAllocString(pszComputerName, bstrComputerName);
  297. BAIL_ON_FAILURE(hr);
  298. }
  299. else
  300. hr = HRESULT_FROM_WIN32(GetLastError());
  301. }
  302. else
  303. hr = HRESULT_FROM_WIN32(GetLastError());
  304. error:
  305. if (pszComputerName)
  306. {
  307. FreeADsMem(pszComputerName);
  308. }
  309. RRETURN(hr);
  310. }
  311. HRESULT
  312. CWinNTSystemInfo::get_DomainName(
  313. BSTR * bstrDomainName
  314. )
  315. {
  316. PWKSTA_USER_INFO_1 pInfo = NULL;
  317. PWKSTA_USER_INFO_1A pInfoA = NULL;
  318. DWORD err;
  319. HRESULT hr = S_OK;
  320. PWSTR pszDomainName = NULL;
  321. //
  322. // Validate parameters
  323. //
  324. if (!bstrDomainName )
  325. {
  326. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  327. }
  328. //
  329. // Call NetWkstaUserGetInfo to find domain name
  330. //
  331. #if (defined WIN95)
  332. err = NetWkstaUserGetInfoA(NULL, 1, (LPBYTE *) &pInfoA);
  333. if (err != ERROR_SUCCESS)
  334. {
  335. RRETURN(HRESULT_FROM_WIN32(err));
  336. }
  337. pszDomainName = (PWSTR)AllocADsMem((lstrlenA(pInfoA->wkui1_logon_domain) + 1) * sizeof(WCHAR));
  338. if (!pszDomainName)
  339. {
  340. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  341. }
  342. if (MultiByteToWideChar(CP_ACP,
  343. MB_PRECOMPOSED,
  344. pInfoA->wkui1_logon_domain,
  345. -1,
  346. pszDomainName,
  347. lstrlenA(pInfoA->wkui1_logon_domain) + 1) == 0)
  348. {
  349. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(GetLastError()));
  350. }
  351. hr = ADsAllocString(pszDomainName, bstrDomainName);
  352. BAIL_ON_FAILURE(hr);
  353. #else
  354. err = NetWkstaUserGetInfo(NULL, 1, (LPBYTE*) &pInfo);
  355. if (err != ERROR_SUCCESS)
  356. {
  357. RRETURN(HRESULT_FROM_WIN32(err));
  358. }
  359. hr = ADsAllocString((LPWSTR)pInfo->wkui1_logon_domain, bstrDomainName);
  360. BAIL_ON_FAILURE(hr);
  361. #endif
  362. error:
  363. if (pInfo)
  364. {
  365. NetApiBufferFree(pInfo);
  366. }
  367. if (pInfoA)
  368. {
  369. NetApiBufferFree(pInfoA);
  370. }
  371. if (pszDomainName)
  372. {
  373. FreeADsMem(pszDomainName);
  374. }
  375. RRETURN(hr);
  376. }
  377. HRESULT
  378. CWinNTSystemInfo::get_PDC(
  379. BSTR * bstrPDC
  380. )
  381. {
  382. PWSTR pszPDC = NULL;
  383. HRESULT hr;
  384. DWORD err;
  385. //
  386. // Validate parameters
  387. //
  388. if (!bstrPDC )
  389. {
  390. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  391. }
  392. err = NetGetDCName(NULL, NULL, (LPBYTE*)&pszPDC);
  393. if (err != ERROR_SUCCESS)
  394. {
  395. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  396. }
  397. hr = ADsAllocString(&pszPDC[2], bstrPDC); // remove '\\';
  398. BAIL_ON_FAILURE(hr);
  399. error:
  400. if (pszPDC)
  401. {
  402. NetApiBufferFree(pszPDC);
  403. }
  404. return hr;
  405. }
  406. STDMETHODIMP
  407. CWinNTSystemInfoCF::CreateInstance(
  408. IUnknown * pUnkOuter,
  409. REFIID iid,
  410. LPVOID * ppv
  411. )
  412. {
  413. HRESULT hr = E_FAIL;
  414. if (pUnkOuter)
  415. RRETURN(E_FAIL);
  416. hr = CWinNTSystemInfo::CreateWinNTSystemInfo(
  417. iid,
  418. ppv
  419. );
  420. RRETURN(hr);
  421. }