By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,979 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,979 IT Pros & Developers. It's quick & easy.

Delegate as argument (C++/CLI)

P: n/a
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.

May 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Vijay,

<vi**********@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
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

May 15 '07 #2

P: n/a
On May 15, 1:19 am, "Marcus Heege" <nos...@heege.netwrote:
Hi Vijay,

<vijay.gan...@gmail.comwrote in message

news:11**********************@y5g2000hsa.googlegro ups.com...


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

May 15 '07 #3

P: n/a
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.

May 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.