470,833 Members | 1,451 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Passing function pointer as delegate and invoking with parameters


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
//function call
void CMyClass::PassData()

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

//This is a pointer to the managed class
pCtrl = gcnew Controller;
//pass delegate to managed function


//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)
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
MyDelegate resCallBack2 = new MyDelegate(rCallBack);

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.

Jun 8 '06 #1
0 1789

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 mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.