MFC

[MFC] 자료형 정리

성엽이 2014. 3. 1. 10:09

LPSTR : char *

LPCSTR : const char *

LPCTSTR : const char * 또는 const WCHAR *

 

 

WORD : 보통 2바이트의 부호없는 정수형   (*.word형을 string형으로 바꾸기 string := IntToStr(word); )

DWORD : 4바이트의 부호없는 정수형    DWORD unsigned long

그럼, WORD와, int의 차이점은...

WORD는 4글자 int는 3글자 
typedef unsigned short         WORD;    //WORD는 예약어가 아니죠  (typedef는 변수부분)

                                                                                               (cf.define은 전처리 부분)

 

*.단순참고

char, unsigned char, signed char : 1 byte
short, unsigned short : 2 bytes
int, unsigned int : 4 bytes
long, unsigned long : 4 bytes
float : 4 bytes
double : 8 bytes
long double : 10 bytes

=================================================================================

 

 

1. C 자료형
   char(1), short(2), int(4), long(4), float(4), double(8), bool
   문자: char

 


  
2. Win32 API 자료형
   BYTE(1, unsigned char), WORD(2, unsigned short), UINT(4, unsigned int)
   DWORD(4, unsigned long), LONG(4,long), BOOL
   문자: UCHAR(unsigned char)
   Handle: 대상을 구분하는 4바이트 정수(HWND,HDC...)
   
   MBCS문자(열)  유니코드문자(열)  자동매크로문자(열)
   ------------         -----------------        ------------------
   char   wchar_t    TCHAR
   LPSTR(char*)         LPWSTR(wchar_t*)         LPTSTR
   LPCSTR(const char*)  LPCWSTR(const wchar_t *) LPCTSTR
  
   .LPTSTR과 LPCTSTR를 사용하는 것이 좋음.
   .OLECHAR(wchar_t), LPOLESTR(LPWSTR), LPCOLESTR(LPCWSTR), OLESTR(x) = _T(x)

 

 

 

3. COM 스트링
   BSTR : 문자열 길이를 시작전에 저장하고, 이어서 유티코드문자열을 저장하는 방식
   LPCWSTR -> BSTR : 생성안됨. 생성함수를 이용해야 함.
                     BSTR bstr = sysAllocString(L"HELLO HI"); // 메모리 할당
                                 sysFreeString(bstr);         // 메모리 제거

 


   VARIANT: 문자열이 들어올때 BSTR과 동일

VARIANT 자료형은 각 개발 환경의 자료형의 차이를 해결하기위하여 제공되는 공용체 이다.

 

예제) INT nIndex 와 CString strItem이 있다고 가정하고...

 

// VARIANT 형 선언

VARIANT varIndex, varItem;

 

// VARIANT type 지정
varIndex.vt = VT_INT;
varItem.vt = VT_BSTR;

 

// VARIANT 값 할당.
varIndex.intVal = nIndex;
varItem.bstrVal = strItem.AllocSysString();// CString을 bstr로 변환하는 함수

 

// VARIANT 인자를 사용하여 함수 호출

 

 


 

