473,788 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing none static member function as callback parameter to manag

Hi,

I have my main application class(unmanaged ) that has a none static member
function that I need to pass as a delegate to managed C# method.

In one of the methods of this class(unmamange d), I am calling a managed C#
method(I use gcnew to instantiate the managed class). One of the parameters
of this C# method is a delegate. I need to pass the none static member
function as a delegate(functi on pointer) as a parameter in the managed C#
function call.

e.g.

//Managed C#
namespace MySpace
{
public delegate int MyDelegate();

MyDelegate rDelegate;

public class Managed
{
void ManagedFunction (MyDelegate del)
{
//do stuff
}

}
}
//Unmanaged C++ written in VC++ 6.0

//.h
class Unmanaged
{
public:
Unmanaged();
~Unmanaged();

void UnmanagedFunc1( );
void UnmanagedFunc2( );

}

*.cpp
using namespace MySpace;

void Unmanaged::Unma nagedFunc1()
{
MySpace::Manage d ^man = gcnew MySpace::Manage d;

//I need to pass UnmanagedFunc2( ) here as a delegate/callback as a
//parameter to ManagedFunction .
man->ManagedFunctio n(??)

}

void Unmanaged::Unma nagedFunc2()
{

}

Will appreciate all the help I can get on this.

Also what if I need to pass a string also as a parameter to
ManagedFunction (). In unmanaged it is a char* or a CString. How can I do the
conversion to pass it to managed.

Thanks for the help.

Aug 17 '06 #1
12 11369
Haxan wrote:
Hi,

I have my main application class(unmanaged ) that has a none static member
function that I need to pass as a delegate to managed C# method.
After some discussion of this topic (see http://tinyurl.com/h2kry), I
came up with two very simple examples demonstrating managed->unmanged
and unmanaged->managed event handling:

http://tweakbits.com/ManagedToUnmanagedCallback.cpp
http://tweakbits.com/UnmanagedToManagedCallback.cpp

What you're asking for is handling managed events from unmanaged code,
so my ManagedToUnmana gedCallback.cpp example should give you an idea.

On the other hand, I assume your code is written in VC++ 2005, compiled
in mixed-mode. It looks like your unmanaged class is written in VC6,
which requires yet another level of interaction. In other words, the
Thunk class (see my example) will call a VC6 DLL inside.

In addition to my example, Microsoft has an official guide about this
subject too:
http://msdn2.microsoft.com/en-us/library/367eeye0.aspx

Finally, you have to take care of your argument marshaling, such as
converting String^ to const char* / const wchar_t*. You can use
StringToHGlobal Ansi/StringToHGlobal Uni to do that, as shown here:
http://tinyurl.com/kmzmh (see Jochen's StringConvA / StringConvW classes).

Tom
Aug 18 '06 #2
Tamas Demjen wrote:
which requires yet another level of interaction.
* indirection, I meant.
Aug 18 '06 #3
Thanks very Tamas. This looks like what I was looking for. Appreciate the
tips. I will work on it.

"Tamas Demjen" wrote:
Tamas Demjen wrote:
which requires yet another level of interaction.
* indirection, I meant.
Aug 18 '06 #4
Im still having problems with passing char * parameter in my unmanaged code.
This is how I have defined

//Managed C#
namespace MySpace
{
public delegate int MyDelegate(stri ng strMan);

MyDelegate rDelegate;

public class Managed
{
void ManagedFunction (MyDelegate del)
{
//do stuff
}

}
}
//Unmanaged C++ written in VC++ 6.0

//.h
class Unmanaged
{
public:
Unmanaged();
~Unmanaged();

void UnmanagedFunc1( );
static void UnmanagedFunc2( gcroot<System:: String^pUnm);

}

*.cpp
using namespace MySpace;

void Unmanaged::Unma nagedFunc1()
{
MySpace::Manage d ^man = gcnew MySpace::Manage d;

//I need to pass UnmanagedFunc2( ) here as a delegate/callback as a
//parameter to ManagedFunction .
man->ManagedFunctio n(??)

}

void Unmanaged::Unma nagedFunc2(gcro ot<System::Stri ng^pUnm)
{
}

I get ane error C3352 ..The specified function does not match the delegate
type void(System::St ring ^)

Will appreciate if you can provide me some suggestion on how to fix this.

Thanks


"Tamas Demjen" wrote:
Tamas Demjen wrote:
which requires yet another level of interaction.
* indirection, I meant.
Aug 18 '06 #5
Please note that my unmanaged class is a dll. It seems like we cannot pass
Managed types/objects as parameters in dlls.

"Tamas Demjen" wrote:
Tamas Demjen wrote:
which requires yet another level of interaction.
* indirection, I meant.
Aug 18 '06 #6
Haxan wrote:
Please note that my unmanaged class is a dll. It seems like we cannot pass
Managed types/objects as parameters in dlls.
You don't have to. Your managed code can call your unmanaged DLL.

Tom
Aug 18 '06 #7
Haxan wrote:
*.cpp
using namespace MySpace;

void Unmanaged::Unma nagedFunc1()
{
MySpace::Manage d ^man = gcnew MySpace::Manage d;

//I need to pass UnmanagedFunc2( ) here as a delegate/callback as a
//parameter to ManagedFunction .
man->ManagedFunctio n(??)

}
The event must be handled by a managed class, which can forward the call
to a native class. I call this trampoline class Thunk. That's where the
translation from String^ to char* takes place:

ref class Thunk
// managed event handler that forwards to unmanaged call
{
public:
void CallbackForward er(String^ s)
{
UnmanagedFunc1( StringConvA(s). c_str());
}
};

where StringConvA is (untested):

struct StringConvA
{
char *szAnsi;
StringConvA(Str ing^ s)
:
szAnsi(static_c ast<char*>(Syst em::Runtime::In teropServices:: Marshal::String ToHGlobalAnsi(s ).ToPointer()))
{}
~StringConvA()
{
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szAnsi));
}
char* c_str() const
{
return szAnsi;
}

};

Tom
Aug 18 '06 #8
Actually the unmanaged class is part of an old dll that was written using
VC++ 6.0. Since we later on started using managed code, the changes have to
be part of the dll.

"Tamas Demjen" wrote:
Haxan wrote:
Please note that my unmanaged class is a dll. It seems like we cannot pass
Managed types/objects as parameters in dlls.

You don't have to. Your managed code can call your unmanaged DLL.

Tom
Aug 18 '06 #9
Haxan wrote:
Actually the unmanaged class is part of an old dll that was written using
VC++ 6.0. Since we later on started using managed code, the changes have to
be part of the dll.
You have to write a mixed-mode DLL, a translation layer between the C#
and the VC6 code. That mixed-mode DLL has to export native functions
that your VC6 code can call. In turn, it will internally set up the
managed event forwarder and calls the managed library function. This
intermediate DLL is necessary, because I don't think you can compile
C++/CLI code from VC6.

Tom
Aug 18 '06 #10

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

Similar topics

7
2497
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
4
2383
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public: void start(); void run();
6
8830
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
0
3291
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer = 0ms.....60ms. I'm already timing the callbacks using the Win32API QueryPerformanceCounter/Frequency. The code I have is actually performing the callback and it looks like it is close to 1ms but I now need to get my object passed to this static...
3
3067
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
4
5903
by: Jimmy | last post by:
I need to use Asynchronous Socket functions in a server application and am learning from sources such as the MSDN2 (http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx). What I observed is that all callback handlers in examples are static functions. I did try non-static callback handlers; they certainly works, and they usually make my code simpler. For example, a typical tutorial will start an asynchronous connection with:
6
7684
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
2
4130
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are typedef'd to take void* parameters, through which the DLL's function passes its void* parameter to the callback function. This allows me to use class instances as callback handlers, as in the example below. //Executable and DLL know this:
8
1972
by: Neviton | last post by:
Is it possible ? There is any work around? Thanx
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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...
0
6750
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
5398
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...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.