windows核心编程基础(1)——windows 中自定义的typedef

准备工作:安装 VS2017 和win10 sdk (安装VS2017的时候要勾选上)

1.windows.h 中关于int类型的自定义(具体是在windef.h 的minwindef.h中)

Data type Size Signed?
BYTE 8 bits Unsigned
DWORD 32 bits Unsigned
INT32 32 bits Signed
INT64 64 bits Signed
LONG 32 bits Signed
LONGLONG 64 bits Signed
UINT32 32 bits Unsigned
UINT64 64 bits Unsigned
ULONG 32 bits Unsigned
ULONGLONG 64 bits Unsigned
WORD 16 bits Unsigned
还有BOOL 类型 的宏定义(其实本质上也是int类型)

typedef   int    BOOL;

也就是说BOOL可以接收任何 int类型的变量,可以用0和非0来判断一个函数是否执行成功

#define FALSE 0

#define TRUE 1

(区别于C++中的 bool: true false 均小写 :只能接收 true 和false

2.windows.h 中关于指针类型的自定义

For example, LPRECT is a pointer to a RECT, where RECT is a structure that describes a rectangle.

下面三种是等价的:

RECT*  rect;  // Pointer to a RECT structure.
LPRECT rect;  // The same
PRECT  rect;  // Also the same.
注:LP称为长指针,又称为远指针  作用是访问当前segment外的内存,不过当初设计是为了 将16bit软件迁移到32bit windows上 现在没用了,三者一样

指针的精度 (本身的类型为 数值类型 只是为相应的指针提供精度 详见下面的源代码)

  • DWORD_PTR
  • INT_PTR
  • LONG_PTR
  • ULONG_PTR
  • UINT_PTR
取决于编译器的位数  如果在32bit 上 就是32bit 在64bit编译器上编译就是64bit

下面是源代码(64bit)

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

#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

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

typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;

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


3.重中之重 ————windows unicode 编码

Windows represents Unicode characters using UTF-16 encoding, in which each character is encoded as a 16-bit value. UTF-16 characters are called wide characters, to distinguish(区别于) them from 8-bit ANSI characters. The Visual C++ compiler supports the built-in data type ( Visual C++编译器内置数据类型wchar_t for wide characters.

The header file WinNT.h also defines the following typedef.

typedef wchar_t WCHAR;

定义一个宽字符类型的字符需要 在字符或者字符串 需要在前面加上一个L



wchar_t a = L'a';
wchar_t *str = L"hello";wchar_t a = L'a';wchar_t *str = L"hello";


Here are some other string-related typedefs that you will see:

Typedef Definition
CHAR char
PSTR or LPSTR char*
PCSTR or LPCSTR const char*
PWSTR or LPWSTR wchar_t*
PCWSTR or LPCWSTR const wchar_t*

window 提供的api 也是 分为 ANSI 版本 即 ascii码版本和 unicode 版本 (底层 Windows会将 ascii 转换为unicode编码

一些新的windows 的api 只有unicode版本 所以最好用unicode版本的api  )

对于定义了 UNICODE 宏的程序  自动调用 UNICODE版本

下面是 SetWindowText的宏定义example

#ifdef UNICODE
#define SetWindowText  SetWindowTextW
#else
#define SetWindowText  SetWindowTextA
#endif 

TEXT函数的问题——编译时选择版本问题 尽量不要用了 过时了 详见下面的文档


Back when applications needed to support both Windows NT as well as Windows 95, Windows 98, and Windows Me, it was useful to compile the same code for either ANSI or Unicode strings, depending on the target platform. To this end, the Windows SDK provides macros that map strings to Unicode or ANSI, depending on the platform.

Macro Unicode ANSI
TCHAR wchar_t char
TEXT("x") L"x" "x"

 

For example, the following code:

SetWindowText(TEXT("My Application"));

resolves to one of the following:

SetWindowTextW(L"My Application"); // Unicode function with wide-character string.

SetWindowTextA("My Application");  // ANSI function.

The TEXT and TCHAR macros are less useful today, because all applications should use Unicode. However, you might see them in older code and in some of the MSDN code examples.


_UNICODE 宏定义问题

 有一些c运行时头文件库需要这个宏定义 以便正确调用unicode版本的函数



The headers for the Microsoft C run-time libraries define a similar set of macros. For example, _tcslen resolves to strlen if _UNICODE is undefined; otherwise it resolves to wcslen, which is the wide-character version of strlen.

#ifdef _UNICODE
#define _tcslen     wcslen
#else
#define _tcslen     strlen
#endif 

Be careful: Some headers use the preprocessor symbol UNICODE, others use _UNICODE with an underscore prefix. Always define both symbols.Visual C++ sets them both by default when you create a new project.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值