Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
AnswerRe: Focusing problem Pin
sam#16-Oct-06 21:54
sam#16-Oct-06 21:54 
AnswerRe: Focusing problem Pin
quiteSmart16-Oct-06 22:49
quiteSmart16-Oct-06 22:49 
Questionwhy is thread not working with graphic object?? [modified] Pin
samtam16-Oct-06 20:30
samtam16-Oct-06 20:30 
AnswerRe: why is thread not working with graphic object?? Pin
Guffa16-Oct-06 20:41
Guffa16-Oct-06 20:41 
GeneralRe: why is thread not working with graphic object?? Pin
samtam16-Oct-06 21:08
samtam16-Oct-06 21:08 
AnswerRe: why is thread not working with graphic object?? Pin
Guffa17-Oct-06 21:51
Guffa17-Oct-06 21:51 
AnswerRe: why is thread not working with graphic object?? Pin
sam#16-Oct-06 21:43
sam#16-Oct-06 21:43 
QuestionHandling events from COM object without TypeLIB in C#.NET Pin
Aleksey.Andreev16-Oct-06 20:06
Aleksey.Andreev16-Oct-06 20:06 
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

#include

#include

#include

#include
#include
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;=""
}
return="" 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!!!!
QuestionSample of Compress Folder Pin
dehghan198416-Oct-06 19:08
dehghan198416-Oct-06 19:08 
Questionstandone app without installer Pin
bryce16-Oct-06 19:05
bryce16-Oct-06 19:05 
AnswerRe: standone app without installer Pin
Expert Coming16-Oct-06 20:06
Expert Coming16-Oct-06 20:06 
Questionusing Help.ShowHelp(this,"path","keyword") Pin
sikandarhayat16-Oct-06 18:31
sikandarhayat16-Oct-06 18:31 
Questiongetting list of all databases Pin
saqib8216-Oct-06 18:29
saqib8216-Oct-06 18:29 
AnswerRe: getting list of all databases Pin
sikandarhayat16-Oct-06 18:34
sikandarhayat16-Oct-06 18:34 
GeneralRe: getting list of all databases Pin
saqib8216-Oct-06 18:42
saqib8216-Oct-06 18:42 
GeneralRe: getting list of all databases Pin
sikandarhayat16-Oct-06 18:57
sikandarhayat16-Oct-06 18:57 
QuestionHow to get a color change of an image! Pin
Mirunab16-Oct-06 18:21
Mirunab16-Oct-06 18:21 
AnswerRe: How to get a color change of an image! Pin
Guffa16-Oct-06 18:58
Guffa16-Oct-06 18:58 
GeneralRe: How to get a color change of an image! Pin
Mirunab16-Oct-06 19:18
Mirunab16-Oct-06 19:18 
QuestionSum and delete columns in a dataset datatable Pin
minnie mouse16-Oct-06 15:37
minnie mouse16-Oct-06 15:37 
AnswerRe: Sum and delete columns in a dataset datatable Pin
sikandarhayat16-Oct-06 18:37
sikandarhayat16-Oct-06 18:37 
QuestionPicture Box Image (GET) Pin
Expert Coming16-Oct-06 15:30
Expert Coming16-Oct-06 15:30 
QuestionStart menu search in Vista Pin
prismejon16-Oct-06 14:54
prismejon16-Oct-06 14:54 
QuestionIs there anything to convert my .net 2.0 type files to .net 1.1? Pin
Anthony Mushrow16-Oct-06 14:24
professionalAnthony Mushrow16-Oct-06 14:24 
AnswerRe: Is there anything to convert my .net 2.0 type files to .net 1.1? Pin
bearfx17-Oct-06 4:37
bearfx17-Oct-06 4:37 

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.