4. CRT(c runtime library)지원 스트링 클래스 (#include "comdef.h")
   4-1. _bstr_t 
        :BSTR 랩퍼 클래스, 메모리 할당/제거를 자동으로 수행
       
        . LPCSTR, LPCWSTR  -> _bstr_t
          :_bstr bstr = "hello hi";
        . _bstr_t -> LPCSTR, LPCWSTR
          : LPCSTR psz1 = (LPCSTR)bs1;
        . _bstr_t -> BSTR 
          : 형변환 안됨. 함수이용
            BSTR bstr = bs1.copy();
            sysFreeString(bstr); // BSTR은 사용후 메모리 해제를 해야함.

   4-2. _variant_t
        :VARIANT 랩퍼 클래스, 메모리 할당/제거 자동 수행
        
        . LPCSTR, LPCWSTR -> _variant_t
          : _variant_t v1 = "hello hi";
        . _variant_t -> _bstr_t -> LPCSTR,LPCWSTR
          : LPCSTR psz1 = (LPCSTR)(_bstr_t)v1;

 

 

 

5. ATL 지원 스트링클래스
   5-1 CComBSTR : BSTR랩퍼클래스, 메모리할당/제거 자동 수행
      . LPCSTR, LPCWSTR -> CComBSTR
        CComBSTR bs1 = "hello hi";
      . CComBSTR -> BSTR -> LPCWSTR
        BSTR bs = (BSTR)bs1;
      . BSTR -> CComBSTR
        CComBSTR bs2; bs2.Attach(W2BSTR(L"hello hi");

   5-2 CComVariant: VARIANT랩퍼클래스, 메모리할당/제거 자동 수행
      . LPCSTR, LPCWSTR -> CComVariant
        CComVariant bs1 = "hello hi";
      . CComVariant -> CComBSTR -> BSTR -> LPCWSTR
        CComBSTR bs = bs1.bstrVal;

 

 

6. STL 스트링
   6-1 string
       . LPCSTR -> string
         string str = "hello hi";
       . string -> LPCSTR (형변환 안됨. 함수 이용)
         LPCSTR psz = str.c_str();
   6-2 wstring
       . LPCWSTR -> wstring
         wstring str = "hello hi";
       . wstring -> LPCWSTR
         LPCWSTR psz = str.c_str();

 

 

7. MFC 스트링
   . LPCSTR, LPCWSTR -> CString
     CString str = "hello hi";
   . CString -> LPCTSTR
     1. LPCTSTR lpsz = (LPCTSTR)str;
     2. LPTSTR lptsz = str.getBuffer(0), str.ReleaseBuffer(); (올바른 사용)
     3. LPTSTR lptsz = (LPTSTR)(LPCTSTR)str; (잘못된 표현)
     4. CString -> BSTR
        BSTR bstr = str.AllocSysString(); sysFreeString(bstr);

 

 

8. VC7 스트링
   String: .Net에서 새로 정의한 스트링 클래스
          String* s1 = S"hello hi";
          CString s2(s1);
    

9. ETC

   1. BSTR --> LPCSTR
      USES_CONVERSION;
      LPCSTR lpaszTemp = OLE2CA(bstrValue);

   2. LPCSTR --> BSTR
      USES_CONVERSION;
      BSTR bstrTemp = ::SysAllocString(A2COLE(lpaszValue));

   3. CString --> LPCSTR
      1) ANSI 버전
          LPCSTR lpaszTemp = (LPCSTR) strValue;
      2) UNICODE 버전
          USES_CONVERSION;
          LPCSTR lpaszTemp = T2CA((LPCTSTR) strValue);

   4. LPCSTR --> CString
      1) ANSI 버전
         CString strTemp = lpaszValue;
      2) UNICODE 버전
          USES_CONVERSION;
          CString strTemp = A2CT(lpaszValue);

 

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

 

Windows Data Types

The data types supported by Microsoft® Windows® are used to define function return values, function and message parameters, and structure members. They define the size and meaning of these elements.

The following table contains the following types: character, integer, Boolean, pointer, and handle. The character, integer, and Boolean types are common to most C compilers. Most of the pointer-type names begin with a prefix of P or LP. Handles refer to a resource that has been loaded into memory. For more information about handling 64-bit integers, see Large Integers.

Data Types

