472,805 Members | 777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 1919

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
5
by: Mike | last post by:
Hi! I am trying to write a function that will span a thread for me. Eventually I want to incorporate this finction into a class: public void SpanThread(string threadName,string threadDelegate...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
10
by: ChrisB | last post by:
Coming from a C/C++ background, how would I pass a function pointer to a function? I want to write a function that handles certain thread spawning. Here's what I'm trying to invision: function(...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.