typedef LPWSTR LPTSTR;
typedef LPCWSTR LPCTSTR;
#else
typedef LPSTR LPTSTR;
typedef LPCSTR LPCTSTR;
#endif
VARTYPE vt;
union {
short iVal; // VT_I2.
long lVal; // VT_I4.
float fltVal; // VT_R4.
double dblVal; // VT_R8.
DATE date; // VT_DATE.
BSTR bstrVal; // VT_BSTR.
…
short * piVal; // VT_BYREF|VT_I2.
long * plVal; // VT_BYREF|VT_I4.
float * pfltVal; // VT_BYREF|VT_R4.
double * pdblVal; // VT_BYREF|VT_R8.
DATE * pdate; // VT_BYREF|VT_DATE.
BSTR * pbstrVal; // VT_BYREF|VT_BSTR.
};
};
:: VariantInit(&va); // 初始化
int a = 2002;
va.vt = VT_I4; // 指明long數據類型
va.lVal = a; // 賦值
COleVariant v2 = "This is a test";
// 結果是VT_BSTR類型,值爲"This is a test"
COleVariant v3((long)2002);
COleVariant v4 = (long)2002;
// 結果是VT_I4類型,值爲2002
bstr1 = "Bye"; // 直接賦值
OLECHAR* str = OLESTR("ta ta"); // 長度爲5的寬字符
CComBSTR bstr2(wcslen(str)); // 定義長度爲5
wcscpy(bstr2.m_str, str); // 將寬字符串複製到BSTR中
CComBSTR bstr3(5, OLESTR("Hello World"));
CComBSTR bstr4(5, "Hello World");
CComBSTR bstr5(OLESTR("Hey there"));
CComBSTR bstr6("Hey there");
CComBSTR bstr7(bstr6);
// 構造時複製,內容爲"Hey there"
char * p = "This is a test";
CString theString = chArray;
theString.Format(_T("%s"), chArray);
theString = p;
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
LPTSTR p = s.GetBuffer();
// 在這裏添加使用p的代碼
if(p != NULL) *p = _T('/0');
s.ReleaseBuffer();
// 使用完後及時釋放,以便能使用其它的CString成員函數
#pragma comment(lib, "comsupp.lib")
int _tmain(int argc, _TCHAR* argv[]){
BSTR bstrText = ::SysAllocString(L"Test");
char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); // 用完釋放
delete[] lpszText2;
return 0;
}
char* lpszText2 = b;
BSTR bstrText = ::SysAllocStringLen(L"Test",4);
BSTR bstrText = ::SysAllocStringByteLen("Test",4);
_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal;
BSTR bstrText = bstr.m_str;
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
BSTR bstrText = str.AllocSysString();
…
SysFreeString(bstrText); // 用完釋放
CStringA str;
str.Empty();
str = bstrText;
wchar_t wszStr[] = L"This is a test";
String* str = S”This is a test”;
LPCTSTR tcstr= CA2CT("this is a test");
wchar_t wszStr[] = L"This is a test";
char* chstr = CW2A(wszStr);
27
ESC
32
SPACE
33
!
34
"
35
#
36
$
37
%
38
&
39
'
40
(
41
)
42
*
43
+
44
'
45
-
46
.
47
/
48
0
49
1
50
2
51
3
52
4
53
5
54
6
55
7
56
8
57
9
58
:
59
;
60
<
61
=
62
>
63
?
64
@
65
A
66
B
67
C
68
D
69
E
70
F
71
G
72
H
73
I
74
J
75
K
76
L
77
M
78
N
79
O
80
P
81
Q
82
R
83
S
84
T
85
U
86
V
87
W
88
X
89
Y
90
Z
91
[
92
/
93
]
94
^
95
_
96
`
97
a
98
b
99
c
100
d
101
e
102
f
103
g
104
h
105
i
106
j
107
k
108
l
109
m
110
n
111
o
112
p
113
q
114
r
115
s
116
t
117
u
118
v
119
w
120
x
121
y
122
z
123
{
124
|
125
}
126
~
LPCTSTR r;r = TEXT("hello");CString s;s = r;BSTR b;b = s.AllocSysString();SysFreeString(b);
When compiled for ASCII
r is narrow, type const char *
s is narrow, type ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char> > >
b is wide, type unsigned short *
When compiled for Unicode
r is wide, type const unsigned short *
s is wide, type ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >
b is wide, type unsigned short *
CString s1;s1 = "hello1";CString s2 = "hello2";CString s3("hello3");
When compiled for ASCII, all 3 strings are made up of narrow characters. When compiled for Unicode, all 3 strings are made up of wide characters, even though the string literals are not escaped with L or TEXT().
goal:
a sample mfc app
that i can compile ascii or unicode
that has a string type
that has functions like has, find, split, before
that has a class that can produce a BSTR
the class can produce ASCII text to write to a file even when compiled for unicode
string literals
"hello"
L"hello"
TEXT("hello")
string types
CString
LPCTSTR
LPTSTR
TCHAR
const WCHAR *
BSTR
compile
#ifdef _UNICODE
#else
#endif
functions
MultiByteToWideChar