473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Delegate as argument (C++/CLI)

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
3 4323
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Bowman | last post by:
Hello, I need some help getting a callback delegate passed as an argument to a dynamically linked Dll method so it in turn, can eventually call it. Below is the salient portions of code I'm...
0
by: Budi | last post by:
Hi, We try callback C# method from C++ DLL using delegate, using delegate without argument works fine for me however the CallBackFunctionWithParameter which call delegate with parameter crash...
5
by: Edward Diener | last post by:
The first type to a delegate constructor is obvious. It is a System::Object * . What is the MC++ type of the second argument to the delegate constructor ? In C++ it would be a member function...
11
by: Florian A. | last post by:
Hi everyone! I'm trying to write a global hook in C# and C++/CLI. I'm almost done if there wasn't this little delegate problem. I get the function pointer to a delegate and pass it to my...
5
by: karch | last post by:
I can't figure out why this code does not compile - says that 'num' in an undeclared identifier and that 'delegate':identifier not found. Thoughts? Help? void MethodD() { List<int>^ data;...
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
7
by: Andreas Reiff | last post by:
Hey! I have a managed c++ app (actually, the app is mixed managed/unmanaged, but this happens in the managed part) that dynamically generates some program in c# (with CSharpCodeProvider). The...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.