interop >> How to advise in C++ on an event in c# ?

by YnVyZ2VyMTAw » Sat, 03 Jun 2006 02:05:01 GMT

I created an interface in c# and I tryied to import the tlb in c++ and to
advise me on the event in the interface.

I use the following sample but it's base on a VB client.

http://msdn2.microsoft.com/en-us/library/dd8bf0x3.aspx

Here is the code c# of the interface
using System;
using System.Runtime.InteropServices;
namespace EventSource
{
public delegate void ClickDelegate(int x, int y);
public delegate void ResizeDelegate();
public delegate void PulseDelegate();

// Step 1: Defines an event sink interface (ButtonEvents) to be
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ButtonEvents
{
void Click(int x, int y);
void Resize();
void Pulse();
}
// Step 2: Connects the event sink interface to a class
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces(GetType(ButtonEvents))]
public class Button
{
public event ClickDelegate Click;
public event ResizeDelegate Resize;
public event PulseDelegate Pulse;

public Button()
{
}
public void CauseClickEvent(int x, int y)
{
Click(x, y);
}
public void CauseResizeEvent()
{
Resize();
}
public void CausePulse()
{
Pulse();
}
}
}


I need to follow the same step as the VB code bellow but in C++.

' COM client (event sink)
' This Visual Basic 6.0 client creates an instance of the Button class and
' implements the event sink interface. The WithEvents directive
' registers the sink interface pointer with the source.
Public WithEvents myButton As Button

Private Sub Class_Initialize()
Dim o As Object
Set o = New Button
Set myButton = o
End Sub
' Events and methods are matched by name and signature.
Private Sub myButton_Click(ByVal x As Long, ByVal y As Long)
MsgBox "Click event"
End Sub

Private Sub myButton_Resize()
MsgBox "Resize event"
End Sub

Private Sub myButton_Pulse()
End Sub


In the interface ButtonEvents, i try to use event declaration instead of
void method declaration and the generated file created by the tlb include
methos wrapper like Click_add and Click_remove but i don't know how to use it.

Thanks you



Similar Threads

1. How to advise in C++ on an event in c# ?

2. Advising to submit form event.

3. Can't advise office custom button's Click event

I wrote the follow class to be used as a parameter of Advise:

class ATL_NO_VTABLE ToolbarButtonClickHandler: public CComObjectRoot,
			public Office::_CommandBarButtonEvents {
private:
	ToolbarClickEventDispatchBase* m_disp;
public:
	DECLARE_NOT_AGGREGATABLE(ToolbarButtonClickHandler)

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	BEGIN_COM_MAP(ToolbarButtonClickHandler)
	    COM_INTERFACE_ENTRY(Office::_CommandBarButtonEvents)
	    COM_INTERFACE_ENTRY(IDispatch)
	END_COM_MAP()

	ToolbarButtonClickHandler(void);
	virtual ~ToolbarButtonClickHandler(void);

	void Init(ToolbarClickEventDispatchBase* disp);

	//IDispatch
	STDMETHOD(GetTypeInfoCount)(UINT *pctinfo);
	STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, 
LCID lcid, DISPID *rgDispId);
	STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD 
wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, 
UINT *puArgErr);

	//_CommandBarButtonEvents
	STDMETHOD(OnClick)(LPDISPATCH Ctrl, VARIANT_BOOL * CancelDefault);
};


Then in _IDTExtensibility2::OnConnection, the connecting code is:

for(int i = 1; i <= count; i++){
	CComPtr<Office::CommandBarControl> item;
	pControls->get_Item(CComVariant(i), &item);
	CComQIPtr<IConnectionPointContainer> pConnContainer(item);
	CComQIPtr<IConnectionPoint> pConn;
	pConnContainer->FindConnectionPoint(__uuidof(Office::_CommandBarButtonEvents), 
&pConn);
	hr = pConn->Advise(m_clickHandler, &m_cookies[i - 1]); //m_clickHandler is 
an instance of CComObject<ToolbarButtonClickHandler>
	ATLASSERT(SUCCEEDED(hr));
}

According to the hr, which is returned S_OK, it seems that the connection is 
made successfully. But When I click the button, nothing happens. Can anyone 
tell me what the problem is?
-- 
Thanks,
Philip 

4. COM C++ Server Advise does not get entered by C# Client

5. Advises on C++ technology or template to choose

Hi,

In VS 2005, when creating my new project I was wondering which C++
technology (template) will perfectly fits to my needs.

I'm doing a small Client-Server GUI testing automation application.
Like WinRunner,SilkTest, Rational Robot, automate or macro
scheduler....

The Server  : Will be a kind of notepad (ultraedit) used to develop
and run GUI test scenarios script.  The server will send the actions
to execute to a remote client.

The Client : This application will have no GUI and will receive the
test scenario (list of actions to execute).  The client will
"translate" each action to execute to Win32 API call/msg (like
sendmessage, enumWindow, clicking button, entering text into a
textbox, getting infos from the gui, ...) and will return results to
the server.

My question is more for the client part,  95% of the code will be
Win32 native API calls and 5% will be for networking stuff to
communicate with the server.  There is no GUI interface.  The process
must be executed as fast as possible.

What is the best technology to develop this part?  Win32 application?
Invisible form application? Invisible console application?  Windows
services?.    Considering the choosen technology must be as convenient
as possible to Win32 native API calls, which one is the best for the
cleanest/fastest solution?  I'm switching from one to another without
being sure i'm using the best solution...It's now time to ask to more
experienced people.

Thanks for your help.

6. MFC C++ programming under VS 2005 and book advise

7. C++ Advise

Hello!

I new to this forum and in need of some advice.

I have learned the basics of java in the school as I studying
computer science. I would rather learn C++ though.
You can create windows applications, games, Linux applications, its
faster, more power etc.
And you get a nice .exe file when you compile in windows. (Which java
lacks)

I have already started to read an C++ book and I half way through,
but when it comes to GUI applications I have some
questions I would like to clear out.

As I understand it C++ doesn provide a GUI library. But then we have
Windows API and Visualc++. So my questions are.


Windows API and VisualC++ they don't seem to be using the same
library? Windows API is it too old to be using today?
I know that in VisualC++ you can just copy and paste in the design
mode to get a button but in WinAPI its lines of code.

And another problem is that many C++ books don't even mention the GUI.
Don't even know if there are books about it.
Are there more libraries that one should be aware of?


In other hand C++ is an "old" language compared to java, C#, VB but
it still is the most powerful one (my opinion).


Thanks in advance



8. Good way to use native C++ callback as managed C++ delegates / events