Passing double to Unmanaged Functions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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/d.../html/cpconConsumingUnmanagedDLLFunctions.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconplatforminvokedatatypes.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.
 
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);
 
Back
Top