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.

284 lines
7.1 KiB

  1. /**************************************************************************************************
  2. FILENAME: DataIoClient.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. */
  5. #define INC_OLE2
  6. #include "stdafx.h"
  7. #ifndef SNAPIN
  8. #include <windows.h>
  9. #endif
  10. #include <stdio.h>
  11. #include "DataIo.h"
  12. #include "DataIoCl.h"
  13. #include "Message.h"
  14. #include "ErrMacro.h"
  15. MULTI_QI mq;
  16. /**************************************************************************************************
  17. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  18. ROUTINE DESCRIPTION:
  19. This module inititalizes the DCOM DataIo Client communications.
  20. INPUT:
  21. None.
  22. RETURN:
  23. TRUE - Success.
  24. FALSE - Failure to initilize.
  25. */
  26. BOOL
  27. InitializeDataIoClient(
  28. IN REFCLSID rclsid,
  29. IN PTCHAR pMachine,
  30. IN OUT LPDATAOBJECT* ppstm
  31. )
  32. {
  33. // Check if we already have a pointer to this DCOM server.
  34. if(*ppstm != NULL) {
  35. Message(TEXT("InitializeDataIoClient - called with non-NULL pointer"), -1, NULL);
  36. return FALSE;
  37. }
  38. HRESULT hr;
  39. // TCHAR wsz [200];
  40. COSERVERINFO sServerInfo;
  41. ZeroMemory(&sServerInfo, sizeof(sServerInfo));
  42. /*
  43. if(pMachine != NULL) {
  44. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pMachine, -1, wsz, 200);
  45. sServerInfo.pwszName = wsz;
  46. }
  47. */
  48. // Initialize the Multi QueryInterface structure.
  49. mq.pIID = &IID_IDataObject;
  50. mq.pItf = NULL;
  51. mq.hr = S_OK;
  52. // Create a remote instance of the object on the argv[1] machine
  53. hr = CoCreateInstanceEx(rclsid,
  54. NULL,
  55. CLSCTX_SERVER,
  56. &sServerInfo,
  57. 1,
  58. &mq);
  59. // Message(TEXT("InitializeDataIoClient - CoCreateInstanceEx"), hr, NULL);
  60. // Check for failure.
  61. if (FAILED(hr)) {
  62. return FALSE;
  63. }
  64. // Return the pointer to the server.
  65. *ppstm = (IDataObject*)mq.pItf;
  66. return TRUE;
  67. }
  68. /**************************************************************************************************
  69. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  70. ROUTINE DESCRIPTION:
  71. INPUT:
  72. RETURN:
  73. TRUE - Success.
  74. FALSE - Failure.
  75. typedef struct {
  76. WORD dwID; // ESI data structre ID always = 0x4553 'ES'
  77. WORD dwType; // Type of data structure
  78. WORD dwVersion; // Version number
  79. WORD dwCompatibilty;// Compatibilty number
  80. ULONG ulDataSize; // Data size
  81. WPARAM wparam; // LOWORD(wparam) = Command
  82. TCHAR cData; // Void pointer to the data - NULL = no data
  83. } DATA_IO, *PDATA_IO;
  84. */
  85. BOOL
  86. DataIoClientSetData(
  87. IN WPARAM wparam,
  88. IN PTCHAR pData,
  89. IN DWORD dwDataSize,
  90. IN LPDATAOBJECT pstm
  91. )
  92. {
  93. // Check for DCOM pointer to the server.
  94. if(pstm == NULL) {
  95. return FALSE;
  96. }
  97. HRESULT hr;
  98. HANDLE hData;
  99. DATA_IO* pDataIo;
  100. // Allocate and lock enough memory for the ESI data structure and the data being sent.
  101. hData = GlobalAlloc(GHND,dwDataSize + sizeof(DATA_IO));
  102. EF_ASSERT(hData);
  103. pDataIo = (DATA_IO*)GlobalLock(hData);
  104. EF_ASSERT(pDataIo);
  105. // Fill in the ESI data structure.
  106. pDataIo->dwID = ESI_DATA_STRUCTURE; // ESI data structre ID always = 0x4553 'ES'
  107. pDataIo->dwType = FR_COMMAND_BUFFER; // Type of data structure
  108. pDataIo->dwVersion = FR_COMMAND_BUFFER_ONE; // Version number
  109. pDataIo->dwCompatibilty = FR_COMMAND_BUFFER_ONE; // Compatibilty number
  110. pDataIo->ulDataSize = dwDataSize; // Data size
  111. pDataIo->wparam = wparam; // LOWORD(wparam) = Command
  112. // Copy the memory into the buffer, unlock it and
  113. // put the handle into the STGMEDIUM data structure.
  114. CopyMemory((PTCHAR)&pDataIo->cData, pData, dwDataSize);
  115. GlobalUnlock(hData);
  116. FORMATETC formatetc;
  117. STGMEDIUM medium;
  118. // Set up FORMATETC with CF_TEXT and global memory.
  119. formatetc.cfFormat = CF_TEXT;
  120. formatetc.ptd = NULL;
  121. formatetc.dwAspect = DVASPECT_CONTENT;
  122. formatetc.lindex = -1;
  123. formatetc.tymed = TYMED_HGLOBAL;
  124. // Set up STGMEDIUM with global memory and NULL for pUnkForRelease.
  125. // SetData msut then be responsible for freeing the memory.
  126. medium.tymed = TYMED_HGLOBAL;
  127. medium.pUnkForRelease = NULL;
  128. medium.hGlobal = hData;
  129. // Send it all to SetData, telling it that it is responsible for freeing the memory.
  130. hr = pstm->SetData(&formatetc, &medium, TRUE);
  131. // Message(TEXT("DataIoClientSetData - IDataObject::SetData"), hr, NULL);
  132. // Check for failure.
  133. if (FAILED(hr)) {
  134. return FALSE;
  135. }
  136. return TRUE;
  137. }
  138. /*****************************************************************************************************************
  139. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  140. ROUTINE DESCRIPTION:
  141. GLOBALS:
  142. FORMATETC - has been initialized in InitializeDataIoClient.
  143. INPUT:
  144. None.
  145. RETURN:
  146. HGLOBAL - handle to the memory containing the data.
  147. HGLOBAL - NULL = failure.
  148. */
  149. HGLOBAL
  150. DataIoClientGetData(
  151. IN LPDATAOBJECT pstm
  152. )
  153. {
  154. // Check for DCOM pointer to the server.
  155. if(pstm == NULL) {
  156. return NULL;
  157. }
  158. FORMATETC formatetc;
  159. STGMEDIUM medium;
  160. HRESULT hr;
  161. // Zero the STGMEDIUM structure.
  162. ZeroMemory((void*)&medium, sizeof(STGMEDIUM));
  163. // Set up FORMATETC with CF_TEXT and global memory.
  164. formatetc.cfFormat = CF_TEXT;
  165. formatetc.ptd = NULL;
  166. formatetc.dwAspect = DVASPECT_CONTENT;
  167. formatetc.lindex = -1;
  168. formatetc.tymed = TYMED_HGLOBAL;
  169. // Get the data from the object.
  170. hr = pstm->GetData(&formatetc, &medium);
  171. // Message(TEXT("DataIoClientGetData - IDataObject::GetData"), hr, NULL);
  172. // Check for failure.
  173. if (FAILED(hr)) {
  174. return NULL;
  175. }
  176. DWORD dwSize;
  177. HGLOBAL hDataIn;
  178. PTCHAR pDataSource;
  179. PTCHAR pDataIn;
  180. // Allocate and lock enough memory for the data we received.
  181. dwSize = (DWORD)GlobalSize(medium.hGlobal);
  182. hDataIn = GlobalAlloc(GHND, dwSize);
  183. EF_ASSERT(hDataIn);
  184. pDataIn = (PTCHAR)GlobalLock(hDataIn);
  185. EF_ASSERT(hDataIn);
  186. // Get a pointer and lock the source data.
  187. pDataSource = (PTCHAR)GlobalLock(medium.hGlobal);
  188. // Copy the memory into the local buffer.
  189. CopyMemory(pDataIn, pDataSource, dwSize);
  190. // Unlock the memory.
  191. GlobalUnlock(hDataIn);
  192. GlobalUnlock(medium.hGlobal);
  193. // Free the source memory.
  194. ReleaseStgMedium(&medium);
  195. // Return the handle to the memory.
  196. return hDataIn;
  197. }
  198. /*****************************************************************************************************************
  199. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  200. ROUTINE DESCRIPTION:
  201. Exit routine for DataIo
  202. GLOBAL VARIABLES:
  203. INPUT:
  204. None;
  205. RETURN:
  206. */
  207. BOOL
  208. ExitDataIoClient(
  209. IN LPDATAOBJECT* ppstm
  210. )
  211. {
  212. // Release the object.
  213. if(*ppstm != NULL) {
  214. LPDATAOBJECT pstm = *ppstm;
  215. pstm->Release();
  216. *ppstm = NULL;
  217. }
  218. return TRUE;
  219. }