468,315 Members | 1,519 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Passing function pointer as delegate and invoking with parameters

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
0 1662

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Ben | last post: by
17 posts views Thread by Charles Sullivan | last post: by
5 posts views Thread by Mike | last post: by
10 posts views Thread by ChrisB | last post: by
10 posts views Thread by vcquestions | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.