Windows API中的坑

本文主页链接:Windows API中的坑

ExpandEnvironmentStrings

风险:
进程会继承其父进程的环境变量。在展开如%APPDATA%等文件夹时,有可能父进程对此环境变量进行过改动。那么可能你获取的就不是你想要的当前SESSION的%APPDATA%了。
建议:
使用SHGetFolderPath系列函数来做这件事。

GetModuleFileName

风险:
在DLL中调用时。若传入的instance參数为NULL,那获取的将是载入DLL的进程的EXE的路径。若须要获取DLL的路径。传入的instance參数需为DLL的hModule。

ShellExecuteEx

风险:
调用API之后,若初始MASK设置不对,SHELLEXECUTEINFO结构体里的hProcess可能为空。
建议: 若确定要使用hProcess,则在调用ShellExecuteEx前确认SHELLEXECUTEINFO结构体的fMask成员设置为SEE_MASK_NOCLOSEPROCESS。并且MSDN上对hProcess成员的凝视例如以下:

A handle to the newly started application. This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.
Note ShellExecuteEx does not always return an hProcess, even if a process is launched as the result of the call. For example, an hProcess does not return when you use SEE_MASK_INVOKEIDLIST to invoke IContextMenu.

UrlDownloadToFile

风险1:
使用UrlDownloadToFile下载文件,若文件内容经过gzip压缩,即返回header包含Content-Encoding: gzip。若调用线程没有初始化COM。那UrlDownloadToFile会失败,由于urlmon不能正确处理压缩后的数据包。


建议:
调用此函数前需确保该线程已经调用CoInitialize。

风险2:
使用UrlDownloadToFile下载文件前它会自己主动先在本地缓存中查找此文件。所以可能终于得到的不是Server上的最新内容。


建议:
能够为URL加入随机參数以防止缓存,也能够使用DeleteUrlCacheEntry清理缓存后再使用UrlDownloadToFile下载文件。

原文地址:https://www.cnblogs.com/slgkaifa/p/6756299.html