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.

492 lines
10 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 pWinNTSystemInfo;
  192. delete pDispMgr;
  193. RRETURN_EXP_IF_ERR(hr);
  194. }
  195. //+---------------------------------------------------------------------------
  196. // Function: CWinNTSystemInfo::InterfaceSupportsErrorInfo
  197. //
  198. // Synopsis:
  199. //
  200. // Arguments:
  201. //
  202. // Returns: HRESULT
  203. //
  204. // Modifies: -
  205. //
  206. // History:
  207. //
  208. //----------------------------------------------------------------------------
  209. HRESULT
  210. CWinNTSystemInfo::InterfaceSupportsErrorInfo(THIS_ REFIID riid)
  211. {
  212. if (IsEqualIID(riid, IID_IADsWinNTSystemInfo)) {
  213. RRETURN(S_OK);
  214. } else {
  215. RRETURN(S_FALSE);
  216. }
  217. }
  218. HRESULT
  219. CWinNTSystemInfo::get_UserName(
  220. BSTR * bstrUserName
  221. )
  222. {
  223. PWSTR pszUserName = NULL;
  224. ULONG uLength;
  225. HRESULT hr;
  226. //
  227. // Validate parameters
  228. //
  229. if ( !bstrUserName )
  230. {
  231. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  232. }
  233. //
  234. // Get length of buffer to be allocated
  235. //
  236. uLength = 0;
  237. GetUserName(NULL, &uLength);
  238. if (uLength > 0)
  239. {
  240. //
  241. // Allocate memory and do the real work
  242. //
  243. pszUserName = (PWSTR)AllocADsMem(uLength * sizeof(WCHAR));
  244. if (!pszUserName)
  245. {
  246. RRETURN(E_OUTOFMEMORY);
  247. }
  248. if (GetUserName(pszUserName, &uLength))
  249. {
  250. hr = ADsAllocString(pszUserName, bstrUserName);
  251. BAIL_ON_FAILURE(hr);
  252. }
  253. else
  254. hr = HRESULT_FROM_WIN32(GetLastError());
  255. }
  256. else
  257. hr = HRESULT_FROM_WIN32(GetLastError());
  258. error:
  259. if (pszUserName)
  260. {
  261. FreeADsMem(pszUserName);
  262. }
  263. RRETURN(hr);
  264. }
  265. HRESULT
  266. CWinNTSystemInfo::get_ComputerName(
  267. BSTR * bstrComputerName
  268. )
  269. {
  270. PWSTR pszComputerName = NULL;
  271. ULONG uLength;
  272. HRESULT hr;
  273. //
  274. // Validate parameters
  275. //
  276. if (!bstrComputerName)
  277. {
  278. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  279. }
  280. //
  281. // Get length of buffer to be allocated
  282. //
  283. uLength = 0;
  284. GetComputerName(NULL, &uLength);
  285. if (uLength > 0)
  286. {
  287. //
  288. // Allocated memory and do the real work
  289. //
  290. pszComputerName = (PWSTR)AllocADsMem(uLength * sizeof(WCHAR));
  291. if (!pszComputerName)
  292. {
  293. RRETURN(E_OUTOFMEMORY);
  294. }
  295. if (GetComputerName(pszComputerName, &uLength))
  296. {
  297. hr = ADsAllocString(pszComputerName, bstrComputerName);
  298. BAIL_ON_FAILURE(hr);
  299. }
  300. else
  301. hr = HRESULT_FROM_WIN32(GetLastError());
  302. }
  303. else
  304. hr = HRESULT_FROM_WIN32(GetLastError());
  305. error:
  306. if (pszComputerName)
  307. {
  308. FreeADsMem(pszComputerName);
  309. }
  310. RRETURN(hr);
  311. }
  312. HRESULT
  313. CWinNTSystemInfo::get_DomainName(
  314. BSTR * bstrDomainName
  315. )
  316. {
  317. PWKSTA_USER_INFO_1 pInfo = NULL;
  318. PWKSTA_USER_INFO_1A pInfoA = NULL;
  319. DWORD err;
  320. HRESULT hr = S_OK;
  321. PWSTR pszDomainName = NULL;
  322. //
  323. // Validate parameters
  324. //
  325. if (!bstrDomainName )
  326. {
  327. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  328. }
  329. //
  330. // Call NetWkstaUserGetInfo to find domain name
  331. //
  332. #if (defined WIN95)
  333. err = NetWkstaUserGetInfoA(NULL, 1, (LPBYTE *) &pInfoA);
  334. if (err != ERROR_SUCCESS)
  335. {
  336. RRETURN(HRESULT_FROM_WIN32(err));
  337. }
  338. pszDomainName = (PWSTR)AllocADsMem((lstrlenA(pInfoA->wkui1_logon_domain) + 1) * sizeof(WCHAR));
  339. if (!pszDomainName)
  340. {
  341. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  342. }
  343. if (MultiByteToWideChar(CP_ACP,
  344. MB_PRECOMPOSED,
  345. pInfoA->wkui1_logon_domain,
  346. -1,
  347. pszDomainName,
  348. lstrlenA(pInfoA->wkui1_logon_domain) + 1) == 0)
  349. {
  350. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(GetLastError()));
  351. }
  352. hr = ADsAllocString(pszDomainName, bstrDomainName);
  353. BAIL_ON_FAILURE(hr);
  354. #else
  355. err = NetWkstaUserGetInfo(NULL, 1, (LPBYTE*) &pInfo);
  356. if (err != ERROR_SUCCESS)
  357. {
  358. RRETURN(HRESULT_FROM_WIN32(err));
  359. }
  360. hr = ADsAllocString((LPWSTR)pInfo->wkui1_logon_domain, bstrDomainName);
  361. BAIL_ON_FAILURE(hr);
  362. #endif
  363. error:
  364. if (pInfo)
  365. {
  366. NetApiBufferFree(pInfo);
  367. }
  368. if (pInfoA)
  369. {
  370. NetApiBufferFree(pInfoA);
  371. }
  372. if (pszDomainName)
  373. {
  374. FreeADsMem(pszDomainName);
  375. }
  376. RRETURN(hr);
  377. }
  378. HRESULT
  379. CWinNTSystemInfo::get_PDC(
  380. BSTR * bstrPDC
  381. )
  382. {
  383. PWSTR pszPDC = NULL;
  384. HRESULT hr;
  385. DWORD err;
  386. //
  387. // Validate parameters
  388. //
  389. if (!bstrPDC )
  390. {
  391. RRETURN(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
  392. }
  393. err = NetGetDCName(NULL, NULL, (LPBYTE*)&pszPDC);
  394. if (err != ERROR_SUCCESS)
  395. {
  396. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  397. }
  398. hr = ADsAllocString(&pszPDC[2], bstrPDC); // remove '\\';
  399. BAIL_ON_FAILURE(hr);
  400. error:
  401. if (pszPDC)
  402. {
  403. NetApiBufferFree(pszPDC);
  404. }
  405. return hr;
  406. }
  407. STDMETHODIMP
  408. CWinNTSystemInfoCF::CreateInstance(
  409. IUnknown * pUnkOuter,
  410. REFIID iid,
  411. LPVOID * ppv
  412. )
  413. {
  414. HRESULT hr = E_FAIL;
  415. if (pUnkOuter)
  416. RRETURN(E_FAIL);
  417. hr = CWinNTSystemInfo::CreateWinNTSystemInfo(
  418. iid,
  419. ppv
  420. );
  421. RRETURN(hr);
  422. }