473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ma naged)]
public __gc class CSourceClass
{
// TODO: Add your methods for this class here.
public:
CSourceClass(vo id){}
~CSourceClass(v oid){}

void FireEvent()
{
__raise EventTest();
}

__event void EventTest();
};
}

Thank you very much,

Ydejun
Jul 19 '05 #1
1 3552

"Dejun Yang" <yd****@yahoo.c om> 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(ma naged)]. 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(vo id){}
~CSourceClass(v oid){}
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.CSource Class.__Delegat e_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(vo id){}
void FireEvent()
{
__raise EventTest();
}
};

}

using this from C# is simple:

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

Richard
--
my email ev******@zicf.b et 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
2861
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 http://www.codeproject.com/csharp/CsManagedEventSinksHooks.as So far so good....except when it comes down to binding the component to the folder on the Exchange Store. When I do...I get the following error
0
3624
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 Subject or Message Body for the e-mail that triggers the Event Sink to run. What am I doing wrong? Thank you,
1
1939
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 handler in the unmanaged class because the types are managed (compiler error C3265). For example, I can't declare the following sink: static void Row_Deleting(Object* sender, System::Data::DataRowChangeEventArgs* e)
0
1391
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 violation - (MSCORWKS.DLL): 0xC0000005: Access Violation. Here's my event sink class: namespace { static const int EVENT_ID = 111;//any arbitary value
1
1561
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 event. Example: HRESULT CEventSink::OnSendStart(bool* bProceed) This event for instance should cancel the send process if bProceed = false and continue if bProceed = true
7
5131
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 it's the 64-bit version of Exchange 2007 installed on Windows Server 2003 Enterprise x64, while the Exchange 2007 SDK samples seem mostly geared toward 32-bit. Even at that, I was able to build the TLB and install it into COM+ Component Services,...
9
3610
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 function is finished the task, then it will notify the caller. So I am planning to create a seperate thread to do the long XML loading function by using - _beginthreadex()
1
3220
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 outbound interface is ISoftUSBEndpointEevents which implements IUnknown interface. It is shown below ISoftUSBEndpointEvents : public IUnknown { ........... .........
5
1284
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 send an email from inside of an event sink. So for example an email comes into the event sink, and then the event sink generates a new email which it then sends. The reason I ask this is because the event sink code sits on the exchange...
0
8016
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8440
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8431
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6773
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3937
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.