By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Passing function pointer as delegate and invoking with parameters

P: n/a
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 inside this function Im seeing 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
Jun 8 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.