VC ++ common data type and detailed operation

zhaozj2021-02-16  106

VC common data type and its operation details (unreasonable)

------- Xi'an University of Posts and Telecommunications Computer

Xu Zhaoyuan (Flxyzsby@163.com flxyzsby@yahoo.com.cn)

2004/08/03

table of Contents

One. VC common data type list

two. Common data type transformation

2.1 Mathematical type variables and strings conversion

2.2 CSTRING and STRING, CHAR * Conversion and Operation of Other Data Types

● Comparison of CString, String, Char *

● Mathematical types and cstring transformation

● CSTRING and CHAR * Mutual conversion example

● CSTRING and BSTR conversion

● Varian type translates into a CString type

2.3 BSTR, _BSTR_T and CCOMBSTR

2.4 Variant, _variant_t and Colevariant

Appendix CSTRING and string turnover

Reference book: 9CBS, << MFC deeply shallow (SECOND EDIT) >>

One. VC common data type list

Type

Default size

Description

base

foundation

class

type

all

Yes

small

write

Note: These basic data types are supported for MFC or APIs.

Boolean Unsigned 8 Bit, Value True / False

BYTE UNSIGNED 8 bit, integer, output by character output

Char unsigned 8 bit, character

Double Signed 64 Bit floating point

Float Signed32 Bit floating point

Handle_t

Primitive Handle Type

Hyper Signed 64 bit integer

INT Signed 32 bit integer

Long Signed 32 bit integer

Short Signed 16 bit integer

Small sign 8 bit integer

Void * 32-bit points to unknown types of pointers

Wchar_t unsigned 16 bit 16-bit character, can accommodate more characters than Char

Win32

API

often

use

number

according to

class

type

all

Big

write

Note: These Win32API supported simple data types are mainly used to define the function return value, message parameters, and structural members. This type of data type can be roughly divided into five categories: character type, Boolean, integer, pointer and handle type (?). There are about more than 100 different types,

BOOL / BOOLEAN

8bit, True / False Boolean

Byte

Unsigned 8 bit

BSTR

CCOMBSTR

_BSTR_T

32 bit bstr is a 32-bit pointer to a string

Is the package for BSTR

Is the package for BSTR

Charr

8 Bit (ANSI) character type

ColorRef

32 Bit RGB color value integer

DWORD

Unsigned 32 bit integer

Float

Float Float type

Handle

Object handle

Hbitmap

Bitmap handle

Hbrush

BRUSH handle

Hcursor

Cursor handle

HDC

Device context handle

Hfile

OpenFile opens the File handle

Hfont

Font handle

Hhook

HOOK handle

HKEY

Registry key handle

HPEN

Pen handle

HWnd

WINDOW handle

Int

-------- --------

Long

-------- ---------

Longlong

64-bit zone symbol integer

Lparam

32 BIT message parameters

Lpbool

BOOL type pointer

LPBYTE

BYTE type pointer

LPCOLOREF

ColorRef pointer

LPCSTR / LPSTR / PCSTR

Pointing 8-bit (ANSI) string type pointer LPCWSTR / LPWSTR / PCWSTR

Point to 16-bit Unicode string type

LPCTSTR / LPTSTR / PCTSTR

Pointing one 8 or 16-bit string type pointer

LPVOID

32-bit pointer points to an unspecified type

LPDWORD

Pointing a DWORD pointer

Other Similarity: LPHANDLE, LPINT, LPLONG, LPWORD, LPRESULT

Pbool, Pboolean, Pbyte, Pchar, Pdword, Pfloat, Phandle, Pint, Plong, Pshort ...

Explanation: (1) LP is 16bit, p is 8bit in the 16-bit system, 32bit in the 32-bit system (at this time)

(2) C finger CONST, T represents TCHAR mode, can be worked in ANSI, or Unicode

Short

USIGNED integral

Other uchar, uint, ulong, ulonglong, ushort is an unsigned corresponding type

TBYTE

WCHAR type or char type

TCHAR

ANSI and Unicode can

Variant

_VARIANT_T

Colevariant

A structure reference OAIDL.H

_VARIANT_T is a Package of Variant

Colevariant is also a Variant's package class

WndProc

32-bit pointers pointing to a window process

Wchar

16-bit Unicode characters

Word

16-bit unsigned integer

WPARAM

Message parameters

MFC

Unique

data

Types of

The following two data types are unique data types in the Microsoft Basic Class library

The value of the position of one element in the Position tag, is used by the set class in the MFC

LPCRect points to a RECT structure constant (cannot be modified) 32-bit pointer

CString is actually a class in the MFC

Description:

(1) ------- Expressed omitted

(2) 1byte = 8bit,

The word is related to the machine, in the 8-bit system: word = 1 byte, in the 16-bit system, 1 word = 2 bytes, 32 bits: 1 word = 4 bytes,

1 word = 8 bytes in 64 bits. Don't mix these concepts.

two. Common data type conversion and operation

2.1 Mathematical type variables conversion with strings (these functions are in STDLIB.H)

(1) Convert the mathematical type into a string can be used with the following functions:

