IntersectRect、wcsrchr、CComPtr、GetFileAttributes

IntersectRect    两矩形相交形成的新矩形

The IntersectRect function calculates the intersection of two source rectangles and places the coordinates of the intersection rectangle into the destination rectangle. If the source rectangles do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the destination rectangle.

BOOL IntersectRect(
  LPRECT lprcDst,        // intersection buffer
  CONST RECT*lprcSrc1,  // first rectangle
  CONST RECT*lprcSrc2   // second rectangle
);
 

Parameters

lprcDst
[out] Pointer to the RECT structure that is to receive the intersection of the rectangles pointed to by the lprcSrc1 and lprcSrc2 parameters. This parameter cannot be NULL.
lprcSrc1
[in] Pointer to the RECT structure that contains the first source rectangle.
lprcSrc2
[in] Pointer to the RECT structure that contains the second source rectangle.

Return Values

If the rectangles intersect, the return value is nonzero.

If the rectangles do not intersect, the return value is zero.

Windows NT/2000/XP: To get extended error information, call GetLastError.

Remarks

Because applications can use rectangles for different purposes, the rectangle functions do not use an explicit unit of measure. Instead, all rectangle coordinates and dimensions are given in signed, logical values. The mapping mode and the function in which the rectangle is used determine the units of measure.  

wcsrchr 获取一个字符在字符串中最后一次出现的位置

Scan a string for the last occurrence of a character.

 
char *strrchr(
   const char *str,
   int c 
); // C only
char *strrchr(
   char *str,
   int c 
); // C++ only
const char *strrchr(
   const char *str,
   int c 
); // C++ only
wchar_t *wcsrchr(
   const wchar_t *str,
   wchar_t c 
); // C only
wchar_t *wcsrchr(
   wchar_t *str,
   wchar_t c 
); // C++ only
const wchar_t *wcsrchr(
   const wchar_t *str,
   wchar_t c 
); // C++ only
unsigned char *_mbsrchr(
   const unsigned char *str,
   unsigned int c 
); // C only
unsigned char *_mbsrchr(
   unsigned char *str,
   unsigned int c 
); // C++ only
const unsigned char *_mbsrchr(
   const unsigned char *str,
   unsigned int c 
); // C++ only
unsigned char *_mbsrchr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C only
unsigned char *_mbsrchr_l(
   unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only
const unsigned char *_mbsrchr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only

Parameters

str

Null-terminated string to search.

c

Character to be located.

locale

Locale to use.

Return Value

Returns a pointer to the last occurrence of c in str, or NULL if c is not found.

CComPtr用法

COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃。所以使用COM接口,必须小心翼翼才行。
但是,即使所有的代码中,都正确的AddRef和Release,也不一定能保证万无一失,例如:
void SomeApp( IHello * pHello )
{
IHello* pCopy = pHello;
pCopy->AddRef(); 
OtherApp();
pCopy->Hello();
pCopy->Release();
}
看起来好像无懈可击,但是假设OtherApp中抛出了异常,那么pCopy->Release不就被跳过去了吗?
幸好,所有的问题都从简单到复杂,再从复杂到简单的,因为我们有CComPtr!

CComPtr被称为智能指针,是ATL提供的一个模版类,能够从语法上自动完成AddRef和Release。(源代码在atlbase.h中)
CComPtr的用法很简单,以IHello*为例,将程序中所有接口指针类型(除了参数),都使用CComPtr<IHello> 代替即可。即程序中除了参数之外,再也不要使用IHello*,全部以CComPtr<IHello>代替。
CComPtr的用法和普通COM指针几乎一样,另外使用中有以下几点需要注意。
1. CComPtr已经保证了AddRef和Release的正确调用,所以不需要,也不能够再调用AddRef和Release。
2. 如果要释放一个智能指针,直接给它赋NULL值即可。(这一点要牢记曾因为没有设置为null而出错)
3. CComPtr本身析构的时候会释放COM指针。
4. 当对CComPtr使用&运算符(取指针地址)的时候,要确保CComPtr为NUL。(因为通过CComPtr的地址对CComPtr赋值时,不会自动调用AddRef,若不为NULL,则前面的指针不能释放,CComPtr会使用assert报警)
以刚才的程序为例:
void SomeApp( IHello * pHello )
{
CComPtr<IHello> pCopy = pHello;
OtherApp();
pCopy->Hello();
}
由于pCopy是一个局部的对象,所以即使OtherApp()抛出异常,pCopy也会被析构,指针能够被释放。
如果不想在程序临近发布前,还因为COM指针的引用计数造成崩溃的话,就牢记这一点吧:程序中除了参数之外,不要直接使用COM指针类型,一定要全部以CComPtr<IXXX>代替

DWORD GetFileAttributes(LPCTSTR lpFilename); 为一个指定的文件或目录返回文件系统的属性

参数:

    hFileName: 输入参数,为需要获取属性的文件或目录

返回值:

    返回DWORD值,表示文件属性。如果返回INVALID_FILE_ATTRIBUTES,则表示失败,可使用GetLastError函数获取错误信息

使用说明:
    要判断文件属性,需要使用“&”与属性常量进行运算,如果运行结果为真,则表示具有这种属性

注意:INVALID_FILE_ATTRIBUTES 实为DWORD(-1). 因此在判断文件属性之前,首先要确保函数返回值不是INVALID_FILE_ATTRIBUTES,因为和INVALID_FILE_ATTRIBUTES 做 ‘&’ 操作,结果总是非零,从而带来误判。

原文地址:https://www.cnblogs.com/lisuyun/p/3812271.html