ValueMeaning
ATOMAtom. For more information, see Atoms.
BOOLBoolean variable (should be TRUE or FALSE).
BOOLEANBoolean variable (should be TRUE or FALSE).
BYTEByte (8 bits).
CALLBACKCalling convention for callback functions.
CHAR8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts.
COLORREFRed, green, blue (RGB) color value (32 bits). See COLORREF for information on this type.
CONSTVariable whose value is to remain constant during execution.
CRITICAL_SECTIONCritical-section object. For more information, see Critical Section Objects.
DWORD32-bit unsigned integer.
DWORD_PTRUnsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. (Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows. )
DWORD3232-bit unsigned integer.
DWORD6464-bit unsigned integer.
FLOATFloating-point variable.
HACCELHandle to an accelerator table.
HANDLEHandle to an object.
HBITMAPHandle to a bitmap .
HBRUSHHandle to a brush.
HCONVHandle to a dynamic data exchange (DDE) conversation.
HCONVLISTHandle to a DDE conversation list.
HCURSORHandle to a cursor.
HDCHandle to a device context (DC).
HDDEDATAHandle to DDE data.
HDESKHandle to a desktop.
HDROPHandle to an internal drop structure.
HDWPHandle to a deferred window position structure.
HENHMETAFILEHandle to an enhanced metafile.
HFILEHandle to a file opened by OpenFile, not CreateFile.
HFONTHandle to a font.
HGDIOBJHandle to a GDI object.
HGLOBALHandle to a global memory block.
HHOOKHandle to a hook.
HICONHandle to an icon.
HIMAGELISTHandle to an image list.
HIMCHandle to input context.
HINSTANCEHandle to an instance.
HKEYHandle to a registry key.
HKLInput locale identifier.
HLOCALHandle to a local memory block.
HMENUHandle to a menu.
HMETAFILEHandle to a metafile.
HMODULEHandle to a module. The value is the base address of the module.
HMONITORHandle to a display monitor.
HPALETTEHandle to a palette.
HPENHandle to a pen .
HRGNHandle to a region.
HRSRCHandle to a resource.
HSZHandle to a DDE string.
HWINSTAHandle to a window station.
HWNDHandle to a window.
INT32-bit signed integer.
INT_PTRSigned integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic.
INT3232-bit signed integer.
INT6464-bit signed integer.
LANGIDLanguage identifier. For more information, see Locales.
LCIDLocale identifier. For more information, see Locales.
LCTYPELocale information type. For a list, see Locale and Language Information.
LONG32-bit signed integer.
LONG_PTRSigned long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic.
LONG3232-bit signed integer.
LONG6464-bit signed integer.
LONGLONG64-bit signed integer.
LPARAMMessage parameter.
LPBOOLPointer to a BOOL.
LPBYTEPointer to a BYTE.
LPCOLORREFPointer to a COLORREF value.
LPCRITICAL_SECTIONPointer to a CRITICAL_SECTION.
LPCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.
LPCTSTRAn LPCWSTR if UNICODE is defined, an LPCTSTR otherwise.
LPCVOIDPointer to a constant of any type.
LPCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.
LPDWORDPointer to a DWORD.
LPHANDLEPointer to a HANDLE.
LPINTPointer to an INT.
LPLONGPointer to a LONG.
LPSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.
LPTSTRAn LPWSTR if UNICODE is defined, an LPSTR otherwise.
LPVOIDPointer to any type.
LPWORDPointer to a WORD.
LPWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.
LRESULTSigned result of message processing.
LUIDLocally unique identifier.
PBOOLPointer to a BOOL.
PBOOLEANPointer to a BOOL.
PBYTEPointer to a BYTE.
PCHARPointer to a CHAR.
PCRITICAL_SECTIONPointer to a CRITICAL_SECTION.
PCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.
PCTSTRPCWSTR if UNICODE is defined, a PCSTR otherwise.
PCWCHPointer to a constant WCHAR.
PCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts .
PDWORDPointer to a DWORD.
PFLOATPointer to a FLOAT.
PHANDLEPointer to a HANDLE.
PHKEYPointer to an HKEY.
PINTPointer to an INT.
PLCIDPointer to an LCID.
PLONGPointer to a LONG.
PLUIDPointer to a LUID.
POINTER_3232-bit pointer. On a 32-bit system, this is a native pointer. On a 64-bit system, this is a truncated 64-bit pointer.
POINTER_6464-bit pointer. On a 64-bit system, this is a native pointer. On a 32-bit system, this is a sign-extended 32-bit pointer.
PSHORTPointer to a SHORT.
PSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.
PTBYTEPointer to a TBYTE.
PTCHARPointer to a TCHAR.
PTSTRPWSTR if UNICODE is defined, a PSTR otherwise.
PTBYTEPointer to a TBYTE.
PTCHARPointer to a TCHAR.
PTSTRPWSTR if UNICODE is defined, a PSTR otherwise.
PUCHARPointer to a UCHAR.
PUINTPointer to a UINT.
PULONGPointer to a ULONG.
PUSHORTPointer to a USHORT.
PVOIDPointer to any type.
PWCHARPointer to a WCHAR.
PWORDPointer to a WORD.
PWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.
REGSAMSecurity access mask for registry key.
SC_HANDLEHandle to a service control manager database. For more information, see SCM Handles.
SC_LOCKHandle to a service control manager database lock. For more information, seeSCM Handles.
SERVICE_STATUS_HANDLEHandle to a service status value. For more information, see SCM Handles.
SHORTShort integer (16 bits).
SIZE_TThe maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer.
SSIZE_ TSigned SIZE_T.
TBYTEWCHAR if UNICODE is defined, a CHAR otherwise.
TCHARWCHAR if UNICODE is defined, a CHAR otherwise.
UCHARUnsigned CHAR.
UINTUnsigned INT.
UINT_PTRUnsigned INT_PTR.
UINT32Unsigned INT32.
UINT64Unsigned INT64.
ULONGUnsigned LONG.
ULONG_PTRUnsigned LONG_PTR.
ULONG32Unsigned LONG32.
ULONG64Unsigned LONG64.
ULONGLONG64-bit unsigned integer.
UNSIGNEDUnsigned attribute.
USHORTUnsigned SHORT.
VOIDAny type.
WCHAR16-bit Unicode character. For more information, see Character Sets Used By Fonts.
WINAPICalling convention for system functions.
WORD16-bit unsigned integer.
WPARAMMessage parameter.

<!-- Info Task Footer -->

Platform SDK Release: February 2003

 

 

*.

기존 데이터형으로 조합이 가능한 형을 새로운 데이터형으로 선언하여 사용하는 이유는 단순히 편하게 사용하자는 의도만 있는 것은 아니다. 윈도우즈가 항상 IBM 컴퓨터에서만 실행되라는 법은 없으며 언젠가는 다른 시스템으로 이식될 가능성도 있을 수 있는데 그럴 경우 소스 차원의 호환성을 확보하기 위해 이런 데이터형을 사용한다. 알기 쉽게 예를 든다면 WORD형이 현재는 2바이트의 부호없는 정수형이지만 팬티엄 이후 686이나 786쯤에는 4바이트로 확장될지도 모른다. 설사 그렇더라도 소스는 수정할 필요없이 헤더 파일에서 WORD형의 형 정의만 바꾸어 주고 소스를 다시 컴파일하면 호환성에 문제가 없다.


[출처] 자료형 정리(문자열)-all |작성자 으짜라고