小弟在学习VC++的过程中 遇到一个很棘手的问题 就是VC++里面的那些数据类型 很多都理不清 还有一些都不知所云 恳请高手指教 希望大家能帮我总结一下 先谢谢各位了!

解决方案 »

  1.   


    Platform SDK: Windows API 
    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. Type Definition 
    ATOM Atom. For more information, see Atoms. 
    BOOL Boolean variable (should be TRUE or FALSE). 
    BOOLEAN Boolean variable (should be TRUE or FALSE). 
    BYTE Byte (8 bits). 
    CALLBACK Calling convention for callback functions. 
    CHAR 8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts. 
    COLORREF Red, green, blue (RGB) color value (32 bits). See COLORREF for information on this type. 
    CONST Variable whose value is to remain constant during execution. 
    CRITICAL_SECTION Critical-section object. For more information, see Critical Section Objects. 
    DWORD 32-bit unsigned integer.  
    DWORD_PTR Unsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. 
    DWORD32 32-bit unsigned integer. 
    DWORD64 64-bit unsigned integer. 
    FLOAT Floating-point variable. 
    HACCEL Handle to an accelerator table. 
    HANDLE Handle to an object. 
    HBITMAP Handle to a bitmap. 
    HBRUSH Handle to a brush. 
    HCONV Handle to a dynamic data exchange (DDE) conversation. 
    HCONVLIST Handle to a DDE conversation list. 
    HCURSOR Handle to a cursor. 
    HDC Handle to a device context (DC). 
    HDDEDATA Handle to DDE data. 
    HDESK Handle to a desktop. 
    HDROP Handle to an internal drop structure. 
    HDWP Handle to a deferred window position structure. 
    HENHMETAFILE Handle to an enhanced metafile. 
    HFILE Handle to a file opened by OpenFile, not CreateFile. 
    HFONT Handle to a font. 
    HGDIOBJ Handle to a GDI object. 
    HGLOBAL Handle to a global memory block. 
    HHOOK Handle to a hook. 
    HICON Handle to an icon. 
    HIMAGELIST Handle to an image list. 
    HIMC Handle to input context. 
    HINSTANCE Handle to an instance. 
    HKEY Handle to a registry key. 
    HKL Input locale identifier. 
    HLOCAL Handle to a local memory block. 
    HMENU Handle to a menu. 
    HMETAFILE Handle to a metafile. 
    HMODULE Handle to a module. 
    HMONITOR Handle to a display monitor. 
    HPALETTE Handle to a palette. 
    HPEN Handle to a pen. 
    HRGN Handle to a region. 
    HRSRC Handle to a resource. 
    HSZ Handle to a DDE string. 
    HWINSTA Handle to a window station. 
    HWND Handle to a window. 
    INT 32-bit signed integer. 
    INT_PTR Signed integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic. 
    INT32 32-bit signed integer.  
    INT64 64-bit signed integer.  
    LANGID Language identifier. For more information, see Locales. 
    LCID Locale identifier. For more information, see Locales. 
    LCTYPE Locale information type. For a list, see Locale and Language Information 
    LONG 32-bit signed integer. 
    LONG_PTR Signed long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic. 
    LONG32 32-bit signed integer. 
    LONG64 64-bit signed integer. 
    LONGLONG 64-bit signed integer. 
    LPARAM Message parameter. 
    LPBOOL Pointer to a BOOL. 
    LPBYTE Pointer to a BYTE. 
    LPCOLORREF Pointer to a COLORREF value. 
    LPCRITICAL_SECTION Pointer to a CRITICAL_SECTION. 
    LPCSTR Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. 
    LPCTSTR An LPCWSTR if UNICODE is defined, an LPCSTR otherwise. 
    LPCVOID Pointer to a constant of any type. 
    LPCWSTR Pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. 
    LPDWORD Pointer to a DWORD. 
    LPHANDLE Pointer to a HANDLE. 
    LPINT Pointer to an INT. 
    LPLONG Pointer to a LONG. 
    LPSTR Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. 
    LPTSTR An LPWSTR if UNICODE is defined, an LPSTR otherwise. 
    LPVOID Pointer to any type. 
    LPWORD Pointer to a WORD. 
    LPWSTR Pointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. 
    LRESULT Signed result of message processing. 
    LUID Locally unique identifier. 
    PBOOL Pointer to a BOOL. 
    PBOOLEAN Pointer to a BOOL. 
    PBYTE Pointer to a BYTE. 
    PCHAR Pointer to a CHAR. 
    PCRITICAL_SECTION Pointer to a CRITICAL_SECTION. 
    PCSTR Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. 
    PCTSTR A PCWSTR if UNICODE is defined, a PCSTR otherwise. 
    PCWCH Pointer to a constant WCHAR. 
    PCWSTR Pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. 
    PDWORD Pointer to a DWORD. 
    PFLOAT Pointer to a FLOAT. 
    PHANDLE Pointer to a HANDLE. 
    PHKEY Pointer to an HKEY. 
    PINT Pointer to an INT. 
    PLCID Pointer to an LCID. 
    PLONG Pointer to a LONG. 
    PLUID Pointer to a LUID. 
    POINTER_32 32-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_64 64-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. 
    PSHORT Pointer to a SHORT. 
    PSTR Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. 
    PTBYTE Pointer to a TBYTE. 
    PTCHAR Pointer to a TCHAR. 
    PTSTR A PWSTR if UNICODE is defined, a PSTR otherwise. 
    PUCHAR Pointer to a UCHAR. 
    PUINT Pointer to a UINT. 
    PULONG Pointer to a ULONG. 
    PUSHORT Pointer to a USHORT. 
    PVOID Pointer to any type. 
    PWCHAR Pointer to a WCHAR. 
    PWORD Pointer to a WORD. 
    PWSTR Pointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. 
    REGSAM Security access mask for registry key. 
    SC_HANDLE Handle to a service control manager database. For more information, see SCM Handles. 
    SC_LOCK Handle to a service control manager database lock. For more information, see SCM Handles. 
    SERVICE_STATUS_HANDLE Handle to a service status value. For more information, see SCM Handles. 
    SHORT Short integer (16 bits). 
    SIZE_T The maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer.  
    SSIZE_T Signed SIZE_T. 
    TBYTE A WCHAR if UNICODE is defined, a CHAR otherwise. 
    TCHAR A WCHAR if UNICODE is defined, a CHAR otherwise. 
    UCHAR Unsigned CHAR. 
    UINT Unsigned INT. 
    UINT_PTR Unsigned INT_PTR. 
    UINT32 Unsigned INT32. 
    UINT64 Unsigned INT64. 
    ULONG Unsigned LONG. 
    ULONG_PTR Unsigned LONG_PTR. 
    ULONG32 Unsigned LONG32. 
    ULONG64 Unsigned LONG64. 
    ULONGLONG 64-bit unsigned integer. 
    UNSIGNED Unsigned attribute. 
    USHORT Unsigned SHORT. 
    VOID Any type. 
    WCHAR 16-bit Unicode character. For more information, see Character Sets Used By Fonts. 
    WINAPI Calling convention for system functions. 
    WORD 16-bit unsigned integer. 
    WPARAM Message parameter. Platform SDK Release: August 2001  What did you think of this topic?
    Let us know.  Order a Platform SDK CD Online
    (U.S/Canada)   (International)  
      

  2.   

    转载:常用数据类型使用转换详解
    作者:程佩君读者层次:初学刚接触VC编程的朋友往往对许多数据类型的转换感到迷惑不解,本文将介绍一些常用数据类型的使用。我们先定义一些常见类型变量借以说明int i = 100;
    long l = 2001;
    float f=300.2;
    double d=12345.119;
    char username[]="程佩君";
    char temp[200];
    char *buf;
    CString str;
    _variant_t v1;
    _bstr_t v2;一、其它数据类型转换为字符串
    短整型(int)
    itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制
    itoa(i,temp,2); ///按二进制方式转换 
    长整型(long)
    ltoa(l,temp,10); 
    浮点数(float,double)
    用fcvt可以完成转换,这是MSDN中的例子:
    int decimal, sign; 
    char *buffer; 
    double source = 3.1415926535; 
    buffer = _fcvt( source, 7, &decimal, &sign ); 
    运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
    decimal表示小数点的位置,sign表示符号:0为正数,1为负数 
    CString变量
    str = "2008北京奥运";
    buf = (LPSTR)(LPCTSTR)str; 
    BSTR变量
    BSTR bstrValue = ::SysAllocString(L"程序员"); 
    char * buf = _com_util::ConvertBSTRToString(bstrValue); 
    SysFreeString(bstrValue); 
    AfxMessageBox(buf); 
    delete(buf); 
    CComBSTR变量
    CComBSTR bstrVar("test"); 
    char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str); 
    AfxMessageBox(buf); 
    delete(buf); _bstr_t变量
    _bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用
    _bstr_t bstrVar("test"); 
    const char *buf = bstrVar;///不要修改buf中的内容 
    AfxMessageBox(buf); 
    通用方法(针对非COM数据类型)
    用sprintf完成转换
    char  buffer[200];
    char  c = '1';
    int   i = 35;
    long  j = 1000;
    float f = 1.7320534f;
    sprintf( buffer, "%c",c);
    sprintf( buffer, "%d",i);
    sprintf( buffer, "%d",j);
    sprintf( buffer, "%f",f);二、字符串转换为其它数据类型
    strcpy(temp,"123"); 短整型(int)
    i = atoi(temp); 
    长整型(long)
    l = atol(temp); 
    浮点(double)
    d = atof(temp); 
    CString变量
    CString name = temp; 
    BSTR变量 
    BSTR bstrValue = ::SysAllocString(L"程序员"); 
    ...///完成对bstrValue的使用
    SysFreeString(bstrValue); CComBSTR变量
    CComBSTR类型变量可以直接赋值
    CComBSTR bstrVar1("test");
    CComBSTR bstrVar2(temp);_bstr_t变量
    _bstr_t类型的变量可以直接赋值
    _bstr_t bstrVar1("test"); 
    _bstr_t bstrVar2(temp); 
    三、其它数据类型转换到CString
    使用CString的成员函数Format来转换,例如:
    整数(int)
    str.Format("%d",i); 
    浮点数(float)
    str.Format("%f",i); 
    字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值
    str = username; 
    对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。四、BSTR、_bstr_t与CComBSTR
    CComBSTR 是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。
    char *转换到BSTR可以这样: 
    BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib
    SysFreeString(bstrValue); 
    反之可以使用
    char *p=_com_util::ConvertBSTRToString(b);
    delete p;
    具体可以参考一,二段落里的具体说明。CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,==等操作,所以使用非常方便。
    特别是_bstr_t,建议大家使用它。
    五、VARIANT 、_variant_t 与 COleVariant
    VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义。
    对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子:
    VARIANT va;
    int a=2001;
    va.vt=VT_I4;///指明整型数据
    va.lVal=a; ///赋值对于不马上赋值的VARIANT,最好先用Void VariantInit(VARIANTARG FAR* pvarg);进行初始化,其本质是将vt设置为VT_EMPTY,下表我们列举vt与常用数据的对应关系:Byte 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. 
    DECIMAL FAR* pdecVal  // VT_BYREF|VT_DECIMAL. 
    IUnknown FAR* punkVal;  // VT_UNKNOWN. 
    IDispatch FAR* pdispVal;  // VT_DISPATCH. 
    SAFEARRAY FAR* parray;  // VT_ARRAY|*. 
    Byte 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;  // Generic ByRef. 
    char cVal;  // VT_I1. 
    unsigned short uiVal;  // VT_UI2. 
    unsigned long ulVal;  // VT_UI4. 
    int intVal;  // VT_INT. 
    unsigned int uintVal;  // VT_UINT. 
    char FAR * pcVal;  // VT_BYREF|VT_I1. 
    unsigned short FAR * puiVal;  // VT_BYREF|VT_UI2. 
    unsigned long FAR * pulVal;  // VT_BYREF|VT_UI4. 
    int FAR * pintVal;  // VT_BYREF|VT_INT. 
    unsigned int FAR * puintVal;  //VT_BYREF|VT_UINT. 
    _variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。
    使用时需加上#include <comdef.h>
    例如:
    long l=222;
    ing i=100;
    _variant_t lVal(l);
    lVal = (long)i;
    COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:
    COleVariant v3 = "字符串", v4 = (long)1999;
    CString str =(BSTR)v3.pbstrVal;
    long i = v4.lVal;
    六、其它一些COM数据类型根据ProgID得到CLSID
    HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID,LPCLSID pclsid);
    CLSID clsid;
    CLSIDFromProgID( L"MAPI.Folder",&clsid);根据CLSID得到ProgID
    WINOLEAPI ProgIDFromCLSID( REFCLSID clsid,LPOLESTR * lplpszProgID); 
    例如我们已经定义了 CLSID_IApplication,下面的代码得到ProgID
    LPOLESTR pProgID = 0;
    ProgIDFromCLSID( CLSID_IApplication,&pProgID);
    ...///可以使用pProgID 
    CoTaskMemFree(pProgID);//不要忘记释放 七、ANSI与Unicode
    Unicode称为宽字符型字串,COM里使用的都是Unicode字符串。将ANSI转换到Unicode
    (1)通过L这个宏来实现,例如: CLSIDFromProgID( L"MAPI.Folder",&clsid);
    (2)通过MultiByteToWideChar函数实现转换,例如:
    char *szProgID = "MAPI.Folder";
    WCHAR szWideProgID[128];
    CLSID clsid;
    long lLen = MultiByteToWideChar(CP_ACP,0,szProgID,strlen(szProgID),szWideProgID,sizeof(szWideProgID));
    szWideProgID[lLen] = '\0'; 
    (3)通过A2W宏来实现,例如: 
    USES_CONVERSION; 
    CLSIDFromProgID( A2W(szProgID),&clsid); 
    将Unicode转换到ANSI
    (1)使用WideCharToMultiByte,例如:
    // 假设已经有了一个Unicode 串 wszSomeString... 
    char szANSIString [MAX_PATH]; 
    WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK, wszSomeString, -1, szANSIString, sizeof(szANSIString), NULL, NULL ); 
    (2)使用W2A宏来实现,例如:
    USES_CONVERSION;
    pTemp=W2A(wszSomeString); 
    八、其它对消息的处理中我们经常需要将WPARAM或LPARAM等32位数据(DWORD)分解成两个16位数据(WORD),例如:
    LPARAM lParam;
    WORD loValue = LOWORD(lParam);///取低16位
    WORD hiValue = HIWORD(lParam);///取高16位
    对于16位的数据(WORD)我们可以用同样的方法分解成高低两个8位数据(BYTE),例如:
    WORD wValue;
    BYTE loValue = LOBYTE(wValue);///取低8位
    BYTE hiValue = HIBYTE(wValue);///取高8位
    两个16位数据(WORD)合成32位数据(DWORD,LRESULT,LPARAM,或WPARAM)
    LONG MAKELONG( WORD wLow, WORD wHigh );
    WPARAM MAKEWPARAM( WORD wLow, WORD wHigh ); 
    LPARAM MAKELPARAM( WORD wLow, WORD wHigh );
    LRESULT MAKELRESULT( WORD wLow, WORD wHigh ); 
    两个8位的数据(BYTE)合成16位的数据(WORD)
    WORD MAKEWORD( BYTE bLow, BYTE bHigh ); 
    从R(red),G(green),B(blue)三色得到COLORREF类型的颜色值
    COLORREF RGB( BYTE byRed,BYTE byGreen,BYTE byBlue );
    例如COLORREF bkcolor = RGB(0x22,0x98,0x34);
    从COLORREF类型的颜色值得到RGB三个颜色值
    BYTE Red = GetRValue(bkcolor); ///得到红颜色
    BYTE Green = GetGValue(bkcolor); ///得到绿颜色
    BYTE Blue = GetBValue(bkcolor); ///得到兰颜色九、注意事项
    假如需要使用到ConvertBSTRToString此类函数,需要加上头文件comutil.h,并在setting中加入comsupp.lib或者直接加上#pragma comment( lib, "comsupp.lib" )后记:本文匆匆写成,错误之处在所难免,欢迎指正.
      

  3.   

    http://www.csdn.net/Develop/Read_Article.asp?id=12365
    http://www.vckbase.com/study/article/data_convert.htm
      

  4.   

    谢谢各位支持,在此非常感谢 kingcom_xu(杀不了人的刀,郁闷!) & loopyifly(深情咸鱼) & YP2002CN(老婆我不敢了,老婆我愛你)给我解惑总结!开始散分了~~