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.

653 lines
16 KiB

  1. /*++
  2. * File name:
  3. * tclientaxobj.h
  4. * Contents:
  5. * This module contains the header for the CTClientApi class.
  6. *
  7. * Copyright (C) 2002 Microsoft Corp.
  8. --*/
  9. #include "resource.h"
  10. #include <atlctl.h>
  11. #include "gdata.h"
  12. //
  13. // VOID
  14. // SaveError (
  15. // IN PCSTR Error,
  16. // IN DWORD TlsIndex,
  17. // OUT HRESULT *Result
  18. // );
  19. //
  20. #define SaveError(Error, TlsIndex, Result) \
  21. { \
  22. \
  23. /* \
  24. * Set the result code. The presence of an error string indicates \
  25. * failure. \
  26. */ \
  27. \
  28. *(Result) = (Error) == NULL ? S_OK : E_FAIL; \
  29. \
  30. /* \
  31. * Update the current thread's error string. If a TLS index could not \
  32. * be allocated for the current instance, error strings cannot be used. \
  33. */ \
  34. \
  35. if ((TlsIndex) != TLS_OUT_OF_INDEXES) \
  36. { \
  37. \
  38. /* \
  39. / If a TLS index was allocated, setting the value should not fail. \
  40. */ \
  41. \
  42. if (!RTL_VERIFY(TlsSetValue((TlsIndex), (PVOID)(Error)))) \
  43. { \
  44. *(Result) = HRESULT_FROM_WIN32(GetLastError()); \
  45. } \
  46. } \
  47. } \
  48. //
  49. // CTClientApi class.
  50. //
  51. class CTClientApi :
  52. public CComObjectRoot,
  53. public CComCoClass<CTClientApi, &CLSID_CTClient>,
  54. public CComControl<CTClientApi>,
  55. public IDispatchImpl<ITClientApi, &IID_ITClientApi, &LIBID_TCLIENTAXLib>,
  56. public IPersistStreamInitImpl<CTClientApi>,
  57. public IPersistStorageImpl<CTClientApi>,
  58. public IOleControlImpl<CTClientApi>,
  59. public IOleObjectImpl<CTClientApi>,
  60. public IOleInPlaceActiveObjectImpl<CTClientApi>,
  61. public IOleInPlaceObjectWindowlessImpl<CTClientApi>,
  62. public IViewObjectExImpl<CTClientApi> //,
  63. // public IObjectSafetyImpl<CTClientApi, INTERFACESAFE_FOR_UNTRUSTED_CALLER>
  64. {
  65. public:
  66. CTClientApi(
  67. ) :
  68. m_pCI(NULL)
  69. {
  70. //
  71. // Allocate the index for per-thread errors. If the allocation
  72. // fails, error strings will not be available.
  73. //
  74. m_dwErrorIndex = TlsAlloc();
  75. //
  76. // Initialize the allocation lists.
  77. //
  78. #if 0
  79. InitializeListHead(&m_HeapAllocations);
  80. InitializeListHead(&m_SysStringAllocations);
  81. InitializeListHead(&m_TClientAllocations);
  82. #endif
  83. //
  84. // Initialize the global printing routine. It would be preferable to
  85. // set it only if it is currently empty, but Windows 95 compatibility
  86. // is required, and therefore InterlockedCompareExchange would have
  87. // to be dynamically loaded with LoadLibrary.
  88. //
  89. InterlockedExchangePointer((PVOID *)&g_pfnPrintMessage,
  90. CTClientApi::PrintMessage);
  91. }
  92. ~CTClientApi(
  93. )
  94. {
  95. #if 0
  96. PLIST_ENTRY pNextEntry;
  97. PALLOCATION pAllocation;
  98. #endif
  99. //
  100. // If a connection exists, disconnect it.
  101. //
  102. if (m_pCI != NULL)
  103. {
  104. RTL_VERIFY(SCDisconnect(m_pCI) == NULL);
  105. m_pCI = NULL;
  106. }
  107. //
  108. // TODO: The lists aren't used at present, but if they are going to
  109. // be used, an access count and rundown flag should be used to
  110. // protect the following code.
  111. //
  112. #if 0
  113. //
  114. // Free any heap allocations.
  115. //
  116. while (!IsListEmpty(&m_HeapAllocations))
  117. {
  118. //
  119. // Remove the entry from the list, free the allocated memory,
  120. // then free the allocation structure.
  121. //
  122. pNextEntry = RemoveHeadList(&m_HeapAllocations);
  123. ASSERT(pNextEntry != NULL);
  124. pAllocation = CONTAINING_RECORD(pNextEntry,
  125. ALLOCATION,
  126. AllocationListEntry);
  127. ASSERT(pAllocation != NULL && pAllocation->Address != NULL);
  128. RTL_VERIFY(HeapFree(GetProcessHeap(), 0, pAllocation->Address));
  129. RTL_VERIFY(HeapFree(GetProcessHeap(), 0, pAllocation));
  130. }
  131. //
  132. // Free any COM strings that will not be freed by the caller.
  133. //
  134. while (!IsListEmpty(&m_SysStringAllocations))
  135. {
  136. //
  137. // Remove the entry from the list, free the allocated memory,
  138. // then free the allocation structure.
  139. //
  140. pNextEntry = RemoveHeadList(&m_SysStringAllocations);
  141. ASSERT(pNextEntry != NULL);
  142. pAllocation = CONTAINING_RECORD(pNextEntry,
  143. ALLOCATION,
  144. AllocationListEntry);
  145. ASSERT(pAllocation != NULL && pAllocation->Address != NULL);
  146. SysFreeString((BSTR)pAllocation->Address);
  147. RTL_VERIFY(HeapFree(GetProcessHeap(), 0, pAllocation));
  148. }
  149. //
  150. // Free any TClient-allocated memory.
  151. //
  152. while (!IsListEmpty(&m_TClientAllocations))
  153. {
  154. //
  155. // Remove the entry from the list, free the allocated memory,
  156. // then free the allocation structure.
  157. //
  158. pNextEntry = RemoveHeadList(&m_TClientAllocations);
  159. ASSERT(pNextEntry != NULL);
  160. pAllocation = CONTAINING_RECORD(pNextEntry,
  161. ALLOCATION,
  162. AllocationListEntry);
  163. ASSERT(pAllocation != NULL && pAllocation->Address != NULL);
  164. SCFreeMem(pAllocation->Address);
  165. RTL_VERIFY(HeapFree(GetProcessHeap(), 0, pAllocation));
  166. }
  167. #endif
  168. //
  169. // If an error index was allocated, free it.
  170. //
  171. if (m_dwErrorIndex != TLS_OUT_OF_INDEXES)
  172. {
  173. RTL_VERIFY(TlsFree(m_dwErrorIndex));
  174. }
  175. }
  176. //
  177. // Define message map which may be enabled later, if GUI support is added
  178. // (e.g. for logging).
  179. //
  180. #if 0
  181. BEGIN_MSG_MAP(CTClientApi)
  182. MESSAGE_HANDLER(WM_PAINT, OnPaint)
  183. MESSAGE_HANDLER(WM_CREATE, OnCreate)
  184. MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  185. MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
  186. MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
  187. MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
  188. MESSAGE_HANDLER(WM_TIMER, OnTimer)
  189. MESSAGE_HANDLER(WM_SIZE, OnSize)
  190. MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
  191. END_MSG_MAP()
  192. #endif // 0
  193. //
  194. // Define COM map.
  195. //
  196. BEGIN_COM_MAP(CTClientApi)
  197. COM_INTERFACE_ENTRY(IDispatch)
  198. COM_INTERFACE_ENTRY(ITClientApi)
  199. // COM_INTERFACE_ENTRY(IObjectSafety)
  200. COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject, IViewObjectEx)
  201. COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject2, IViewObjectEx)
  202. COM_INTERFACE_ENTRY_IMPL(IViewObjectEx)
  203. COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleWindow, IOleInPlaceObjectWindowless)
  204. COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleInPlaceObject, IOleInPlaceObjectWindowless)
  205. COM_INTERFACE_ENTRY_IMPL(IOleInPlaceObjectWindowless)
  206. COM_INTERFACE_ENTRY_IMPL(IOleInPlaceActiveObject)
  207. COM_INTERFACE_ENTRY_IMPL(IOleControl)
  208. COM_INTERFACE_ENTRY_IMPL(IOleObject)
  209. COM_INTERFACE_ENTRY_IMPL(IPersistStorage)
  210. COM_INTERFACE_ENTRY_IMPL(IPersistStreamInit)
  211. END_COM_MAP()
  212. //
  213. // Define connection-point map.
  214. //
  215. BEGIN_CONNECTION_POINT_MAP(CTClientApi)
  216. END_CONNECTION_POINT_MAP()
  217. //
  218. // Define property map.
  219. //
  220. BEGIN_PROP_MAP(CTClientApi)
  221. END_PROPERTY_MAP()
  222. //
  223. // COM declarations.
  224. //
  225. //DECLARE_NOT_AGGREGATABLE(CTClientApi)
  226. DECLARE_GET_CONTROLLING_UNKNOWN()
  227. //DECLARE_CONTROL_INFO(CLSID_CTClient)
  228. DECLARE_REGISTRY_RESOURCEID(IDR_TClient)
  229. protected:
  230. //
  231. // Define connection information and error index.
  232. //
  233. PCONNECTINFO m_pCI;
  234. DWORD m_dwErrorIndex;
  235. //
  236. // The following lists are used to track allocations from the process and
  237. // CRT heaps. The SysXxxString routines use the CRT heap, which is also
  238. // used for the TClient allocations. General allocations use the process
  239. // heap.
  240. //
  241. // Note: These lists are not currently used.
  242. //
  243. #if 0
  244. LIST_ENTRY m_HeapAllocations;
  245. LIST_ENTRY m_SysStringAllocations;
  246. LIST_ENTRY m_TClientAllocations;
  247. #endif
  248. //
  249. // ITClientApi interface.
  250. //
  251. public:
  252. //
  253. // Declare message handlers which may be enabled later.
  254. //
  255. #if 0
  256. HRESULT
  257. OnDraw (
  258. ATL_DRAWINFO& di
  259. );
  260. LRESULT
  261. OnCreate (
  262. UINT uMsg,
  263. WPARAM wParam,
  264. LPARAM lParam,
  265. BOOL& bHandled
  266. );
  267. LRESULT
  268. OnDestroy (
  269. UINT uMsg,
  270. WPARAM wParam,
  271. LPARAM lParam,
  272. BOOL& bHandled
  273. );
  274. LRESULT
  275. OnLButtonDown (
  276. UINT uMsg,
  277. WPARAM wParam,
  278. LPARAM lParam,
  279. BOOL& bHandled
  280. );
  281. LRESULT
  282. OnLButtonUp (
  283. UINT uMsg,
  284. WPARAM wParam,
  285. LPARAM lParam,
  286. BOOL& bHandled
  287. );
  288. LRESULT
  289. OnMouseMove (
  290. UINT uMsg,
  291. WPARAM wParam,
  292. LPARAM lParam,
  293. BOOL& bHandled
  294. );
  295. LRESULT
  296. OnSize (
  297. UINT uMsg,
  298. WPARAM wParam,
  299. LPARAM lParam,
  300. BOOL& bHandled
  301. )
  302. {
  303. UNREFERENCED_PARAMETER(uMsg);
  304. UNREFERENCED_PARAMETER(wParam);
  305. UNREFERENCED_PARAMETER(lParam);
  306. UNREFERENCED_PARAMETER(bHandled);
  307. return 0;
  308. }
  309. LRESULT
  310. OnEraseBackground (
  311. UINT uMsg,
  312. WPARAM wParam,
  313. LPARAM lParam,
  314. BOOL& bHandled
  315. )
  316. {
  317. UNREFERENCED_PARAMETER(uMsg);
  318. UNREFERENCED_PARAMETER(wParam);
  319. UNREFERENCED_PARAMETER(lParam);
  320. UNREFERENCED_PARAMETER(bHandled);
  321. return 0;
  322. }
  323. LRESULT
  324. OnTimer (
  325. UINT uMsg,
  326. WPARAM wParam,
  327. LPARAM lParam,
  328. BOOL& bHandled
  329. )
  330. {
  331. UNREFERENCED_PARAMETER(uMsg);
  332. UNREFERENCED_PARAMETER(wParam);
  333. UNREFERENCED_PARAMETER(lParam);
  334. UNREFERENCED_PARAMETER(bHandled);
  335. return 0;
  336. }
  337. #endif // 0
  338. STDMETHODIMP
  339. get_Error (
  340. OUT BSTR *Message
  341. )
  342. {
  343. PSTR szError;
  344. BSTR bstrError;
  345. HRESULT hrResult;
  346. USES_CONVERSION;
  347. ATLTRACE(_T("ITClientApi::get_Error\n"));
  348. //
  349. // Get the current thread's error string. If a TLS index could not be
  350. // allocated, an error string cannot be returned.
  351. //
  352. if (m_dwErrorIndex != TLS_OUT_OF_INDEXES)
  353. {
  354. szError = (PSTR)TlsGetValue(m_dwErrorIndex);
  355. }
  356. else
  357. {
  358. szError = NULL;
  359. }
  360. //
  361. // If the error string is NULL or empty, use NULL.
  362. //
  363. if (szError == NULL || *szError == '\0')
  364. {
  365. bstrError = NULL;
  366. }
  367. //
  368. // Convert the current error string to a BSTR. This will allocate
  369. // from the CRT heap, and the storage must be freed by the caller,
  370. // using SysFreeString.
  371. //
  372. else
  373. {
  374. bstrError = A2BSTR(szError);
  375. if (bstrError == NULL)
  376. {
  377. return E_OUTOFMEMORY;
  378. }
  379. }
  380. //
  381. // Copy the address of the error string to the message pointer.
  382. //
  383. hrResult = E_FAIL;
  384. _try
  385. {
  386. _try
  387. {
  388. *Message = bstrError;
  389. hrResult = S_OK;
  390. }
  391. //
  392. // If the message pointer is invalid, set an appropriate return
  393. // value.
  394. //
  395. _except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
  396. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  397. {
  398. hrResult = E_POINTER;
  399. }
  400. }
  401. //
  402. // If the message pointer could not be set, the BSTR will not be
  403. // returned, so free it.
  404. //
  405. _finally
  406. {
  407. if (FAILED(hrResult))
  408. {
  409. ASSERT(bstrError != NULL);
  410. SysFreeString(bstrError);
  411. }
  412. }
  413. return hrResult;
  414. }
  415. STDMETHODIMP
  416. SaveClipboard (
  417. IN BSTR FormatName,
  418. IN BSTR FileName
  419. );
  420. STDMETHODIMP
  421. IsDead (
  422. OUT BOOL *Dead
  423. );
  424. STDMETHODIMP
  425. SendTextAsMessages (
  426. IN BSTR Text
  427. );
  428. STDMETHODIMP
  429. Connect2 (
  430. IN BSTR ServerName,
  431. IN BSTR UserName,
  432. IN BSTR Password,
  433. IN BSTR Domain,
  434. IN BSTR Shell,
  435. IN ULONG XResolution,
  436. IN ULONG YResolution,
  437. IN ULONG ConnectionFlags,
  438. IN ULONG ColorDepth,
  439. IN ULONG AudioOptions
  440. );
  441. STDMETHODIMP
  442. GetFeedbackString (
  443. OUT BSTR *FeedbackString
  444. );
  445. STDMETHODIMP
  446. GetFeedback (
  447. OUT SAFEARRAY **Feedback
  448. );
  449. STDMETHODIMP
  450. ClientTerminate (
  451. VOID
  452. );
  453. STDMETHODIMP
  454. Check (
  455. IN BSTR Command,
  456. IN BSTR Parameter
  457. );
  458. STDMETHODIMP
  459. Clipboard (
  460. IN ULONG Command,
  461. IN BSTR FileName
  462. );
  463. STDMETHODIMP
  464. Connect (
  465. IN BSTR ServerName,
  466. IN BSTR UserName,
  467. IN BSTR Password,
  468. IN BSTR Domain,
  469. IN ULONG XResolution,
  470. IN ULONG YResolution
  471. );
  472. STDMETHODIMP
  473. Disconnect (
  474. VOID
  475. );
  476. STDMETHODIMP
  477. Logoff (
  478. VOID
  479. );
  480. STDMETHODIMP
  481. SendData (
  482. IN UINT Message,
  483. IN UINT_PTR WParameter,
  484. IN LONG_PTR LParameter
  485. );
  486. STDMETHODIMP
  487. Start (
  488. IN BSTR AppName
  489. );
  490. STDMETHODIMP
  491. SwitchToProcess (
  492. IN BSTR WindowTitle
  493. );
  494. STDMETHODIMP
  495. SendMouseClick (
  496. IN ULONG XPosition,
  497. IN ULONG YPosition
  498. );
  499. STDMETHODIMP
  500. GetSessionId (
  501. OUT ULONG *SessionId
  502. );
  503. STDMETHODIMP
  504. CloseClipboard (
  505. VOID
  506. );
  507. STDMETHODIMP
  508. OpenClipboard (
  509. IN HWND Window
  510. );
  511. STDMETHODIMP
  512. SetClientTopmost (
  513. IN BOOL Enable
  514. );
  515. STDMETHODIMP
  516. Attach (
  517. IN HWND Window,
  518. IN LONG_PTR Cookie
  519. );
  520. STDMETHODIMP
  521. Detach (
  522. VOID
  523. );
  524. STDMETHODIMP
  525. GetIni (
  526. OUT ITClientIni **Ini
  527. );
  528. STDMETHODIMP
  529. GetClientWindowHandle (
  530. OUT HWND *Window
  531. );
  532. //
  533. // Utility routines.
  534. //
  535. static
  536. VOID
  537. CTClientApi::PrintMessage (
  538. MESSAGETYPE MessageType,
  539. LPCSTR Format,
  540. ...
  541. );
  542. };