473,396 Members | 1,693 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,396 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 3533

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.