3

In this example m_Amount is CString. stringToNumber function converts it to a LONGLONG number successfully. But when I want to assign it to a variant I get this error:

error C2440: 'type cast' : cannot convert from '__int64' to 'class _variant_t'

mycode

_variant_t  myVar = _variant_t( (LONGLONG)stringToNumber(m_Amount) );
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
Bob
  • 22,810
  • 38
  • 143
  • 225

4 Answers4

1

You don't need the cast.

Use:

_variant_t(
   __int64 i8Src
)

See _variant_t MSDN for further reference.

Windows CE however does not provide all data types available on a regular Windows XP/Vista/Win 7 installation. In your case, this means that WinCE does not support 64-bit signed/unsigned integers. So, you are best advised to use either a narrower data type or roll your own type (that uses perhaps two ints) to get a LONGLONG.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • is this code correct? `_variant_t myVar = _variant_t( stringToNumber(m_Amount) );` – Bob May 02 '12 at 06:05
  • Yes! This should work. Also, I prefer the following: `_variant_t myVar( stringToNumber(m_Amount) );`. – dirkgently May 02 '12 at 06:09
  • BTW: Aren't you better off using `VARIANT` or `CComVariant` (until unless you have project specific requirements)? – dirkgently May 02 '12 at 06:10
  • `_variant_t myVar = _variant_t( stringToNumber(m_Amount) );` did not work and get same error. i can not change the type of myVar unfortunately. – Bob May 02 '12 at 06:13
  • Are you on using Windows CE? In that case you may have to use a narrower integer type. – dirkgently May 02 '12 at 06:14
  • I am developing on Windows Mobile by eVC++. – Bob May 02 '12 at 06:17
0

What toolchain are you using?

There should be a conversion from __int64 to _variant_t if _WIN32_WINNT >= 0x501, which corresponds to a target of at least WinXP. That should be true for up to date Windows toolchains, but you may need to explicitly set that macros definition yourself with some older tools (like VS 2005, I believe).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I am developing on Windows Mobile by eVC++. I did not get your purpose. I am new in C++ – Bob May 02 '12 at 06:16
  • Ah - I'm not familiar with eVC++ and whatever SDK it might be using, but I wouldn't be too surprised if it doesn't support `__int64` in the `_variant_t`. – Michael Burr May 02 '12 at 06:20
0

Try this one:

union UltimateCast
{
   __int64 From;
   __variant_t To;
};

Put the __int64 value you need to cast from into the From field. After that read the resulting value from the To field.

  • 1
    Save your int64 value into From field and read the casting result from To. This is actually a reinterpret cast. – Sergey K. May 02 '12 at 07:06
  • how these values are converted? – Bob May 02 '12 at 07:09
  • I have fixed the answer according to the comment above. The values share the same memory location, so one will be interpreted as the other. –  May 02 '12 at 07:10
  • what is this error: `error C2621: union 'UltimateCast' : member 'To' has copy constructor` – Bob May 02 '12 at 07:21
  • @breceivemail: Union's in C++03 were not allowed to have types with copy ctors (or assignment operators or non-trivial ctors). Some compiler may provide this as a feature but WinCE probably doesn't. – dirkgently May 02 '12 at 08:05
  • @TonyPerez: This won't work on his system. He is using WinCE and the compiler complies with C++03 where union members cannot have non-trivial special member functions: `object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor can an array of such objects`. – dirkgently May 02 '12 at 08:08
0

I was using _variant_t to insert LONGLONG data into the SQL server database. As mentioned in this asnwer numeric is mapped to CString in C++. So I retrieve my numeric data from database as string, convert it to LONGLONG, change and then convert it to string again and store result string to database.

SQL numeric --(retrieve from database)--> string -> LONGLONG -> string --(insert into the database)--> SQL numeric

Community
  • 1
  • 1
Bob
  • 22,810
  • 38
  • 143
  • 225