Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
struct tagVARIANT VARIANT;
{
''''
}

VARIANT v1;


I am trying to assign an int value 111 or a string value "111" to v1. Can anyone help? Thx ahead.
Posted
Updated 14-Aug-11 22:59pm
v2
Comments
Dalek Dave 15-Aug-11 4:59am    
Edited for Code Block.
Simon_Whale 15-Aug-11 5:16am    
you need to adjust your tags accordingly as you have both VB10 and C++ they are 2 completely different languages
Sergey Alexandrovich Kryukov 15-Aug-11 23:02pm    
Variants... one of the worst inventions in the history of programming.
--SA

C++
v1.intVal = 111;
v1.bstrVal = "111";  // if UNICODE then L"111"
 
Share this answer
 
Comments
sandy_1126 15-Aug-11 5:28am    
I try both, for the second one, there is a error when debuging. "a value of type "const char *" cannot be assigned to an entity of type "BSTR""
Richard MacCutchan 15-Aug-11 7:45am    
Probably means that you are building for UNICODE, you should use the second option L"111".
Take a look here[^]
On the bottom of page you'll find an example.
 
Share this answer
 
Comments
Richard MacCutchan 15-Aug-11 5:14am    
There is no example in the link you provided, or did I miss something?
Maciej Los 15-Aug-11 8:54am    
You missed something!
Richard MacCutchan 15-Aug-11 9:23am    
What exactly? That link is about the C++ struct type, I see nothing about VARIANT.
Maciej Los 15-Aug-11 15:39pm    
Ok, you have right. Sorry for misunderstanding.
VARIANT v1;
v1.vt = VT_I4;
v1.lVal = 111;

or

VARIANT v1;
v1.vt = VT_BSTR;
v1.bstrVal = SysAllocString("111");

Good luck.
 
Share this answer
 
v2
Comments
sandy_1126 15-Aug-11 5:32am    
I try the second one, but error occurs. "argument of type "const char *" is incompatible with parameter of type "const OLECHAR *"" = =
mbue 15-Aug-11 8:41am    
Sorry:
v1.bstrVal = SysAllocString(L"111");
Add
#include "oaidl.h" 

for VARIANT
and Unicode version:
VB
VARIANT v1;
v1.vt = VT_BSTR;
v1.bstrVal = L"111";


should work.

Update Try Unicode version.

This is a tested code (took some time to recall):

XML
#include <oaidl.h>
#include <iostream>
#include <comutil.h>


int main()
{
    VARIANT v1[1];
    v1[0].iVal = 111;
    std::cout<<"int:"<<v1[0].iVal<<'\n';

    BSTR bstrt(L"111");
    v1[0].bstrVal = bstrt;

    std::wcout<<"string:"<<v1[0].bstrVal<<'\n';
return 0;

}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900