tags:

views:

193

answers:

4

I am writing an unmanaged DLL in C++. Is using float as a function parameter in a Windows DLL a good idea?

I'd like my DLL to be usable from as many languages as possible (VB6, .NET, etc). To this end, I've used STDCALL and avoided C++ types.

Will most languages handle float correctly? If not, what should I use?

+1  A: 

All Microsoft languages, such as VB and C# will be just fine with it. In fact, you should be fine using any type that VARIANTs support.

jeffamaphone
+4  A: 

The float type used in most compilers is even standardized in some IEEE format, so go ahead.

xtofl
It's IEEE 754, for the curious.
Iceman
+1  A: 

The trick is to figure out how to "marshall" the value from the calling language into the type expected by the function in the dll. Many times the data type in the calling language will be similar but slightly different than the type used by C. When that happens you need a way to copy the value into the C type and then pass that to the DLL.

Hopefully this article from the MSDN can get you started:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx

Jason Dagit
I guess my question is whether I need to Marshall floats in a straigh C++ DLL (unmanaged).
jm
@jm: floats are blittable types that look the same on the C# side as they do in C++. See this article: http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx
Iceman
A: 

The short answer is that mainstream languages will handle float parameters to a DLL function with little to no overhead.

You're fine using any of the fundamental C data types, and if calling code can't handle it the problem is on their end, not yours, since you produced a perfectly valid DLL.

Iceman