ManagedC++, Garabage collection and function pointer

L

Lloyd Dupont

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>
 
M

Marcus Heege

Lloyd Dupont said:
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.
 
Top