Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to conver a char* to a VARIANT type in vc++

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0


Hello , i'd like to know how to convert a char* into a VARIANT and vice-versa. I have an VB interface that will pass me a variant and inside my c++ code i'll use it as VARIANT

thanks in advance
 
with VB you can change unicode strings. In them a character instead of one byte have two bytes. The type in wchar_t or BSTR instead of char.
for example
char* x="hello";
wchar_t*y=L"hello";//pay attention on L"
BSTR* z=L"hello";
in COMAPI you'll work as:
VARIANT x;
VariantInit(&x);
V_VT(&x)=VT_BSTR;
V_BSTR(&x)=SysAllocString(L"hello");
....using x
VariantClear(x);
It atl is much simplier:
_variant_t x;
x=L"hello";
for using ATL is enough to
#include<atlbase.h>
#include<comdef.h> John Fill
1c.bmp


ivfmd@mail.md
 
Suppose you have two variabiles m_str and m_variant, you can use them as follows:

CString string; (you may use char* to get rid of MFC)
VARIANT m_variant;
::VariantInit(&m_variant)
m_variant.vt=VT_BSTR;

1. From VARIANT to string
string = m_variant.bstrVal;

2. From string to VARIANT
m_variant=COleVariant(string).Detach(); //MFC

//NO MFC
SysFreeString(m_bstrVal); // Free previous string, if any.
m_bstrMsg = SysAllocString((LPCWSTR)string); //

Hope this helps,
s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top