Windows API中的坑

來源:互聯網
上載者:User

標籤: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下載檔案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.