How to create events in c# using COM interop

M

Mayur

This is my COM wrapper written in c#.

using System.Runtime.InteropServices;
namespace SAFE_NAMESPACE_NAME
{
#region Events raised by your COM class
public delegate void Event1Handler(int i, int j);
[Guid("C37B4CE2-E6F0-404b-836A-722064BADBF1"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch) ]
public interface EVENTS_NAME
{
// TODO: Insert event handlers for events your class raises here.
[DispId(1)] void Event1(int i, int j);
}
#endregion
#region Interface published by your COM class
[Guid("117E0536-B80A-45b5-92BA-81B7AF5BAE5E"),
InterfaceType(ComInterfaceType.InterfaceIsDual) ]
public interface INTERFACE_NAME
{
// TODO: Insert functions your class publishes here.
[DispId(1)] int Function1(string s1, string s2);
}
#endregion
[Guid("251A0F17-2118-4884-BC55-4CEF4EA8A009"),
ProgId("[SAFE_NAMESPACE_NAME].[CLASS_NAME]"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(EVENTS_NAME)) ]
public class CLASS_NAME : INTERFACE_NAME
{
//
// TODO: Insert events raised by your class here.
//

public event Event1Handler Event;
public CLASS_NAME() : base()
{}
// TODO: Implement the methods of your class interface here.
public int Function1(string s1, string s2)
{
return 0;
}
}
}

c++ client
I am able to call the method exposed through interface.
#import "SAFE_NAMESPACE_NAME.tlb"
using namespace SAFE_NAMESPACE_NAME;
INTERFACE_NAME *t_my_interface;

CoInitialize(NULL);
SAFE_NAMESPACE_NAME::INTERFACE_NAMEPtr
q(__uuidof(SAFE_NAMESPACE_NAME::EVENTS_NAME));
t_my_interface = q;
t_my_interface->Function1("Hi","Hello");

But how to add Event handler in unmanaged c++ for the above Interface.
Waiting for the raply.
 
W

Willy Denoyette [MVP]

Mayur said:
This is my COM wrapper written in c#.

using System.Runtime.InteropServices;
namespace SAFE_NAMESPACE_NAME
{
#region Events raised by your COM class
public delegate void Event1Handler(int i, int j);
[Guid("C37B4CE2-E6F0-404b-836A-722064BADBF1"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch) ]
public interface EVENTS_NAME
{
// TODO: Insert event handlers for events your class raises here.
[DispId(1)] void Event1(int i, int j);
}
#endregion
#region Interface published by your COM class
[Guid("117E0536-B80A-45b5-92BA-81B7AF5BAE5E"),
InterfaceType(ComInterfaceType.InterfaceIsDual) ]
public interface INTERFACE_NAME
{
// TODO: Insert functions your class publishes here.
[DispId(1)] int Function1(string s1, string s2);
}
#endregion
[Guid("251A0F17-2118-4884-BC55-4CEF4EA8A009"),
ProgId("[SAFE_NAMESPACE_NAME].[CLASS_NAME]"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(EVENTS_NAME)) ]
public class CLASS_NAME : INTERFACE_NAME
{
//
// TODO: Insert events raised by your class here.
//

public event Event1Handler Event;
public CLASS_NAME() : base()
{}
// TODO: Implement the methods of your class interface here.
public int Function1(string s1, string s2)
{
return 0;
}
}
}

c++ client
I am able to call the method exposed through interface.
#import "SAFE_NAMESPACE_NAME.tlb"
using namespace SAFE_NAMESPACE_NAME;
INTERFACE_NAME *t_my_interface;

CoInitialize(NULL);
SAFE_NAMESPACE_NAME::INTERFACE_NAMEPtr
q(__uuidof(SAFE_NAMESPACE_NAME::EVENTS_NAME));
t_my_interface = q;
t_my_interface->Function1("Hi","Hello");

But how to add Event handler in unmanaged c++ for the above Interface.
Waiting for the raply.

You have to implement an Event sink interface in C++, this question is largely OT for this
NG, google around for "implementing event sink" or "COM Connection points", and/or get a
good book on COM. Anyway without any support from template libraries like ATL and WFC,
you'll have a hard time to get this done in pure C++.

Willy.
 

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