Example: _CRTIMP Char * __CDECL _ITOA (int, char *, int); // This is a function that converts numbers to a string type, the last int indicates the conversion

As procedures:

INT ityep = 3;

Char * szchar;

ITOA (ITYPE, SZCHAR, 2);

Cout << Szchar; // output is 1010

Similar function list:

_CRTIMP Char * __cdecl _itoa (int, char *, int); // For integrity, it is also included there in it.

_CRTIMP Char * __CDECL _ULTOA (unsigned long, char *, int);

_CRTIMP Char * __CDECL _LTOA (Long, Char *, Int);

_CRTIMP Char * __cdecl _i64toa (__ int64, char *, int);

_CRTIMP Char * __CDECL _UI64TOA (unsigned __int64, char *, int);

_CRTIMP WCHAR_T * __CDECL _I64TOW (__ int64, wchar_t *, int);

_CRTIMP WCHAR_T * __CDECL _UI64TOW (unsigned __INT64, WCHAR_T *, INT);

_CRTIMP WCHAR_T * __CDECL _ITOW (int, wchar_t *, int); // Convert to long string type

_CRTIMP WCHAR_T * __CDECL _LTOW (long, wchar_t *, int); _ crtimp wchar_t * __cdecl _ultow (unsigned long, wchar_t *, int);

There are still a lot, please study your own

(2) Converting the string type to mathematical type variables to use some of the following functions:

Example: _CRTIMP INT __CDECL ATOI (const char *); // Parameter is very clear

Char * szchar = "88";

INT TEMP (0);

Temp = ATOI (SZCHAR);

Cout << temp;

Similar functions list:

_CRTIMP INT __CDECL ATOI (Const Char *);

_CRTIMP DOUBLE __CDECL ATOF (const char *);

_CRTIMP long __cdecl atol (const char *);

_CRTIMP long double __cdecl _atold (const char *);

_CRTIMP __INT64 __CDECL _ATOI64 (const char *);

_CRTIMP DOUBLE __CDECL STRTOD (const char *, char **); //

_CRTIMP long __cdecl strtol (const char *, char **, int); //

_CRTIMP long double __cdecl _stertold (const char *, char **);

_CRTIMP unsigned long __cdecl start (const char *, char **, int);

_CRTIMP DOUBLE __CDECL WCSTOD (const wchar_t *, wchar_t **); // Long string type conversion to mathematical type

_CRTIMP long __cdecl wcstol (const wchar_t *, wchar_t **, int);

_CRTIMP unsigned long __cdecl wcstoul (const wchar_t *, wchar_t **, int);

_CRTIMP INT __CDECL _WTOI (const wchar_t *);

_CRTIMP long __cdecl _wtol (const wchar_t *);

_CRTIMP __INT64 __CDECL _WTOI64 (const wchar_t *);

There are still a lot, please study your own

2.2. CSTRING and STRING, Char * Conversion and Operation of Other Data Types

(1) Comprehensive comparison of CString, String, Char * (this part of the author JOISE article on 9CBS

<< CString, String, Char * Comprehensive Comparison >> Write very detailed, please read his article carefully.

Address: http://blog.9cbs.net/joise/

Or reference appendix:

(2) Conversion:

● Mathematical types and cstring transformation

Mathematical types are converted to cstring

Available format functions, for example:

CString S;

INT i = 64;

S.Format ("% d", i)

CString Converts to Mathematical Type: Examples CString Strvalue ("1.234");

Double dblvalue;

DBLVALUE = ATOF ((LPCTSTR) Strvalue;

● CSTRING and CHAR * Mutual conversion example

CString Strvalue ("Hello");

Char * szvalue;

Szvalue = Strvalue.getBuffer (SzValue);

CSTRING // can also be used (LPSTSTR) to convert .szValue = (LPSTR) (LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS

In turn, it can be assigned directly:

Char * szchar = NULL;

CString Strvalue;

Szchar = new char [10];

MEMSET (Szchar, 0, 10);

STRCPY (Szchar, "Hello");

Strvalue = szchar;

● CSTRING and BSTR conversion

CSTRING type converted into BSTR type

When we use the ActiveX control programming, you often need to use a value as a BSTR type. BSTR is a string, a wide string (Unicode) on the Intel platform, and can include embedded NULL characters.

You can call the cstring object to convert CString into BSTR:

CString Str;

Str = .....; // whatver

BSTR BSTR = str.allocsystring ();

BSTR type converted to CString

If you compile the code in Unicode mode, you can simply write:

CSTRING Convert (BSTR BSTR)

{

IF (BSTR == Null)

Return CString (_T ("));

CString S (BSTR); // in Unicode Mode

Return S;

}

If it is an ANSI mode

CSTRING Convert (BSTR B)

{

CString S;

IF (b == null)

Return S; // EMPTY for NULL BSTR

#ifdef unicode

s = B;

#ELSE

LPSTR P = S.GetBuffer (Systringlen (B) 1);

:: Widechartomultibyte (CP_ACP, // Ansi Code Page

0, // no flags

B, // Source Widechar String

-1, // Assumen Nul-Terminated

p, // target buffer

Systringlen (b) 1, // Target Buffer LENGTH

Null, // use system default char

NULL); // Don' '''t Care if default used

S.ReleaseBuffer ();

#ENDIF

Return S;

}

● Varian type translates into a CString type

Variant types are often used to deliver parameters to COM objects or receive values ​​returned from COM objects. You can also write it back to the Variant type method, the function returns what type of input parameters depend on the possible (and often) method (for example, in the automation operation, depending on which method is called with you, which method can be returned (by one Parameters) A result containing a Variant type with Byte, Word, Float, Double, Date, BSTR, etc. (see the definition of the Variant structure on MSDN). In the following example, assuming type is a BSTR variant, That is to say, the value in the string is referenced by BSRTVAL. It has the advantage that in the ANSI application, there is a constructor to convert the value of the LPCWCHAR reference to a cString (see the BSTR-to-CString section). In Unicode mode It will become a standard CString constructor, see the default :: widechartomultibyte conversion warning, and you feel acceptable (most case, you will be satisfied) .variant Vadata; vadata = m_com.Yourmethodhere ();

Assert (VADATA.VT == Vt_BSTR);

CString strdata (vadata.bstrval);

You can also create a more versatile conversion routine according to the different VT domain. You may consider this for this:

CString VariantToString (Variant * VA)

{

CString S;

Switch (VA-> VT)

{/ * vt * /

Case VT_BSTR:

Return Cstring (Vadata-> BStriguration);

Case vt_bstr | vt_byref:

Return CString (* vadata-> pbstrv);

Case vt_i4:

S.Format (_T ("% d"), VA-> LVAL);

Return S;

Case vt_i4 | VT_BYREF:

S.Format (_T ("% d"), * va-> plval);

Case vt_r8:

S.Format (_T ("% f"), VA-> DBLVAL);

Return S;

... The remaining type conversion is completed by the reader himself

DEFAULT:

Assert (false); // unknown variant type (this assert isbotional)

Return CString ("");

} / * vt * /

}

2.3 BSTR, _BSTR_T and CCOMBSTR

CCOMBSTR, _BSTR_T is package to BSTR, BSTR is a 32-bit pointer to a string.

CHAR * Convert to BSTR can be like this:

BSTR B = _COM_UTIL :: ConvertStringTobstr ("Data"); // /// Use Neaver Package Comutil.h

Otherwise, use char * p = _com_util :: convertbstertostring (b);

2.4 (Cit) Variant, _variant_t and Colevariant

Variant's structure can refer to the definition of struct tagvariant in the header file VC98 / include / OAIDL.H.

For the assignment of Variant variables: First assign a value to the VT member, specify the data type, and then assign a value of the same data type in the federation, and take an example: Variant VA;

INT A = 2001;

va.vt = vt_i4; // indicates integer data

va.lval = a; // / assignment

For Variant that doesn't assign values, it is best to use Void Variantinit (Variantarg Far * Pvarg); in initialization, the essence is set to vt_empty, the following table, the following table, the following table, the following table, the corresponding relationship of VT and common data:

Unsigned char bval; vt_ui1

Short Ival; vt_i2

Long Lval; VT_i4

Float fltval; vt_r4

Double dblval; vt_r8

Variant_Bool BoolVal; VT_Bool

Scode scode; VT_ERROR

Cy Cyval; VT_CY

Date Date; VT_DATE

BSTR BSTRVAL; VT_BSTR

IUNKNOWN FAR * PUNKVAL; VT_UNKNOWN

Idispatch far * pdispval; vt_dispatch

SafeArray far * parray; vt_Array | *

Unsigned char far * pbval; vt_byref | vt_ui1

Short far * pival; vt_byref | vt_i2

Long far * plval; vt_byref | vt_i4

FLOAT FAR * PFLTVAL; VT_BYREF | VT_R4

Double Far * pdblval; vt_byref | vt_r8

Variant_Bool Far * PBOOLVAL; VT_BYREF | VT_BOOL

Scode Far * pscode; vt_byref | vt_error

CY FAR * PCYVAL; VT_BYREF | VT_CY

Date Far * pdate; vt_byref | vt_date

BSTR FAR * PBSTRVAL; VT_BYREF | VT_BSTR

IUNKNOWN FAR * FAR * PPUNKVAL; VT_BYREF | VT_UNKNOWN

Idispatch far * far * ppdispval; vt_byref | vt_dispatch

SafeArray Far * Far * PPARRAY; VT_Array | *

Variant Far * pvarval; vt_byref | VT_VARIANT

Void far * byref; VT_BYREF

_Variant_t is a Variant's package class whose assignment can use forced type conversion, and its constructor automatically processes these data types.

E.g:

Long L = 222;

ING I = 100;

_VARIANT_T LVAL (L);

LVAL = (long) i;

The use of Colevariant is basically the same as _variant_t, please refer to the following example:

Colevariant V3 = "String", V4 = (long) 1999;

CString str = (bstr) v3.pbstrval;

Long i = v4.lval;

转载请注明原文地址:https://www.9cbs.com/read-10607.html

New Post(0)