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.

185 lines
4.6 KiB

  1. #include "stdafx.h"
  2. #include "VolCom.h"
  3. #include "DataIoCl.h"
  4. #include "PostMsgC.h"
  5. STDMETHODIMP EsiVolumeDataObject::SetData(LPFORMATETC pformatetc,
  6. STGMEDIUM FAR * pmedium,
  7. BOOL fRelease)
  8. {
  9. WPARAM wpPostCommand;
  10. DWORD dwGlobalSize;
  11. char FAR* pstrSrc;
  12. PCHAR pDataIn;
  13. // We only support CF_TEXT
  14. if (pformatetc->cfFormat != CF_TEXT)
  15. return E_FAIL;
  16. // We want memory only.
  17. if (pformatetc->tymed != TYMED_HGLOBAL)
  18. return E_FAIL;
  19. // Check for valid memory handle.
  20. if(pmedium->hGlobal == NULL)
  21. return E_FAIL;
  22. // Get the size of the incoming data.
  23. dwGlobalSize = (DWORD)GlobalSize(pmedium->hGlobal);
  24. // Allocate enough memory for the incoming data.
  25. hDataIn = GlobalAlloc(GHND,dwGlobalSize);
  26. EE_ASSERT(hDataIn);
  27. // Lock and get pointers to the data.
  28. pDataIn = (PCHAR)GlobalLock(hDataIn);
  29. EE_ASSERT(pDataIn);
  30. pstrSrc = (char FAR*)GlobalLock(pmedium->hGlobal);
  31. EE_ASSERT(pstrSrc);
  32. // Copy the data to this processes memory.
  33. CopyMemory(pDataIn, pstrSrc, dwGlobalSize);
  34. // Unlock and release the pointer to the source memory.
  35. GlobalUnlock(pmedium->hGlobal);
  36. // Release the memory if requested by the caller.
  37. if (fRelease)
  38. ReleaseStgMedium(pmedium);
  39. DATA_IO* pDataIo = (DATA_IO*)pDataIn;
  40. // Extract the Post Command message
  41. wpPostCommand = pDataIo->wparam;
  42. // Check ESI data structre ID which is always = 0x4553 'ES'
  43. if(pDataIo->dwID != ESI_DATA_STRUCTURE)
  44. return FALSE;
  45. // Check the data structure type.
  46. if(pDataIo->dwType != FR_COMMAND_BUFFER)
  47. return FALSE;
  48. // Check for data structure compatibility.
  49. if(pDataIo->dwCompatibilty != FR_COMMAND_BUFFER_ONE)
  50. return FALSE;
  51. // Unlock the memory.
  52. GlobalUnlock(hDataIn);
  53. // Check for any data.
  54. if( pDataIo->ulDataSize == 0 )
  55. {
  56. EH_ASSERT(!GlobalFree(hDataIn));
  57. hDataIn = NULL;
  58. }
  59. // Send the data to the message pump.
  60. // NOTE THAT THE MEMORY MUST FREED BY THE PROCESSING FUNCTION.
  61. // If we use DataIo with a console application, then we cannot use the WNT
  62. // PostMessage() routine as there is No window to post the message to, so
  63. // we will use a locally created PostMessageConsole() routine instead.
  64. m_pVolOwner->PostMessageLocal(NULL, WM_COMMAND, wpPostCommand, (LPARAM)hDataIn);
  65. return S_OK;
  66. }
  67. STDMETHODIMP EsiVolumeClassFactory::CreateInstance(LPUNKNOWN punkOuter, REFIID riid, void** ppv)
  68. {
  69. LPUNKNOWN punk;
  70. HRESULT hr;
  71. *ppv = NULL;
  72. // Check for aggregation - we don't support it..
  73. if ( punkOuter != NULL )
  74. return CLASS_E_NOAGGREGATION;
  75. //
  76. // Create the volume data object.
  77. //
  78. punk = new EsiVolumeDataObject( m_pVolOwner );
  79. // If we didn't get a pointer then we are out of memory.
  80. if (punk == NULL)
  81. return E_OUTOFMEMORY;
  82. // Get a pointer to the ESI Data Object interface.
  83. hr = punk->QueryInterface(riid, ppv);
  84. if (SUCCEEDED(hr) && (*ppv)) {
  85. if (m_pVolOwner->m_pIDataObject) {
  86. CoDisconnectObject((LPUNKNOWN)m_pVolOwner->m_pIDataObject, 0);
  87. m_pVolOwner->m_pIDataObject->Release();
  88. m_pVolOwner->m_pIDataObject = NULL;
  89. }
  90. m_pVolOwner->m_pIDataObject = (IDataObject *)*ppv;
  91. m_pVolOwner->m_pIDataObject->AddRef();
  92. }
  93. // Release the pointer to the ESI Data Object.
  94. punk->Release();
  95. return hr;
  96. }
  97. //
  98. // Initialize the server side communication only for volumes.
  99. // Warning! The class will nuke the class factory in the destructor.
  100. // Make sure the client has achieved communication before this
  101. // function goes out of scope.
  102. //
  103. const BOOL CVolume::InitializeDataIo( DWORD dwRegCls )
  104. {
  105. HRESULT hr;
  106. BOOL fSuccess = FALSE;
  107. //
  108. // Don't do this again if we've already created and
  109. // registered a factory.
  110. //
  111. if ( m_pFactory == NULL )
  112. {
  113. //
  114. // Allocate the factory.
  115. //
  116. m_pFactory = new EsiVolumeClassFactory( this );
  117. if ( m_pFactory )
  118. {
  119. // Register the class-object with OLE.
  120. hr = CoRegisterClassObject( m_VolumeID,
  121. m_pFactory,
  122. CLSCTX_SERVER,
  123. dwRegCls,
  124. &m_dwRegister );
  125. if ( SUCCEEDED( hr ) )
  126. fSuccess = TRUE;
  127. }
  128. }
  129. else
  130. {
  131. //
  132. // Since we're already started, go ahead and indicate
  133. // success.
  134. //
  135. fSuccess = TRUE;
  136. }
  137. return( fSuccess );
  138. }
  139. //
  140. // Generate a random guid for communication purposes.
  141. //
  142. BOOL CVolume::InitVolumeID()
  143. {
  144. BOOL fSuccess = FALSE;
  145. if ( SUCCEEDED( CoCreateGuid( &m_VolumeID ) ) )
  146. fSuccess = TRUE;
  147. return( fSuccess );
  148. }