CString is a dynamic TCHAR array, and BSTR is a proprietary format string (it needs to be operated by the function provided by the system, and LPCTSTR is just a constant TCHAR pointer.
CString is a completely independent class, a dynamic TCHAR array, which encapsulates operators such as+and string manipulation methods.
typedef ol echar FAR * BSTR;
typedef const char * LPCTSTR
Representation of various strings in vc++
First of all, char* is a pointer to an ANSI character array, in which each character occupies 8 bits (the effective data is 7 bits except the most significant bit), thus maintaining the compatibility with traditional C and C++.
LP stands for long pointer. LPSTR is a pointer to an ANSI character array ending in' \ 0', which can be used interchangeably with char*. LPSTR is widely used in win32.
The added' c' in LPCSTR means' CONSTANT', which means that the instance of this data type cannot be changed by its API function, and it is equivalent to LPSTR.
1.LP stands for long pointer. Under win 16, there is a difference between long pointer (LP) and short pointer (p), but there is no difference under win32, and they are all 32 bits. So here LP and P are equivalent.
2.c stands for constant
3. what is t? We know that Tchar is wchar_t when it is compiled in Unicode, and it is compiled into char in normal time.
In order to meet the needs of internationalization of program code, the industry put forward the Unicode standard, which provides a simple and consistent way to represent strings. All the bytes in the character are 16 bits, which can also meet the coding requirements of almost all written characters in the world. Unicode (type wchar_t) is encouraged when developing programs.
LPWSTR and LPCWSTR are produced, which are similar in meaning to LPSTR and LPCSTR, except that the character data is 16-bit wchar_t instead of char.
Then, in order to realize the universality of the two codes, the definition of TCHAR is put forward:
If _UNICODE is defined, it is declared as follows:
typedef wchar _ t TCHAR;
If _UNICODE is not defined, it is declared as follows:
typedef char TCHAR;
What LPTSTR and LPCTSTR mean is that every character is like this TCHAR.
The characters in CString class are declared as TCHAR type, which provides a packaged class for users' convenience.
LPCTSTR:
#ifdef _UNICODE
typedef const wchar _ t * LPCTSTR
# Otherwise
typedef const char * LPCTSTR
#endif
Detailed description of VC universal data types using conversion
Firstly, some common variable types are defined to illustrate.
int I = 100;
long l = 200 1;
Floating point f = 300.2
Double d =12345.119;
Char user name [] = "Xia Cheng Pei Jun";
Carbonization temperature [200];
char * buf
CString string;
_ var _ t v 1;
_ bstr _ t v2
First, other data types are converted to strings.
Short integer (int)
itoa(i,temp, 10); //Convert I into a string and put it in temp. The last number represents decimal.
itoa(i,temp,2); //Convert in binary mode
Long (long)
ltoa(l,temp, 10);
Second, get a pointer to the string from other variables that contain the string.
CString variable
Str = "2008 Beijing Olympic Games ";
buf =(LPSTR)(LPCTSTR)str;
A variable of type BSTR.
V 1 = (_bstr_t) "programmer";
buf = _ com _ util::ConvertBSTRToString((_ bstr _ t)v 1);
Third, the string is converted to other data types.
strcpy(temp," 123 ");
Short integer (int)
I = atoi(temp);
Long (long)
L = atol (temperature);
Floating point (double precision)
D = atof (temperature);
Fourth, other data types are converted to CString.
Use CString's member function format for conversion, for example:
integer
Strait. Format ("%d", i);
floating-point number
Strait. Format ("%f", i);
Data types already supported by CString constructors, such as string pointer (char *), can be assigned directly.
Str = user name;
Verbs (abbreviation of verb) BSTR _ bstr _ t and CComBSTR
CComBSTR and _BSTR_t are the encapsulation of BSTR, which is a 32-bit pointer to a string.
Char * can be converted into BSTR as follows: BSTR B = _ com _ util:: convertstringtobstr ("data"); //You need to add the header file comutil.h before using it.
On the contrary, you can use char * p = _ com _ util:: convertbstortstring (b);
6. Variants, _variant_t and COleVariant
The structure of VARIANT can refer to the definition of structure tagVARIANT in the header file vc98 \ include \ oaidl.h
Assignment of variables: assign values to vt members first, indicating the data type, and then assign values to variables with the same data type in the joint structure. For example:
Variant va;
int a = 200 1;
Va.vt = VT _ I4// indicates integer data.
va . lval = a; //assignment
For Variants that do not assign values immediately, it is best to use Void Variant init (Variant Argfar * pvarg) first; The essence of initialization is to set vt to VT_EMPTY. In the following table, we list the correspondence between vt and public data:
Unsigned character bValVT_UI 1
Short iValVT_I2
Long lValVT_I4
Floating floating valve; Vermont R4
Double dblVal Vermont R8
VARIANT _ BOOL boolVal boolean
Kauders Calder; VT _ error
CY cyValVT_CY
Date date; date
BSTR·bstr val; BSTR, Vermont
IUnknown FAR * punkVal unknown.
Idspatchfar * pdispval scheduling
SAFEARRAY FAR * parrayVT_ARRAY|*
Unsigned charfar * pbvalvt _ Byref | vt _ ui1
Short and far axis; VT_BYREF|VT_I2
long FAR * plValVT_BYREF
float FAR * pfltValR4
double FAR * pdblValR8
VARIANT _ BOOL FAR * pboolValBYREF | Boolean
SCODE FAR * pscodeVT_BYREF
CY FAR * pcyValBYREF
DATE FAR * pdate date
BSTR FAR * pbstrVal; BSTR
I don't know far * far * ppunkval unknown
Idspatchfar * far * PPDISPvalByRef | Scheduling
SAFEARRAY FAR * FAR * pparrayVT_ARRAY|*
Variant FAR * pvarValBYREF|VT_VARIANT
void FAR * byrefBYREF
_VARIANT_t is an encapsulated class of VARIANT, its assignment can use forced type conversion, and its constructor will automatically handle these data types.
For example:
L = 222
ing I = 100;
_ variant _ t lVal(l);
lVal =(long)I;
The usage of COleVariant is basically the same as that of _variant_t, please refer to the following example:
COleVariant v3 = "string ",v4 =(long) 1999;
CString str =(BSTR)v3 . pbstrval;
long i = v4.lVal
Seven. others
In the process of message processing, we often need to decompose 32-bit data (DWORD) such as WPARAM or LPARAM into two 16-bit data (WORD), such as:
LPARAM lParam
WORD loValue = LOWORD(lParam); //Take the low 16 bit.
WORD hi value = hi WORD(lParam); //Take 16 bit high.
For 16 bit data (word), we can decompose it into two 8-bit data (bytes) in the same way, for example:
WORD wValue
BYTE loValue = LOBYTE(wValue); //Take the lower 8 bits
BYTE hi value = hi BYTE(wValue); //Take 8 high places.
How to assign a variable of type CString to a variable of type char*
1, GetBuffer function:
Use CString::GetBuffer function.
char * p;
CString str = " hello
p=str。 GetBuffer (string. GetLength());
Strait. ReleaseBuffer()。
When converting CString to char *
CString str(" aaaaaaaa ");
strcpy(str。 GetBuffer( 10)," aa ");
Strait. ReleaseBuffer()。
Call GetBuffer(int n) when we need a character array, where n is the length of the character array we need. Make sure to call ReleaseBuffer () as soon as you use it.
It is also very important to avoid using const char * where it can be used.
2、memcpy:
CString mCS = _ T(" cxl ");
char mch[20];
memcpy(mch,mCS,20);
3. use LPCTSTR to force conversion: try not to use it.
char * ch
CString string;
ch =(LPSTR)(LPCTSTR)str;
CString str = " good
char * tmp
sprintf(tmp," %s ",(LPTSTR)(LPCTSTR)str);
4、
CString Msg
Msg = Msg+“ABC”;
LPTSTR lpsz
New TCHAR GetLength()+ 1];
_tcscpy(lpsz,Msg);
char * psz
strcpy(psz,lpsz);
Transformation from CString class to const char *
char a[ 100];
CString str(" aaaaaa ");
strncpy(a,(LPCTSTR)str,sizeof(a));
Or as follows:
strncpy(a,str,sizeof(a));
Both of the above usages are correct. Because the second parameter type of strncpy is const char *, the compiler will automatically convert the CString class to const char *.
CString to LPCTSTR (const char *)
CString
const char * lpctStr =(lpctStr)cStr;
LPCTSTR to CString
LPCTSTR lpctStr
CString cStr = lpctStr
Assign a variable of type char* to a variable of type CString.
You can assign values directly, for example:
CString myString = "This is a test";
You can also use constructors, such as:
CString s 1(" Tom ");
Assign a variable of type CString to a variable of type char [] (string)
1, sprintf () function
CString str = " good
char tmp[200];
sprintf(tmp," %s ",(LPCSTR)str);
The forced conversion of (LPCSTR)str is equivalent to (LPTSTR)(LPCTSTR)str.
Use (LPTSTR)(LPCTSTR)str when the variable of CString class needs to be converted to (char*).
But LPCTSTR is const char *, that is, the result string is not writable! Forcibly converting it to LPTSTR and deleting const is extremely dangerous!
It's over if you're not careful! To get char *, you should use GetBuffer () or GetBufferSetLength (), and then call ReleaseBuffer ().
2.strcpy () function
CString string;
char c[256];
strcpy(c,str);
char mychar[ 1024];
CString source = " Hello
strcpy((char *)& amp; mychar,(LPCTSTR)source);
About the use of CString
1, please specify the CString parameter.
For most functions that require string parameters, it is best to designate the formal parameters in the function prototype as const pointer pointing to characters, not CString.
When a parameter is specified to point to a character's const pointer, the pointer can be passed to a TCHAR array (such as the string ["here"]) or a CString object.
The CString object will be automatically converted to LPCTSTR. You can also use CString objects anywhere you can use LPCTSTR.
2. If the parameter will not be modified, it will also be designated as a constant string reference (i.e. constcstring &; )。 If the function wants to modify the string,
Const modifier was deleted. If you need to default to a null value, initialize it to an empty string [""], as shown below:
void add customer(const CString & amp; Names, constants, and strings. Address, constant string and. Comment = "";
3. For most function results, just return the CString object by value.
Basic operation of string
Many high-level languages provide corresponding operators or standard library functions to realize the basic operation of strings.
For the convenience of description, first define several related variables:
char s 1[20]="dir/bin/appl ",s2[20]="file.asm ",s3[30],* p;
Int result;
The following uses the string operation in C language to introduce the basic operation of string.
1, find the string length.
int strlen(char * s); //Find the length of the string s.
Example printf("%d ",strlen (s1)); //The string length of the output s 1 is 12.
2. String replication
char *strcpy(char *to,* from); //Copy the from string to the to string and return the pointer to the beginning of.
Example strcpy(s3, s1); //S3 = "dir/bin/appl ",and the string of S 1 remains unchanged.
3. Relationship
char *strcat(char *to,char * from); //Copy the from string to the end of the to string.
//and return the pointer to the beginning of the string.
Example strcat(s3, "/"); //s3="dir/bin/appl/"
strcat(s3,S2); //s3="dir/bin/appl/file.asm "
4. String comparison
int strcmp(char *s 1,char * S2); //Compare the size of s 1 and s2,
//When s 1
Example result=strcmp("baker "," Baker "); //Result & gt0
result=strcmp(" 12 "," 12 "); //Result =0
result=strcmp("Joe "," Joseph ")//result & lt; 0
5. Character positioning
char *strchr(char *s,char c); //Find the position where c first appears in the string s,
//If found, return the location, otherwise return NULL.
Example p = strcr (S2,'.'); //p Point to the position after "File"
if(p) strcpy(p,"。 CPP”); //s2="file.cpp "
note:
① The above operations are the most basic, and the last four operations have variant forms: strncpy, strncath and strnchr.
② see < string.h & gt for other string operations. In different high-level languages, the types and symbols of string operations are different.
③ The remaining string operations can generally be composed of these basic operations.
For example, the operation of finding a substring can be realized as follows:
void substr(char *sub,char *s,int pos,int len){
//s and sub are character arrays, and sub is used to return a substring of length len from the pos character of the string S..
//where 0
If (pos<0 || pos> strlen (s)-1|| len <; 0)
Error ("Wrong parameter!" );
strncpy(sub & amp; s[pos],len); //copy up to len characters in s[pos] to sub.