.NETGURU
Callback issue again.
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Tamas Karoly via .Net Guru (VIP)
Hi Guys,

This might have come up several times, but I still have problems with calling a managed object's member function from an unmanaged function. Here is what I was trying to do:

-------- unmanaged code -------
typedef void (*CALLBACK_FN)();

static CALLBACK_FN mFn = NULL;

void SetCallbackFn(CALLBACK_FN Fn)
{
    mFn = Fn;
}

void CallCallbackFn()
{
    mFn();
}

-------- managed code -------
public __delegate void ManagedCallback();

public __gc class CManagedWrapper
{
    GCHandle    mFnHandle;
public:
    void SetCallback(ManagedCallback* Fn)
    {
        mFnHandle = GCHandle::Alloc(Fn);
        SetCallbackFn((CALLBACK_FN)Fn);
    }

    void ReleaseCallback()
    {
        mFnHandle.Free();
    }

    void CallFunction()
    {
        CallCallbackFn();
    }
};

When I call CManagedWrapper.CallFunction() I receive an 'Object reference was not set to an instance of an object' error, although I called SetCallback() with param new ManagedCallback(<some function in an object>). I guess the problem is that I simply comvert the delegate to a c-style function pointer. However, the GCHandle.Alloc() should pin the object.

Could you please tell me what's wrong with this code or suggest any other way to achieve the same result?

Thank you,
Tamas Karoly

-----------------------
Posted by a user from .Net Guru (http://www.dot-net-guru.com/)

<Id>lFOSqu6GiUyAwDyD99weyA==</Id>
Reply to this message...
 
    
Dot Net Guruias Sjögren
Tamas,

>However, the GCHandle.Alloc() should pin the object.

No, since you're not allocating a pinning handle. And even if you
were, a pointer to a pinned delegate isn't the same as a function
pointer.

>Could you please tell me what's wrong with this code or suggest any other way to achieve the same result?

Have a look at http://www.codeproject.com/managedcpp/cbwijw.asp

Dot Net Guruias

--
Dot Net Guruias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply to this message...
 
 
System.Runtime.InteropServices.GCHandle




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification