標籤:c style tar a http ext
本文首頁連結: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下載檔案。