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 / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

Unmanaged Function Pointer from Managed Code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Olbert - 20 Sep 2004 20:10 GMT
I have a legacy unmanaged DLL function that expects to be passed a structure like this:

typedef struct tag_ACALLBACK
{
    void *ncCallbackFunction;
    void *ncUserData;
} ACALLBACK;

I'm stumped about how to assign a managed class method to the ncCallbackFunction function pointer.
I've tried creating a __delegate of the proper structure, but, as I half expected, you can't assign
a delegate to a void*:

IdleCallbackHandler* idleHandler = new IdleCallbackHandler(this, &MyClass::IdleCallback);
theSettings->nstIdle.ncCallbackFunction = idleHandler;    // this fails

I've read about, and used, the marshaling/pinvoke capability to pass a delegate to an unmanaged
function that expects a function pointer, but how do I assign a delegate to a function pointer
member variable?

- Mark
Mattias Sj?gren - 21 Sep 2004 11:59 GMT
>I've read about, and used, the marshaling/pinvoke capability to pass a delegate to an unmanaged
>function that expects a function pointer, but how do I assign a delegate to a function pointer
>member variable?

Use the same technique here. Write a function that accepts and returns
a function pointer, and call it through P/Invoke passing in a
delegate. Assign the return value to the struct member.

In v2.0 there will be a better way.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


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.