473,397 Members | 1,969 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,397 software developers and data experts.

Passing double to Unmanaged Functions

I am new to CppNet
How can I pass a double or float value to Unmanaged function from Managed CppNet Program. I can pass an integer with no problem but when I try it with double or float I get the following compile error
can not convert from __gc float * to float * can not convert from unmanaged to managed
Thanks for your help.
Nov 17 '05 #1
2 2077

1. Use Platform Invoke to define the unmanaged function:
2. Use System types and the Interop Marshal will automatically convert the data from managed to unmanaged:

using namespace System::Runtime::InteropServices;
namespace MyNamespace
{
[DllImport(S"myUnmanagedDLL")] extern "C" void myUnmanagedFunction(System::Double *myDouble, System::Float *myFloat)

public __gc class MyClass
{
......

void myClassFunction() {
System::Double aDouble;
System::Float aFloat;
myUnmanagedFunction(&aDouble, &aFloat);
////etc.....
}
}

check out these pages for more info:
http://msdn.microsoft.com/library/de...LFunctions.asp
http://msdn.microsoft.com/library/en...edatatypes.asp

----- JohnO wrote: -----

I am new to CppNet.
How can I pass a double or float value to Unmanaged function from Managed CppNet Program. I can pass an integer with no problem but when I try it with double or float I get the following compile error:
can not convert from __gc float * to float * can not convert from unmanaged to managed.
Thanks for your help.
Nov 17 '05 #2
Hi John,
I am new to CppNet.
How can I pass a double or float value to Unmanaged function from Managed CppNet Program. I can pass an integer with no problem but when I try it with
double or float I get the following compile error: can not convert from __gc float * to float * can not convert from

unmanaged to managed.
You're problem likely arises from trying to pass a float instance that's
part of a managed reference type (or it's somehow getting into the managed
heap) into unmanaged code. What you should do is pin the object containing
the float instance before getting it's address.

Example:

__gc struct FloatContainer {
float theFloat;
};

....

FloatContainer* fa = new FloatContainer();
fa->theFloat = 12.23;

....
FloatContainer __pin * pinnedFa = fa;
UnmanagedFunction(&pinnedFa->theFloat);

--
Tomas Restrepo
to****@mvps.orgs
Nov 17 '05 #3

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

Similar topics

5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
0
by: Haxan | last post by:
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...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
4
by: Andreas Reiff | last post by:
Hi! I want some communication to take place between a c++ app and a c++ .dll with an intermediate managed/unmanaged c++ dll. Basicall, I want the unmanaged c++ dll to have a callback to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...

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.