COM events in CSharp

Y

Yoavo

Hi,
I have an application that was written as ATL project which uses as a server
application and has a COM class which implements IConnectionPoints interface
(to fire events).
I have clients applications which uses the server application (the clients
are written in several languages).
In the VB client I use "WithEvents" option to recieve the events.
In the VC client I create a class derived from IDispEventImpl to recieve the
events.

What do I do in CSharp client ???

Yoav.
 
L

Laura T.

Add reference to your ATL object.
Add code like MyAtlObject.<yourATLObjectsEventName>+=<event handler>.
Should do.
 
G

Guest

I have similar problem - trying to handle some key press events in a
DataGridView, and I added code like this:
this.dataGridView.KeyPress += new
System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView_KeyPress);
to my frmMain.Designer.cs,
and the corresponding dataGridView_KeyPress fnc to my frmMain.cs.
But the compiler (VS .NET 2005) complains:
Error: Cannot implicitly convert type
'System.Windows.Forms.DataGridViewCellEventHandler' to
'System.Windows.Forms.KeyPressEventHandler'

I am used to adding event handlers by double clicking the target element in
my form's design view, but that doesn't work when dealing with handling
keystrokes or cell selection changes and such. Is there a semi-automated way
in the IDE to create event handlers other than that?
Thank you,
Dave
 
Y

Yoavo

In order to make things more clear, here is my code:
In the server, this is the definitions of the COM interface and its events
handler:
interface IDog : IDispatch

{

[id(1), helpstring("method Bark")] HRESULT Bark();

};



[

uuid(17CEB032-D336-4F3C-85E3-90D879D38F1F),

version(1.0),

helpstring("DogTest 1.0 Type Library")

]

library DOGTESTLib

{

importlib("stdole32.tlb");

importlib("stdole2.tlb");



[

uuid(22807A6E-50E6-49F7-904C-746A27110243),

helpstring("_IDogEvents Interface")

]

dispinterface _IDogEvents

{

properties:

methods:

[id(1), helpstring("method OnBark")] HRESULT
OnBark();

};

-----------------------------

This is the implementation (in the server) of IDog::Bark:

STDMETHODIMP XDog::Bark()

{

Fire_OnBark();



return S_OK;

}

-----------------------------

This is the implementation (in the server)of the IConnectionPoints
interface:

template <class T>

class CProxy_IDogEvents : public IConnectionPointImpl<T, &DIID__IDogEvents,
CComDynamicUnkArray>

{

Public:

HRESULT Fire_OnBark()

{

...



}

};

-----------------------------

And this is my c# client implementation:

namespace DogCsClient

{

public partial class Form1 : Form

{

class MyDogEvents : DOGTESTLib._IDogEvents

{



#region _IDogEvents Members



public void OnBark()

{

MessageBox.Show("OnBark event !!!");

}



#endregion

};



DOGTESTLib.Dog mDog;



public Form1()

{

InitializeComponent();



mDog = new DOGTESTLib.Dog();

}



private void button1_Click(object sender, EventArgs e)

{

mDog.Bark();

}

}

}



What should I do inorder to recieve OnBark() event in my c# client ???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top