Click here to Skip to main content
15,915,513 members
Home / Discussions / COM
   

COM

 
GeneralRe: Converting exe to com Pin
mpk197917-Oct-06 21:22
mpk197917-Oct-06 21:22 
GeneralRe: Converting exe to com Pin
Mike Dimmick18-Oct-06 0:42
Mike Dimmick18-Oct-06 0:42 
GeneralRe: Converting exe to com Pin
mpk197918-Oct-06 20:17
mpk197918-Oct-06 20:17 
GeneralRe: Converting exe to com Pin
Mike Dimmick18-Oct-06 1:02
Mike Dimmick18-Oct-06 1:02 
GeneralRe: Converting exe to com Pin
ashokvishnu18-Oct-06 18:06
ashokvishnu18-Oct-06 18:06 
GeneralRe: Converting exe to com Pin
mpk197922-Oct-06 18:55
mpk197922-Oct-06 18:55 
Questionaccess controls Pin
vtalau16-Oct-06 17:51
vtalau16-Oct-06 17:51 
QuestionHandling events from COM without TypeLIB in C#.NET Pin
Aleksey.Andreev16-Oct-06 4:52
Aleksey.Andreev16-Oct-06 4:52 
Hello, developers!

I have a problem, and i don't know how to solve it. Below i describe this situation:
I have a COM object(in C++) without TypeLIB and I there is a late bound client in C#.NET. During work
a COM object send different events to client. And i don't know, how i can recieve this events in C#. If a had a C++ client, i would write this code:


#include "stdafx.h"

#include <basetyps.h>

#include <unknwn.h>

#include <comdef.h>

#include <atlbase.h>

#include <atlcom.h>

#include <ocidl.h>
#include <iostream>
using namespace std;


_COM_SMARTPTR_TYPEDEF(IConnectionPointContainer, __uuidof(IConnectionPointContainer));

_COM_SMARTPTR_TYPEDEF(IConnectionPoint, __uuidof(IConnectionPoint));

_COM_SMARTPTR_TYPEDEF(IEnumConnectionPoints, __uuidof(IEnumConnectionPoints));


class CoFomatikEventSink : IDispatch

{
public:

enum DISPIDS
{
DISPID_OnStateChanged = 1,
DISPID_OnProgressChanged,
};

STDMETHODIMP_(ULONG) AddRef();

STDMETHODIMP_(ULONG) Release();

STDMETHODIMP QueryInterface(REFIID riid, void** ppv);

STDMETHODIMP GetTypeInfoCount(UINT *pctinfo);

STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);

STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);

STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);

CoFomatikEventSink() : cookie(0), refCount(1) {}
~CoFomatikEventSink() { Disconnect(); }

bool Connect(IUnknownPtr object);
bool Disconnect();

// Event Handlers
void OnStateChanged(IUnknownPtr state);
void OnProgressChanged(UINT value, const BSTR text);

private:
IConnectionPointPtr defaultConnectionPoint;
ULONG cookie;
ULONG refCount;
};

STDMETHODIMP_(ULONG) CoFomatikEventSink::AddRef(void)
{
return ++refCount;
}

STDMETHODIMP_(ULONG) CoFomatikEventSink::Release(void)
{
if(--refCount == 0)
{
delete this;
return 0;
}
return refCount;
}

STDMETHODIMP CoFomatikEventSink::QueryInterface(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
*ppv = (IUnknown*)this;
else
if(riid == IID_IDispatch)
*ppv = (IDispatch*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
((IUnknown*)(*ppv))->AddRef();
return S_OK;
}

STDMETHODIMP CoFomatikEventSink::GetTypeInfoCount(UINT *pctinfo)
{
*pctinfo = 0;
return S_OK;
}

STDMETHODIMP CoFomatikEventSink::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
*ppTInfo = NULL;
return E_NOTIMPL;
}

STDMETHODIMP CoFomatikEventSink::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
for(UINT i = 0; i < cNames; ++i)
{
if(_wcsicmp(rgszNames[i], OLESTR("OnStateChanged")) == 0)
{
rgDispId[i] = DISPID_OnStateChanged;
}
else
if(_wcsicmp(rgszNames[i], OLESTR("OnProgressChanged")) == 0)
{
rgDispId[i] = DISPID_OnProgressChanged;
}
else
return DISP_E_UNKNOWNNAME;
}
return S_OK;
}

STDMETHODIMP CoFomatikEventSink::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,

DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
switch(dispIdMember)
{
case DISPID_OnStateChanged:
{
if(pDispParams -> cArgs == 1)
{
OnStateChanged(pDispParams->rgvarg[0].punkVal);
return S_OK;
}
}
break;

case DISPID_OnProgressChanged:
{
if(pDispParams -> cArgs == 2)
{
OnProgressChanged(pDispParams->rgvarg[0].uintVal,pDispParams->rgvarg[1].bstrVal);
return S_OK;
}
}
break;
}
return DISP_E_UNKNOWNINTERFACE;
}

bool CoFomatikEventSink::Connect(IUnknownPtr object)
{
IConnectionPointContainerPtr connectionPointContainer = object;
IEnumConnectionPointsPtr enumConnectionPoints;
ULONG numConnectionPoints = 0;
connectionPointContainer->EnumConnectionPoints(&enumConnectionPoints);
if(enumConnectionPoints->Next(1, &defaultConnectionPoint, &numConnectionPoints ) == S_OK)
{
if(defaultConnectionPoint->Advise(this, &cookie) == S_OK)
{
cout<<cookie<<endl;
return="" true;
="" }
="" defaultconnectionpoint="NULL;
" cookie="0;"
="" false;
}

bool="" cofomatikeventsink::disconnect()
{
="" if(cookie)
="" {
="" defaultconnectionpoint-="">Unadvise(cookie);
defaultConnectionPoint = NULL;
cookie = 0;
}
return true;
}

void CoFomatikEventSink::OnStateChanged(IUnknownPtr state)
{
/* OnStateChanged handler HERE */
}

void CoFomatikEventSink::OnProgressChanged(UINT value, const BSTR text)
{
/* OnProgressChanged handler HERE */
}

// ===========================================================================
int main(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
{
IUnknownPtr fomatik;
fomatik.CreateInstance("MyCOM.Application");
CoFomatikEventSink* ev = new CoFomatikEventSink();
ev->Connect(fomatik);
ev->Release();
}
::CoUninitialize();
}

But i don't know how i can implement IDispatch in C#? Yes, i know, that RCW and CCW do all work to marshal parameters and call functions, and implement IUnknown and IDispatch, but in my application it is neccesary to implement IDispatch myself, because i don't have a TypeLIB for COM object and that's way i can't cache DispIDs from another source.. I have been trying to write this event handler already 3 days, but unsuccessful!!!!
Plese, if anybody know how to solve this problem, help!

Big, hearty thanks in advance!!!!
GeneralRe: Handling events from COM without TypeLIB in C#.NET Pin
prasad_som16-Oct-06 21:23
prasad_som16-Oct-06 21:23 
QuestionVC++ app crashes unusally when COM functions called Pin
dharani16-Oct-06 3:16
dharani16-Oct-06 3:16 
QuestionRe: VC++ app crashes unusally when COM functions called Pin
prasad_som16-Oct-06 4:48
prasad_som16-Oct-06 4:48 
AnswerRe: VC++ app crashes unusally when COM functions called Pin
Hamid_RT18-Oct-06 19:27
Hamid_RT18-Oct-06 19:27 
QuestionCOM+ Pin
AnhTin13-Oct-06 23:15
AnhTin13-Oct-06 23:15 
AnswerRe: COM+ Pin
Steve S16-Oct-06 21:35
Steve S16-Oct-06 21:35 
QuestionMFC ActiveX Control Errors Pin
Stober13-Oct-06 9:48
Stober13-Oct-06 9:48 
AnswerRe: MFC ActiveX Control Errors Pin
Stober13-Oct-06 11:51
Stober13-Oct-06 11:51 
QuestionNeed Directshow Sample Pin
Manjunath S13-Oct-06 8:24
Manjunath S13-Oct-06 8:24 
QuestionHow to load XSD files using VC++ Pin
vgkotha12-Oct-06 20:18
vgkotha12-Oct-06 20:18 
AnswerRe: How to load XSD files using VC++ Pin
Hamid_RT18-Oct-06 19:33
Hamid_RT18-Oct-06 19:33 
QuestionMaking connection points work with MFC Pin
garyflet12-Oct-06 7:51
garyflet12-Oct-06 7:51 
QuestionPass BSTR with Connection point. Pin
HakunaMatada12-Oct-06 3:13
HakunaMatada12-Oct-06 3:13 
AnswerRe: Pass BSTR with Connection point. Pin
Jonathan [Darka]12-Oct-06 4:15
professionalJonathan [Darka]12-Oct-06 4:15 
GeneralRe: Pass BSTR with Connection point. Pin
Steve S12-Oct-06 6:42
Steve S12-Oct-06 6:42 
GeneralRe: Pass BSTR with Connection point. Pin
Jonathan [Darka]12-Oct-06 10:24
professionalJonathan [Darka]12-Oct-06 10:24 
GeneralRe: Pass BSTR with Connection point. Pin
Steve S12-Oct-06 21:36
Steve S12-Oct-06 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.