Exemple #1
0
/******************************************************************************
*  ServerStart -- The ServerStart method is called immediately after a
*  real-time data server is instantiated.
*  Parameters: CallbackObject -- interface pointer the RTDServer uses to
*                                indicate new data is available.
*              pfRes -- set to positive value to indicate success.  0 or
*                       negative value indicates failure.
*  Returns: S_OK
*           E_POINTER
*           E_FAIL
******************************************************************************/
STDMETHODIMP RTDServer::ServerStart(IRTDUpdateEvent *CallbackObject,
                                    long *pfRes)
{
    HRESULT hr = S_OK;

    //Check the arguments first
    if ((CallbackObject == NULL) || (pfRes == NULL))
        hr = E_POINTER;

    //if the data thread has already been launched, return an error
    else if (m_dwDataThread != -1) {
        hr = E_FAIL;
        *pfRes = -1;
    }

    //Try to launch the data thread
    else {
        //Marshal the interface to the new thread
        IStream* pMarshalStream = NULL;
        hr = CoMarshalInterThreadInterfaceInStream( IID_IRTDUpdateEvent,
                CallbackObject, &pMarshalStream );

        CreateThread( NULL, 0, RealTimeDataThread, (void*)pMarshalStream, 0,
                      &m_dwDataThread );
        *pfRes = m_dwDataThread;
    }

    return hr;
}
Exemple #2
0
void CPyCOMTest::CreatePyCOMTestSession(PyCOMTestSessionData& rs)
{
	DWORD dwThreadID = 0;
	_ASSERTE(rs.m_hEvent == NULL);
	_ASSERTE(rs.m_hThread == NULL);
	_ASSERTE(rs.pStream == NULL);
	rs.m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_IPyCOMTest, (IPyCOMTest *)this, &rs.pStream );
	_ASSERTE(SUCCEEDED(hr) && rs.pStream != NULL);
	rs.m_hThread = CreateThread(NULL, 0, &PyCOMTestSessionThreadEntry, &rs, 0, &dwThreadID);
}
Exemple #3
0
CSaveDataBase::CSaveDataBase(bstr_t &SID, IComHistIntPtr &pHistory, IMessagesPtr &pMessage, DWORD dwNotifyMessage, HWND MessageParent)
{
    pHistoryStream = NULL;
	pMessagesStream = NULL;
	hWindow   = MessageParent;
	dwMessage = dwNotifyMessage;
	LoadSID =  SID;
	hr = S_OK;
	if(SUCCEEDED(hr))
		hr = CoMarshalInterThreadInterfaceInStream(__uuidof(IComHistInt),
		pHistory,&pHistoryStream);
	if(SUCCEEDED(hr))
		hr = CoMarshalInterThreadInterfaceInStream(__uuidof(IMessages),
		pMessage,&pMessagesStream);
	if(SUCCEEDED(hr))
	{
		hWorkThread = CreateThread(NULL,5476UL,thThread,(LPVOID)this,NORMAL_PRIORITY_CLASS,&IDWorkThread);
	}
	hExitEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
	bExit      = FALSE;
}
Exemple #4
0
BOOL CCoolInet2::WorkSession()
{
	try
	{
		CreateSession();
		InitEventContainer();

		HANDLE MasEvent[2]={MarshalEvent,hExitEvent};

		SetEvent(hStartEvent);

		while(TRUE)
		{
            MSG msg ;			

			while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				DispatchMessage(&msg);
			}

			DWORD dwResult = MsgWaitForMultipleObjects(2,MasEvent,FALSE,INFINITE,QS_ALLINPUT);
			
			if((dwResult - WAIT_OBJECT_0 ) ==0)
			{
				HRESULT hr = CoMarshalInterThreadInterfaceInStream(__uuidof(ISession),p_InetSession,ppSession);
				
				if(FAILED(hr))
					ppSession = NULL;

				ResetEvent(MarshalEvent);
				SetEvent(hMarshalingEndEvent);
			}
			if((dwResult - WAIT_OBJECT_0 )==1) 
			{
				//ASSERT(FALSE);
				break;
			}
            /// Обработка Сообщений Windows...    
		}
		CloseEventContainer();
		CloseSession();
	}
	catch(...)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	SetEvent(hExitWork);

	return TRUE;
}
Exemple #5
0
JNIEXPORT int JNICALL Java_org_racob_com_DispatchProxy_MarshalIntoStream
  (JNIEnv *env, jobject _this, jint pointer)
{
  IDispatch *pIDispatch = (IDispatch *) pointer;
  if (!pIDispatch) return 0;
  
  IStream *ps; // this is the stream we will marshall into
  HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_IDispatch, pIDispatch, &ps);
  if (!SUCCEEDED(hr)) {
    ThrowComFail(env, "Could not Marshal Dispatch into IStream", hr);
    return 0;
  }

  return (jint) ps;
}
JNIEXPORT void JNICALL Java_com_jacob_com_DispatchProxy_MarshalIntoStream
  (JNIEnv *env, jobject _this, jobject disp)
{
  IDispatch *pIDispatch = extractDispatch(env, disp);
  if (!pIDispatch) return;
  IStream *ps; // this is the stream we will marshall into
  HRESULT hr = CoMarshalInterThreadInterfaceInStream(
                 IID_IDispatch, pIDispatch, &ps);
  if (!SUCCEEDED(hr))
  {
    ThrowComFail(env, "Could not Marshal Dispatch into IStream", hr);
    return;
  }
  // store the stream pointer on the object
  jclass argClass = env->GetObjectClass(_this);
  jfieldID ajf = env->GetFieldID( argClass, "m_pStream", "I");
  env->SetIntField(_this, ajf, (jint)ps);
}
//------------------------------------------------------------------*
HRESULT CFCMessageSessionThread::StartThread(CFCMessageSession *pRawMsgSession, long cookie)
{
    HRESULT hr = pRawMsgSession->get_MachineName(&m_sNodeName);
    if (FAILED(hr))
      return hr;
    hr = CoMarshalInterThreadInterfaceInStream(IID__FCMessageSessionNotificationInternal,
                                               (_FCMessageSessionNotificationInternal*)pRawMsgSession,
                                               &m_pMsgSessionStream);
    if (FAILED(hr))
      return hr;

    pRawMsgSession->AddRef();
    m_pRawMsgSession = pRawMsgSession;
    m_cookie = cookie;

    AddRef();
    DWORD threadID;
    HANDLE hThread = CreateThread(NULL, 0, ThreadMain, (LPVOID)this, 0, &threadID);
    if (hThread == NULL)
      return E_FAIL;

    CloseHandle(hThread);
    return S_OK;
}
HRESULT STDMETHODCALLTYPE C[!output Safe_root]::Buy( 
   IWMPContentContainerList *pInfo,
   DWORD cookie)
{
   BUY_CONTEXT* pBuyCtx = NULL; 
   BOOL postResult = FALSE;
   HRESULT hr = S_OK;
   
   if(NULL == pInfo)
   {
      hr = E_INVALIDARG;
      goto cleanup;
   }

   // If the buy thread has not already been started,
   // start it now.
   
   if(!m_buyThreadHandle)
   {  
      hr = this->StartContentPartnerThread(ThreadTypeBuy);
      if(FAILED(hr))
      {
         ATLTRACE2("%x: Buy: StartContentPartnerThread failed. %x\n", GetCurrentThreadId(), hr);
         goto cleanup;
      } 
      
      ATLTRACE2("%x: Buy: StartContentPartnerThread succeeded.\n", GetCurrentThreadId());    
   }

   // At this point, we know the buy thread started, but
   // we don't know whether it is still active.

   // When we post a buy message, we must provide
   // two things: a cookie that represents the purchase and an
   // IStream interface that the buy thread can use to
   // obtain an IWMPContentContainerList interface. Those two
   // things are the members of BUY_CONTEXT.

   pBuyCtx = new BUY_CONTEXT();

   if(NULL == pBuyCtx)
   {
      ATLTRACE2("%x: Buy: Failed to create new BUY_CONTEXT.\n", GetCurrentThreadId());
      hr = E_OUTOFMEMORY;
      goto cleanup;
   }

   ZeroMemory(pBuyCtx, sizeof(BUY_CONTEXT));
   
   hr = CoMarshalInterThreadInterfaceInStream(
      __uuidof(IWMPContentContainerList),
      pInfo,
      &pBuyCtx->pIStream); 

   if(FAILED(hr))
   {
      ATLTRACE2("%x: Buy: Failed to marshal IWMPContentContainerList interface. %x\n", GetCurrentThreadId(), hr);
      goto cleanup;
   } 

   if(NULL == pBuyCtx->pIStream)
   {
      hr = E_UNEXPECTED;
      goto cleanup;
   }

   ATLTRACE2("%x: Buy: Successfully marshaled IWMPContentContainerList interface.\n", GetCurrentThreadId());
  
   pBuyCtx->cookie = cookie;

   // If the buy thread is not active, the following
   // call to PostThreadMessage will fail.

   postResult = PostThreadMessage(
      m_buyThreadId,
      m_msgBuy,
      0,
      reinterpret_cast<LPARAM>(pBuyCtx) );

   if(0 == postResult)
   {
      hr = HRESULT_FROM_WIN32(GetLastError());
      ATLTRACE2("%x: Buy: PostThreadMessage failed. %x\n", GetCurrentThreadId(), hr);        
      goto cleanup;
   }
   
   ATLTRACE2("%x: Buy: PostThreadMessage %x succeeded.\n", GetCurrentThreadId());

   // We successfully posted the message to the buy thread.
   // We have no more need for the pointer to the buy context.
   pBuyCtx = NULL;

   // The buy thread must free the memory pointed to by pBuyCtx.

cleanup:

   if(pBuyCtx)
   {
      // We failed to post a message to the buy thread.
      // The buy thread will not be able to free the memory
      // pointed to by pBuyCtx.  So we free it here.      
      delete pBuyCtx;
      pBuyCtx = NULL;
   }

   // If pBuyCtx is NULL, buy thread will free the memory 
   // pointed to by pBuyCtx.  

   return hr;
}
HRESULT STDMETHODCALLTYPE C[!output Safe_root]::StartContentPartnerThread(ContentPartnerThreadType threadType)
{
   DWORD waitResult = 0;
   HRESULT hr = S_OK;
   CONTENT_PARTNER_THREAD_CONTEXT* pThreadCtx = NULL;
   HANDLE newThreadHandle = NULL;
   DWORD newThreadId = 0;

   // What does the new thread need to get started?
   //    1. A member of the ContentPartnerThreadType enumeration
   //    2. IStream that it can use to unmarshal IWMPContentPartnerCallback
   //    3. Event handle it can use to signal that the message loop is started
   //    4. ID of the message to exit the message loop 
   //    5. IDs messages that will be sent to the thread   
   // Items 1 - 4 are the first four members of the CONTENT_PARTNER_THREAD_CONTEXT structure.
   // The fifth member of the CONTENT_PARTNER_THREAD_CONTEXT structure is a union.
   // It holds the IDs of messages that will be sent to the thread.

   pThreadCtx = new CONTENT_PARTNER_THREAD_CONTEXT();
   // This memory is freed in ContentPartnerThreadProc.


   if(!pThreadCtx)
   {
      ATLTRACE2("%x: StartThread(type %d): Failed to create CONTENT_PARTNER_THREAD_CONTEXT.\n", GetCurrentThreadId(), threadType);
      hr = E_OUTOFMEMORY;
      goto cleanup;
   }

   ZeroMemory(pThreadCtx, sizeof(CONTENT_PARTNER_THREAD_CONTEXT));

   pThreadCtx->threadType = threadType;
      
   hr = CoMarshalInterThreadInterfaceInStream(
      __uuidof(IWMPContentPartnerCallback), 
      m_spCallback, 
      &pThreadCtx->pIStream);

   if(FAILED(hr))
   {
      ATLTRACE2("%x: StartThread(type %d): Failed to marshal IWMPContentPartnerCallback interface. %x\n", GetCurrentThreadId(), threadType, hr);
      goto cleanup; 
   }

   if(NULL == pThreadCtx->pIStream)
   {
      hr = E_UNEXPECTED;
      goto cleanup;
   }

   pThreadCtx->hInitialized = CreateEvent(NULL, TRUE, FALSE, NULL);

   if(NULL == pThreadCtx->hInitialized)
   {
      ATLTRACE2("%x: StartThread(type %d): CreateEvent failed.\n", GetCurrentThreadId(), threadType);
      hr = E_OUTOFMEMORY;
      goto cleanup;
   }
 
   pThreadCtx->msgExitMessageLoop = m_msgExitMessageLoop;

   switch(threadType)
   {
   case ThreadTypeDownload:
      pThreadCtx->downloadThreadContext.msgDownloadBatch = m_msgDownloadBatch;  
      break;
   case ThreadTypeBuy:
      pThreadCtx->buyThreadContext.msgBuy = m_msgBuy;    
      break;
   case ThreadTypeRefreshLicense:
      pThreadCtx->refreshLicenseThreadContext.msgRefreshLicense = m_msgRefreshLicense;  
      break;
   case ThreadTypeLogin:
      pThreadCtx->loginThreadContext.msgAuthenticate = m_msgAuthenticate; 
      pThreadCtx->loginThreadContext.msgLogin = m_msgLogin;
      pThreadCtx->loginThreadContext.msgLogout = m_msgLogout; 
      break;
   case ThreadTypeSendMessage:
      pThreadCtx->sendMessageThreadContext.msgSendMessage = m_msgSendMessage;  
      break;
   case ThreadTypeUpdateDevice:
      pThreadCtx->updateDeviceThreadContext.msgUpdateDevice = m_msgUpdateDevice;  
      break;
   case ThreadTypeList:
      pThreadCtx->listThreadContext.msgGetListContents = m_msgGetListContents;
      break;
   default:
      ATLTRACE2("%x: StartThread: Unrecognized thread type.\n", GetCurrentThreadId(), 0);
      hr = E_INVALIDARG;
      goto cleanup;
   } // switch(threadType)

   // At this point, all members of pThreadCtx are initialized.

   newThreadHandle = CreateThread(
         NULL,
         0,
         ContentPartnerThreadProc,
         static_cast<LPVOID>(pThreadCtx),
         0,
         &newThreadId);

   // The documentation for CreateThread says this:
   // "If this function fails, the return value is NULL."
   if(NULL == newThreadHandle)
   {
      hr = HRESULT_FROM_WIN32(GetLastError());
      ATLTRACE2("%x: StartThread(type %d): CreateThread failed. %x\n", threadType, GetCurrentThreadId(), hr);   
      goto cleanup;
   }

   // The new thread handle is closed in ShutdownThreads.

   // Store the new thread handle and thread ID.
   switch(threadType)
   {
   case ThreadTypeDownload:
      m_downloadThreadHandle = newThreadHandle;
      m_downloadThreadId = newThreadId;  
      break;
   case ThreadTypeBuy:
      m_buyThreadHandle = newThreadHandle;
      m_buyThreadId = newThreadId;   
      break;
   case ThreadTypeRefreshLicense:
      m_refreshLicenseThreadHandle = newThreadHandle;
      m_refreshLicenseThreadId = newThreadId;    
      break;
   case ThreadTypeLogin:
      m_loginThreadHandle = newThreadHandle;
      m_loginThreadId = newThreadId;  
      break;
   case ThreadTypeSendMessage:
      m_sendMessageThreadHandle = newThreadHandle;
      m_sendMessageThreadId = newThreadId;  
      break;

   case ThreadTypeUpdateDevice:
      m_updateDeviceThreadHandle = newThreadHandle;
      m_updateDeviceThreadId = newThreadId;  
      break;

   case ThreadTypeList:
      m_listThreadHandle = newThreadHandle;
      m_listThreadId = newThreadId;  
      break;

   default:
      ATLTRACE2("%x: StartThread: Unrecognized thread type.\n", GetCurrentThreadId(), 0);
      hr = E_INVALIDARG;
      goto cleanup;
   } // switch(threadType)
   
   // Wait for the new thread to signal that it is initialized.
  
   waitResult = WaitForSingleObject(pThreadCtx->hInitialized, 30000);

   if(WAIT_FAILED == waitResult)
   {
      hr = HRESULT_FROM_WIN32(GetLastError());   
      goto cleanup;
   }

   ATLTRACE2("%x: StartThread: Thread(type %d) signaled that it is initialized.\n", GetCurrentThreadId(), threadType);

   // At this point, we know the new thread called SetEvent to
   // let us know that we can stop waiting.
   // But that could mean one of two things:
   //   1. The new thread called SetEvent and then entered its message loop.
   //   2. The new thread failed to enter its message loop and 
   //      called SetEvent as part of its cleanup code.
   //
   // The point is this: we know that the new thread started, but we
   // do not know whether the new thread is still active.
   
 cleanup:

   if(pThreadCtx->hInitialized)
   {
      CloseHandle(pThreadCtx->hInitialized);
      pThreadCtx->hInitialized = 0;
   }

   if(NULL == newThreadHandle)
   {
      // The new thread did not start; that is, CreateThread failed.
      // The new thread will not be able to free the memory pointed
      // to by pThreadCtx, so we free it here.
 
      if(pThreadCtx)
      {
         delete pThreadCtx;
         pThreadCtx = NULL;
      }
   }

   return hr;

} // StartContentPartnerThread
Exemple #10
0
/*
 * Use the windows message queue as a thread-safe way to serialize calls.
 */
