Windows数据类型
Windows数据类型
微软将C语言的数据类型进行了单纯的包装, 形成了大量的自己的类型.
这些类型只基于Windows SDK中.
它是没有原生bool类型的.
Windows数据类型简介
按照规范, 所有的Windows类型都应在
WinDef.h中找到- 可是, 微软经过这么多年的发展, 数据类型定义已经略乱, 只能借助Visual Studio的
转到定义功能进行查看 - 这些类型是
Windows进行定义的, 也就是说, 它们不是C标准通用的 - 很多数据类型都是一样的, 只不过换了一种写法, 为了使语义更加通顺.
- 可是, 微软经过这么多年的发展, 数据类型定义已经略乱, 只能借助Visual Studio的
常用类型
下面列举一些比较常见的数据类型, 看到了就得认识.
<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 // midl64typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR
- 它本意是一个
32bit的地址值, 用来存放32bit地址值的类型 但是在
64位操作系统中, 它也能存放 64位的地址值- 这就是
向上兼容
- 这就是
- 它的语义,
就是代表的地址的值
// 举例
int num = 10;
int* pNum = #
// 存放地址值
DWORD_PTR pNumber = #
// 偏移计算
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
- 错误状态类型
- 本质是 unsigned long
微软的错误返回机制是千变万化的
- 有的需要这个类型
- 有的是BOOL
- 有的需要通过GetLastError
FLOAT
typedef float FLOAT
HANDLE
typedef void* HANDLE
TCHAR
可变的类型, 与工程设置有关
- 工程为 UNICODE(宽字节), TCHAR 则是 wchar_t
- 工程为 ANSI(多字节), TCHAR 则是 char
HCALL
typedef DWORD HCALL
HRESULT
- 用来表示返回值
typedef LONG 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;
更多类型
类型太多, 再此不在一一列出. 如果想对某种类型进行查看, 有以下几种方式:
字符编码
UTF-8
最小单位就是8bit 可变长, 最少8bit, 多了可以在占用
UTF-16
- Windows用的就是UTF-16(从VISTA开始) --> 也就是Unicode
- 最小单位就是16bit 可变长, 最少16bit, 多了可以在占用
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;
}
未完待续...
如有错误,请提出指正!谢谢.
本文由 花心胡萝卜 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2017-05-10 at 03:48 am