HighTechTalks DotNet Forums  

Passing function pointer as delegate and invoking from managed cod

Dotnet Distributed Applications microsoft.public.dotnet.distributed_apps


Discuss Passing function pointer as delegate and invoking from managed cod in the Dotnet Distributed Applications forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Haxan
 
Posts: n/a

Default Passing function pointer as delegate and invoking from managed cod - 06-08-2006 , 12:55 PM






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




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.