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

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

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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." <HB@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.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

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.