473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(deleg ate) 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)(Sys tem::Int32);
//This function converts the function pointer to delegate and passes to
managed
//function call
void CMyClass::PassD ata()
{

AsyncCallback^ aCallBackRes;
FuncPtrRes aPtr = &CMyClass::Upda teResults;
ManagedSpace::C ontroller::MyDe legate ^ resDelegate
=(ManagedSpace: :Controller::My Delegate
^)Marshal::GetD elegateForFunct ionPointer((Sys tem::IntPtr)aPt r,
ManagedSpace::C ontroller::MyDe legate::typeid) ;

//This is a pointer to the managed class
pCtrl = gcnew Controller;
//pass delegate to managed function
pCtrl->InitializeCont roller(resDeleg ate);

}

//function whose function pointer is passed (delegate function)
void CMyClassUpdateR esults(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(Syst em.Int32 intval);

public void InitializeContr oller(MyDelegat e rCallBack)
{
try
{
System.Int32 myint = new System.Int32();
myint = 45;
//Here is where Im invoking the delegate(functi on 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(rCal lBack);
resCallBack2(my int);
}
}

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 2004

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

Similar topics

37
4952
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well...
12
2786
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 function descriptors for the overloaded functions of that name. A function descriptor would contain the address of the function to be called, and a...
17
3576
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
5
2330
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 ,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete) { // Bombs on this line: ThreadStart newThreadStart = new...
7
9545
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 subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event...
0
1470
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 into a background thread and get other data out and also update the main thread on which the main form was created. It seems to work fine. The...
10
15142
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( thesub as <functionptr?> ) dim t as new system.threading.thread( _ new system.threading.threadstart( Addressof thesub )) .... How can I get...
12
11349
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), I am calling a managed C# method(I use gcnew to instantiate the managed class). One of the parameters of this C# method is a delegate. I need to...
10
2101
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. Let's say we have 2 delegates: public delegate void FirstDelegate( int i ); public delegate void SecondDelegate( int i );
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.