473,414 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to sink an event from C++.NET in C#?

Could anybody help me?
How to sink an event from C++.NET in C#? For example, for
following CSource, how to sink EventTest in C#?

#pragma once
#include <windows.h>
using namespace System;

namespace CSource
{
[event_source(managed)]
public __gc class CSourceClass
{
// TODO: Add your methods for this class here.
public:
CSourceClass(void){}
~CSourceClass(void){}

void FireEvent()
{
__raise EventTest();
}

__event void EventTest();
};
}

Thank you very much,

Ydejun
Jul 19 '05 #1
1 3535

"Dejun Yang" <yd****@yahoo.com> wrote in message
news:06****************************@phx.gbl...
Could anybody help me?
How to sink an event from C++.NET in C#? For example, for
following CSource, how to sink EventTest in C#?
Since your code is a managed class (__gc) you do not need to use
[event_source(managed)]. This attribute is *not* a .NET attribute, it is a
compile time attribute only recognized by the C++ compiler. [event_source()]
is useful for COM events and for native events (ie neither .NET nor COM).

Instead, you simply have to define an event in the class, as you have done.
I have added some other comments:
#pragma once
this is in a header file, yes?
#include <windows.h>
does the code really use Win32? you might have Win32 code, but if you don't
your code will compile quicker without this line

you have included a line like the following, somewhere in your project?

#using <mscorlib.dll>
using namespace System;

namespace CSource
{
public __gc class CSourceClass
{
public:
CSourceClass(void){}
~CSourceClass(void){}
Don't give a managed class a destructor unless you really do need it. The
reason is that a destructor will make objects live longer than they need to,
and hence it will use up resources. Since your code has an empty destructor,
you should remove it entirely.
void FireEvent()
{
__raise EventTest();
}

__event void EventTest();
};
}


There is a problem with this approach, however. The problem is that
declaring the event like this means that the delegate is also declared
within the class and the compiler generates a name for it. In this case the
delegate is called

CSource.CSourceClass.__Delegate_EventTest

This is not easy to remember! It would be far better to declare the delegate
outside of the class. Here is the cleaned up version:

namespace CSource
{

public __delegate void MyEvent();
public __gc class CSourceClass
{
public:
__event MyEvent EventTest;
CSourceClass(void){}
void FireEvent()
{
__raise EventTest();
}
};

}

using this from C# is simple:

CSourceClass obj = new CSourceClass();
// assume EventHandler is a member method
MyEvent ev = new MyEvent(EventHandler);
obj.EventTest += ev;
obj.FireEvent();

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Notlwonk | last post by:
Not sure where to post this Q I am trying to write a Managed Exchange Server Event Sink (as a class library) in C# I have been following this tutorial step-by-step ...
0
by: Notlwonk | last post by:
I have written an event sink in C# that hooks into our Exchange Server 2000 store. The sink fires and the various LOG files are created, but for some reason I cannot seem to read-in the e-mail...
1
by: Bob Whiton | last post by:
I have an unmanaged class which has a member variable: gcroot<DataTable*> myDataTable; I would like to sink the RowDeleting event in my UNmanaged class. However, I can't declare an event...
0
by: techie | last post by:
I have created an event sink in my ATL COM project. The event sink receives events from a C# component. There is no problem with receving events but when my COM object is released I get an access...
1
by: Joe | last post by:
Hi All, I wrote a sink interface for a non-visual mail COM object to get hold of the objects events. A few of the events fired by the COM object takes a pointer to a boolean as argument for the...
7
by: Don | last post by:
Getting errors after following the MSDN article on using VB.NET (and VS2005) for "Implementing a Managed OnSave Event Sink" for Exchange Server 2007. Not sure, but part of the problem may be that...
9
by: Eric Kaplan | last post by:
I have a function that will download XML from internet and load XML data into database. The function will take 5 - 20 minutes to finish. I heard I should use event sink (event listener) when...
1
by: mooni | last post by:
Hi all, I am trying to create a class that will act as event sink to a COM object. Just to tell you the COM object is SoftUSBEndpoint which is part of Device Simulation Framework. Anyways its...
5
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
I have an event sink which does some work to the software we use, according to the data contained within an email. This is all working hunky dory. What i want to know is whether it is possible to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.