467,885 Members | 1,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

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
  • viewed: 1819
Share:
2 Replies

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by John Pass | last post: by
17 posts views Thread by mr.resistor | last post: by
18 posts views Thread by tbringley | last post: by
4 posts views Thread by sofeng | last post: by
4 posts views Thread by Andreas Reiff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.