DWORD WINAPI ScriptThread::scriptThread(/*LPVOID lpInput*/)
{
	AtlTrace("Creating script thread\n");

	// We want a single threaded apartment.
	CoInitialize(NULL);

	// We need a hidden window so we can post messages with a window ID
	// otherwise we can't do modal dialogs.
	HiddenWindow *window = new HiddenWindow();
	RECT whatever = { 0, 0, 0, 0 };
	hWnd = window->Create(NULL, whatever, "HiddenWindow", 0);
	AtlTrace("Window created\n");

	IScriptController *localScript;
	HRESULT hr = CoCreateInstance(CLSID_ScriptController, NULL, CLSCTX_INPROC_SERVER, IID_IScriptController, (void**)&localScript);
	if (SUCCEEDED(hr))
	{
		// Marshal the pointer for other apartments too.
		HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_IScriptController, localScript, &scriptStream);

		window->setScript(localScript);

		SetEvent(hScriptEvent);	// Wake up the (possibly) waiting apartment.

		MSG msg;
		BOOL bRet;
		AtlTrace("Entering message loop\n");

		while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
		{
//_ATL_DEBUG_INTERFACES
			AtlTrace("Got ScriptThread message: 0x%0.4x\n", msg.message);
			if (msg.message == HiddenWindow::WINDOW_QUIT)
			{
				AtlTrace("It was a quit message\n");
				PostQuitMessage(0);	// No. Really
			}
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}

		AtlTrace("Releasing localScript\n");
		localScript->Release();
	}

	::DestroyWindow(window->m_hWnd);
	delete window;

	AtlTrace("Exiting Script thread\n");
	//Exit the thread
	CoUninitialize();
	CloseHandle(hScriptEvent);
	CloseHandle(hScriptThread);
	hScriptThread = NULL;

	delete this;	// Only way to guarantee that the object hangs around until
					// the thread exits without waiting on the thread.

	return 0;
}