Delegate as argument (C++/CLI)

V

vijay.gandhi

Hi,

I am trying to convert a set of classes written in VC++ 6.0 to a .DLL
file using VC++ .NET (Visual Studio 2005) and C++/CLI.

I made one of the classes (one at the root level) to be a 'ref' class.
This class used pointer to a function. Since CLR does not support a
ref class to have a pointer to a function directly, I changed it to a
'delegate'.

Now the problem is - a function of one of the other classes (and not a
ref class) expects an argument which is supposed to be a pointer to a
function. This argument is returned from a function in the root class.
Now that I have changed the pointer variable to a delegate, the
compiler complains that it cannot convert from one type (delegate,
which the root class function returns) to another (pointer to
function, which the function of the other class expects).

Here is the snippet:

typedef double (* DISTANCE_FUNC) (double x1, double y1, double x2,
double y2);
delegate double delDistanceFunction(double x1, double y1, double x2,
double y2);

public ref class root_class {
delDistanceFunction^ pDistanceFunction;
delDistanceFunction^ getDistanceFunction() {
return pDistanceFunction;
}
}

public class not_a_ref_class {
not_a_ref_class(DISTANCE_FUNC func);
}

This statement used in root_class gives error:

not_a_ref_class narc = new not_a_ref_class(getDistanceFunction());

It will be great if somebody can help me solve this problem.


Thanks in advance for your time,
Vijay.
 
M

Marcus Heege

Hi Vijay,

Hi,

I am trying to convert a set of classes written in VC++ 6.0 to a .DLL
file using VC++ .NET (Visual Studio 2005) and C++/CLI.

I made one of the classes (one at the root level) to be a 'ref' class.
This class used pointer to a function. Since CLR does not support a
ref class to have a pointer to a function directly, I changed it to a
'delegate'.

Now the problem is - a function of one of the other classes (and not a
ref class) expects an argument which is supposed to be a pointer to a
function. This argument is returned from a function in the root class.
Now that I have changed the pointer variable to a delegate, the
compiler complains that it cannot convert from one type (delegate,
which the root class function returns) to another (pointer to
function, which the function of the other class expects).

Here is the snippet:

typedef double (* DISTANCE_FUNC) (double x1, double y1, double x2,
double y2);
delegate double delDistanceFunction(double x1, double y1, double x2,
double y2);

public ref class root_class {
delDistanceFunction^ pDistanceFunction;
delDistanceFunction^ getDistanceFunction() {
return pDistanceFunction;
}
}

public class not_a_ref_class {
not_a_ref_class(DISTANCE_FUNC func);
}

This statement used in root_class gives error:

not_a_ref_class narc = new not_a_ref_class(getDistanceFunction());

It will be great if somebody can help me solve this problem.


Thanks in advance for your time,
Vijay.

I have written a blog entry about native function pointers. Look at
www.heege.net.

Marcus
 
J

John

Hi Vijay,



















I have written a blog entry about native function pointers. Look atwww.heege.net.

Marcus- Hide quoted text -

- Show quoted text -

Nice writing on the subject on your blog. Thanks for sharing!
I have a similar situation where I simply convert some legacy C++
(VC6) libs to VC2005 DLL projects with /clr support. The DLL will be
called from managed code. The legacy code uses static variables that
need to be initialized. But I am not sure if the managed code do that
on my behalf when the DLL get loadded or I should explicitly call some
routines to force an initialization. The postings (a lot of them) out
on the Internet are confusing since MS has different ways to
initialize statics in different versions of VCs. There is no clear say
on how to do this for C++/CLI under 2005. Can you shed some lights on
this issue.

Thank!
zz
 
V

vijay.gandhi

Hi Marcus,

Thank you very much for your help. I was able to apply your solution
to my problem. It did save a lot of time and effort of searching
around!!

Thanks!
Vijay.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top