开发者

How to correctly set CURRENCY value in C++

开发者 https://www.devze.com 2023-04-12 20:56 出处:网络
In a C++ DLL which is used from a legacy VB6 program, I use the CURRENCY data type to return some large values.

In a C++ DLL which is used from a legacy VB6 program, I use the CURRENCY data type to return some large values.

The code I'm using to fill it is this:

CURRENCY INT64toCURRENCY(INT64 int64) 
{
    CURRENCY ret;
    ret.int64 = int64 * 10000;
  开发者_JAVA百科  return ret;
}

An integer overflow might arise here, but this is the least problem: The code doesn't work for negative numbers, INT64toCURRENCY(-1) appears in VB6 as 4 294 967 295 which is 232–1.

However, cout << INT64toCURRENCY(-1).int64 shows –10000 which I suppose is correct.

The VB6 program is basically this:

Public Declare Function My_Fun Lib "My_Lib.dll" _
(ByVal param As Boolean) As Currency
Dim c As Currency
c = My_Fun(True)

and My_Fun is defined as

extern "C" CURRENCY __stdcall My_Fun(VARIANT_BOOL param)
{
    return INT64toCURRENCY(-1);
}

What's the problem here and what is the correct code to convert a signed 64-bit integer to CURRENCY?


VarCyFromI8 is the best solution.

http://msdn.microsoft.com/en-us/library/ms644370(v=vs.85).aspx

The CURRENCY type is a bit tricky with regards to signed values; just use what is already there rather than mess with it on your own.

Note that the 64 bit variants are not technically supported until XP. Of course you could use the other VarCyFrom* functions, eg with a DECIMAL or an I4.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号