Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC

COOP - Co-operate Library to Transfer Data between Processes

Rate me:
Please Sign up or sign in to vote.
4.40/5 (10 votes)
28 Feb 2010CPOL3 min read 25.3K   964   41  
Co-operate library to start another process and transfer data between processes
// coopframe.cpp
//

#include "stdafx.h"
#include "coop.h"
#include "coopframe.h"

class CoopSessionData
{
public:
	HANDLE m_eThisCancel;
	CWinThread * m_pCoopThread;
	COOPPROC m_pCoopProc;
	LPVOID m_pParam;

	CoopContext m_context;

public:
	CoopSessionData()
	{
		m_eThisCancel = CreateEvent(NULL, FALSE, FALSE, NULL);
		m_pCoopThread = NULL;
		m_pCoopProc   = NULL;
		m_pParam      = NULL;

		memset(&m_context, 0, sizeof(CoopContext));
	}

	~CoopSessionData()
	{
		CloseHandle(m_eThisCancel);

		ResetCoopContext(&m_context);
	}

public:
	static UINT WaitCoopThread(LPVOID pParam)
	{
		TRACE0("���� WaitCoopThread\n");

		CoopSessionData * self = (CoopSessionData*)pParam;

		while(WaitForActivationByCoop(&self->m_context, INFINITE, self->m_eThisCancel))
		{
			if(IsActivatedWithData(&self->m_context))
			{
				self->m_pCoopProc(GetCoopData(&self->m_context, NULL, 0), GetCoopDataSize(&self->m_context), self->m_pParam);
			}

			ReplyCoopFinished(&self->m_context);

		}

		delete self;

		TRACE0("�˳� WaitCoopThread\n");
		return 0;
	}
};

HCOOPSESSION STDCALL GetCoopSession(COOPPROC fnCoopProc, LPVOID pParam)
{
	CoopSessionData * session = new CoopSessionData();

	if(IsStartedByCoop(&session->m_context))
	{
		session->m_pCoopProc   = fnCoopProc;
		session->m_pParam      = pParam;

		// call back
		if(fnCoopProc != NULL) fnCoopProc(GetCoopData(&session->m_context, NULL, 0), GetCoopDataSize(&session->m_context), pParam);

		session->m_pCoopThread = AfxBeginThread(CoopSessionData::WaitCoopThread, session);

		ReplyCoopFinished(&session->m_context);

		return (HCOOPSESSION)session;
	}
	else
	{
		delete session;

		return NULL;
	}
}

HCOOPSESSION STDCALL StartCoopSession(COOPPROC fnCoopProc, LPVOID pParam, LPCTSTR szPath, LPCVOID pData, UINT nSize)
{
	CoopSessionData * session = new CoopSessionData();

	if(StartCoop(&session->m_context, szPath, pData, nSize))
	{
		session->m_pCoopProc   = fnCoopProc;
		session->m_pParam      = pParam;
		session->m_pCoopThread = AfxBeginThread(CoopSessionData::WaitCoopThread, session);

		return (HCOOPSESSION)session;
	}
	else
	{
		delete session;

		return NULL;
	}
}

VOID STDCALL SetCoopSessionProc(HCOOPSESSION hCoopSession, COOPPROC fnCoopProc, LPVOID pParam)
{
	CoopSessionData * session = (CoopSessionData *)hCoopSession;
	if(session != NULL)
	{
		session->m_pCoopProc = fnCoopProc;
		session->m_pParam    = pParam;
	}
}

BOOL STDCALL IsCoopSessionValid(HCOOPSESSION hCoopSession)
{
	CoopSessionData * session = (CoopSessionData *)hCoopSession;
	return hCoopSession != NULL && IsCoopRunning(&session->m_context);
}

VOID STDCALL CloseCoopSession(HCOOPSESSION hCoopSession)
{
	CoopSessionData * session = (CoopSessionData *)hCoopSession;
	if(session != NULL) SetEvent(session->m_eThisCancel);
}

VOID STDCALL SendToCoopSession(HCOOPSESSION hCoopSession, LPCVOID pData, UINT nSize)
{
	CoopSessionData * session = (CoopSessionData *)hCoopSession;
	ActivateCoop(&session->m_context, pData, nSize);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
China China
Begin coding from basic, since 1994. Interested in coding and database and website constructing.
My website: http://www.regexlab.com/ - Regular Expression Laboratory
The easiest regex engine: http://www.regexlab.com/deelx/

Comments and Discussions