tags:

views:

384

answers:

4

hi, I am calling a C++ function from VB6. In which I need to pass variable of Currency datatype. But in C++ we do not have such datatype. what should I use in C++ fuction to make the compatibilty with currency datatype?

+2  A: 

I guess the best bet is to pass it as a VARIANT and manually handle the VARIANT in the C++ code.

sharptooth
A: 

Just take the currency type and multiply it by 100, then pass it to C++ as a long integer. When you needs results back, do the reverse.

There will be no loss of precision, and the code is very simple.

lavinio
Although you will get an overflow if the answer exceeds the possible range of a long.
MarkJ
True, if the amount exceeds $20 000 000 :) If the amounts are large, then an `__int64` would be more appropriate.
lavinio
+1  A: 

The VB6s currency type is roughly the same as the CY type from C++ (assuming a Microsoft compiler)

Internally it is an 8-byte integer which is scaled by a factor of 10,000, giving you 4 digits after the decimal separator.

Depending on the compiler you can directly use the CY type or pass a VARIANT and use myVariant.cyVal (which is of the CY type).

If you don't have the VARIANT and CY type available (they are not part of the C++ standard) your C++ function has to accept a 64-bit integer and you have to divide the value by 10,000 to get the correct value. (Either use __int64 or long long, again depending on the compiler)

DR
+2  A: 

I believe Visual C++ has a native 64-bit integer type __int64 (also known as CY), which is roughly equivalent to VB6 Currency? Although in your C++ code you will "see" the value as 10,000 times larger than the value you "see" in VB6. Either divide by 10,000 in the C++ to get the correct value, or work with the scaled value to keep the precision.

For other C++ compilers Bruce McKinney's bible Hardcore Visual Basic recommends something like this, so does MSDN:

typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG  HighPart;
    };
    LONGLONG QuadPart;    // In Visual C++, a typedef to __int64
} LARGE_INTEGER;

See here for more details.

MarkJ