Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Thu, 8 Jun 2006 09:55:02 -0700,    group: microsoft.public.dotnet.distributed_apps        back       


Passing function pointer as delegate and invoking from managed cod   
Hi,

I have an unmanaged application that converts a function pointer to a 
delegate and then pass this as a parameter(delegate) to a managed function 
which then invokes it. Currently Im able to jump to this unmanaged function, 
but the values of the parameters Im seeing inside this unmanaged function 
are not correct(they have some garbage values).

//unmanaged class (C++ application)

//This is how I have defined the function in umnamanged application
typedef void(__clrcall * FuncPtrRes)(System::Int32);


//This function converts the function pointer to delegate and passes to 
managed
//function call
void CMyClass::PassData()
{

    AsyncCallback^ aCallBackRes;
    FuncPtrRes aPtr  = &CMyClass::UpdateResults;
    ManagedSpace::Controller::MyDelegate ^ resDelegate 
=(ManagedSpace::Controller::MyDelegate 
^)Marshal::GetDelegateForFunctionPointer((System::IntPtr)aPtr, 
ManagedSpace::Controller::MyDelegate::typeid);

   //This is a pointer to the managed class
   pCtrl = gcnew Controller;
  //pass delegate to managed function
   pCtrl->InitializeController(resDelegate);

}

//function whose function pointer is passed (delegate function)
void CMyClassUpdateResults(System::Int32 intval)
{
               //The value for inval is lost when I get here.
	int myval = intval;
}


//Managed class (dll)

public class Controller  //: IController
    {
          public delegate void MyDelegate(System.Int32 intval);

        public void InitializeController(MyDelegate rCallBack)
        {
            try
            {
                   System.Int32 myint = new System.Int32();
                    myint = 45;
                   //Here is where Im invoking the delegate(function pointer 
from   unmanaged). Im passing int as 45, but when I step into the callback 
function i.e. , the value is lost i.e. myint is not 45 inside the callback 
function.
                   MyDelegate resCallBack2 = new MyDelegate(rCallBack);
                   resCallBack2(myint);
           }
}

 Will really really appreciate all the help on the above. Is there something 
specific we need to do with parameters that are passed to unmanaged callback 
functions(that have been converted to delegates) like the above case. Please 
let me know if there is any other better way of invoking unmanaged functions 
(as delegates) from managed code.

Thanks
Hx
date: Thu, 8 Jun 2006 09:55:02 -0700   author:   Haxan

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us