By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

ManagedC++, Garabage collection and function pointer

P: n/a
I have some managed C++ interacting with native DLL,
for good integration I'm setting up some function pointer in the native DLL,
passing some function pointer from the managed world.
=================
typedef void __cdecl NSLogHandler(Oid exception);
static void __cdecl ObjectiveNSLogHandler(Oid ns_str)
{
NSString^ str = dynamic_cast<NSString^>( ObjcRuntime::GetId(ns_str) );
Console::WriteLine("GNUstep.NET: "+str);
}

void ObjcRuntime::SetupNSLogHandler()
{
NSLogHandler** p_nslog_h = (NSLogHandler**) GetProcAddress(_hLibGSBase,
"_NSLog_printf_handler");
if( !p_nslog_h )
throw gcnew ObjectiveCException("Cannot set NSLog Handler");
*p_nslog_h = &ObjectiveNSLogHandler;
}
=================

I wonder if it's allright or if the code (hence its address) could be moved
too?
what could I do in this case?

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
Mar 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have some managed C++ interacting with native DLL,
for good integration I'm setting up some function pointer in the native
DLL, passing some function pointer from the managed world.
=================
typedef void __cdecl NSLogHandler(Oid exception);
static void __cdecl ObjectiveNSLogHandler(Oid ns_str)
{
NSString^ str = dynamic_cast<NSString^>( ObjcRuntime::GetId(ns_str) );
Console::WriteLine("GNUstep.NET: "+str);
}

void ObjcRuntime::SetupNSLogHandler()
{
NSLogHandler** p_nslog_h = (NSLogHandler**) GetProcAddress(_hLibGSBase,
"_NSLog_printf_handler");
if( !p_nslog_h )
throw gcnew ObjectiveCException("Cannot set NSLog Handler");
*p_nslog_h = &ObjectiveNSLogHandler;
}
=================

I wonder if it's allright or if the code (hence its address) could be
moved too?
what could I do in this case?


I am not sure I understood your question totally, but my gut feeling is,
your approach will not be successful. GetProcAddress gives you a function
pointer. You cast a function pointer into a function pointer pointer.

From your explanations, I assume you
a) have a managed DLL that calls exported methods of a native DLL.
b) want to call one of the exported functions of the DLL which expects a
native function pointer as an argument
c) want to pass a pointer to managed function with this method call

If my assumption is right, then you should know that managed functions can
have native calling conventions and that you can get the address of a
managed function with a native calling convention. This address is a native
function pointer that you use like any other native function pointer. So you
can pass it to your exported function of the native DLL. Under the hood,
C++/CLI creates so called vtfixups in your assembly. This is metadata that
is used by the runtime to create an unmanged / managed thunk, which is
(roughly spoken) the function that the native client actually calls. It is
the job of this thunk to call the managed function.
Mar 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.