Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Unmanaged DLL calling back C++ Managed application function ... Ho

15 views
Skip to first unread message

Marcus Heege

unread,
Oct 1, 2005, 7:29:35 AM10/1/05
to
There are two options:

1) Managed global functions and managed functions of native classes can have
native calling conventions. This means that they can be called from native
code. To enable this, the linker emits metadata that the CLR marshaler can
use to generate a unmanaged/managed thunk.

Maybe this code is more understandable:

<code language="CPPCLI">
#include <stdio.h>

typedef void (__stdcall* PFN)();

#pragma managed // not necessary here
void __stdcall f()
{
System::Console::WriteLine("This is a managed function");
}

#pragma unmanaged
void CallFunction(PFN pfn)
{
printf("This is an unmanaged function\n");
printf("Calling via pointer\n");
pfn();
printf("Direct call\n");
f();
}
#pragma managed

int main()
{
CallFunction(&f);
}
</code>

Notice that the managed function f has a native calling convention and can
therefore be called by the native function. If you inspect the assembly with
ILDASM, you will find .vtfixups in the manifest and a .vtentry line in f().

Option 2: You can define your own P/Invoke function that uses a delegate as
an argument type instead of a function pointer.. If you choose this option,
you can also use functions of a managed class as the target function. This
is even true for nonstatic functions of a managed class. Therefore this
option can be very interesting, too. Implementing a C function pointer
callback that forwards a call to a special instance of a class is a real
pain. With delegates as arguments, this is very simple.

Marcus Heege

"H.B." <H...@discussions.microsoft.com> wrote in message
news:9374BE01-5ACF-41E8...@microsoft.com...
> Hi,
>
> I need to make a function that can display data on my Managed C++ app and
> be
> called by an unmanaged C++ DLL. Something like :
>
> void Form1::Form1_Load(System::Object * sender, System::EventArgs * e)
> {
> MyDLLInit(MyAppDisplayFunction);
> }
>
> void Form1::MyAppDisplayFunction()
> {
> MyAppTextBox->Text = S"My DLL called this function";
> }
>
> Can I make that with CALLBACK ? If so, how to access member from callback?
>
> Any ideas,
>
> Hugo


0 new messages