PoEdu培训 Windows班 第三课 Windows数据类型
文章类别: 培训笔记 0 评论

PoEdu培训 Windows班 第三课 Windows数据类型

文章类别: 培训笔记 0 评论

Windows数据类型

Windows数据类型

微软将C语言的数据类型进行了单纯的包装, 形成了大量的自己的类型.
这些类型只基于Windows SDK中.
它是没有原生bool类型的.

Windows数据类型简介

常用类型

下面列举一些比较常见的数据类型, 看到了就得认识.

<span style="color:red">注: 如无特殊说明, 以下数据定义均取自VS2013版本</span>

BYTE

typedef unsigned char BYTE;

可见, BYTE其实是 8位无符号, 语义表示 1byte.

WORD

typedef unsigned short WORD;

可见, WORD其实是 16位无符号, 语义表示 一个字

DWORD

typedef unsigned long DWORD;

可见, DWORD其实就是 32位无符号, 语义表示 两个字

QWORD

VS2015以上才有的数据类型
typedef unsigned __int64 QWORD;

可见, QWORD其实就是 64位无符号, 语义表示 四个字

DWORD_PTR

//
// The INT_PTR is guaranteed to be the same size as a pointer.  Its
// size with change with pointer size (32/64).  It should be used
// anywhere that a pointer is cast to an integer type. UINT_PTR is
// the unsigned variation.
//
// __int3264 is intrinsic to 64b MIDL but not to old MIDL or to C compiler.
//
#if ( defined(__midl) && (501 < __midl) )

    typedef [public] __int3264 INT_PTR, *PINT_PTR;
    typedef [public] unsigned __int3264 UINT_PTR, *PUINT_PTR;

    typedef [public] __int3264 LONG_PTR, *PLONG_PTR;
    typedef [public] unsigned __int3264 ULONG_PTR, *PULONG_PTR;

#else  // midl64
// old midl and C++ compiler

#if defined(_WIN64)
    typedef __int64 INT_PTR, *PINT_PTR;
    typedef unsigned __int64 UINT_PTR, *PUINT_PTR;

    typedef __int64 LONG_PTR, *PLONG_PTR;
    typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;

    #define __int3264   __int64

#else
    typedef _W64 int INT_PTR, *PINT_PTR;
    typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;

    typedef _W64 long LONG_PTR, *PLONG_PTR;
    typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;

    #define __int3264   __int32

#endif
#endif // midl64

typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR

// 举例
int num = 10;
int* pNum = &num;
// 存放地址值
DWORD_PTR pNumber = &num;
// 偏移计算
pNumber += 1;
// 使用
*(int*)pNumber = 100;

Windows API返回的就是 DWORD_PTR

#include <Windows.h>

INT Foo(DOWRD_PTR num)
{
    *(PINT)num = 100;
    return *(PINT)num;
}

INT main()
{
    INT num = 10;
    Foo(&num);
    
    return 0;
}

DWORD32

typedef unsigned int DWORD32, *PDWORD32;

它就是一个32位的值, 不随平台而改变

DWORD64

typedef unsigned __int64 DWORD64, *PDWORD64;

它就是一个64位的值, 不随平台而改变

DWORDLONG

typedef unsigned __int64 ULONGLONG;
typedef ULONGLONG DWORDLONG;

它就是一个64位的值, 不随平台而改变

LPWSTR

typedef wchar_t WCHAR;
typedef Null_terminated WCHAR NWPSTR, LPWSTR, *PWSTR;

LONG POINTER WIDE STRING
就是一个 宽字节字符串指针

LPSTR

typedef char CHAR;
typedef Null_terminated CHAR NPSTR, LPSTR, *PSTR;

LONG POINTER (ANSI) STRING
就是一个 窄字节字符串指针

LPCWSTR

typedef Null_terminated CONST WCHAR LPCWSTR, PCWSTR;

LONG POINTER CONST WIDE STRING
就是一个 常量宽字节字符串指针

LPCSTR

typedef Null_terminated CONST CHAR LPCSTR, PCSTR;

LONG POINTER CONST (ANSI) STRING
就是一个 常量窄字节字符串指针

error_status_t

FLOAT

typedef float FLOAT

HANDLE

typedef void* HANDLE

TCHAR

HCALL

typedef DWORD HCALL

HRESULT

INT

typedef int INT;

INT8

typedef char INT8

INT16

typedef short INT16

INT32

typedef int INT32

INT64

typedef __int64 INT64

LMCSTR

typedef const wchar_t* LMCSTR;

VS2015以上版本才有

LMSTR

typedef WCHAR* LMSTR;

VS2015以上版本才有

LONG

typedef long LONG;

LONGLONG

typedef __int64 LONGLONG;

LONG_PTR

typedef __int3264 LONG_PTR;

LONG32

typedef int LONG32;

LONG64

typedef __int64 LONG64;

LPVOID

typedef const void* LPVOID;

SHORT

typedef short SHORT;

SIZE_T

typedef ULONG_PTR SIZE_T;

不同于 C++中的size_t类型, 需要注意

UCHAR

typedef unsigned char UCHAR;

STRING

typedef UCHAR* STRING;

UINT

typedef unsigned int UINT;

UINT8

typedef unsigned char UINT8;

UINT16

typedef unsigned short UINT16;

UINT32

typedef unsigned int UINT32;

UINT64

typedef unsigned __int64 UINT64;

ULONG

typedef unsigned long ULONG;

ULONG_PTR

typedef unsigned __int3264 ULONG_PTR;

ULONG32

typedef unsigned int ULONG32;

ULONG64

typedef unsigned __int64 ULONG64;

ULONGLONG

typedef unsigned __int64 ULONGLONG;

UNICODE

typedef wchar_t UNICODE;

已经被废弃

USHORT

typedef unsigned short USHORT;

VOID

typedef void VOID;

更多类型

类型太多, 再此不在一一列出. 如果想对某种类型进行查看, 有以下几种方式:

  1. 通过Visual Studio的转到定义功能
  2. 查询MSDN官方文档, 所以数据类型地址在 点击此处

    • 通过左侧导航栏可以快读定位
  3. 下载微软提供的PDF文档, 在这里下载

字符编码

UTF-8

最小单位就是8bit 可变长, 最少8bit, 多了可以在占用

UTF-16

UTF-32

最小单位就是32bit 可变长, 最少32bit, 多了可以在占用

他们之间都可以进行相互转换

// 示例
#include <Windows.h>
#include <iostream>

int main()
{
    WCHAR ch = L'H';
    LPWSTR str = L"Hades";

    LPCSTR str0 = "花心胡萝卜Hades";
    std::cout << strlen(str0) << "   " << str0[0] << std::endl;
    LPCWSTR str1 = L"花心胡萝卜Hades";
    std::cout << wcslen(str1) << "   " << str1[0] << std::endl;

    return 0;
}

Alt 运行结果

未完待续...

如有错误,请提出指正!谢谢.

回复