Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
H.B. - 30 Sep 2005 17:37 GMT
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
Marcus Heege - 01 Oct 2005 12:29 GMT
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

> Hi,
>
[quoted text clipped - 17 lines]
>
> Hugo

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.