note 1:
一個線程可以調用以下兩個函數來擷取和設定其程序的目前驅動器和目錄:
dword getcurrentdirectory(
dword cchcurdir,
ptstr pszcurdir);
bool setcurrentdirectory(pctstr pszcurdir);
注意
windef.h檔案中被定義為260的常量max_path是目錄名稱或檔案名稱的最大字元數.是以在調用getcurrentdirectory的時候,向該函數傳遞由max_path個tchar類型的元素構成的一個緩沖區是非常安全的.
note 2:
系統跟蹤記錄着程序的目前驅動器和目錄,但它沒有記錄每個驅動器的目前目錄.不過,利用作業系統提供的支援,可以處理多個驅動器的目前目錄.這個支援是通過程序的環境字元串來提供的.
可以使用c運作庫函數_chdir而不是windowssetcurrentdirectory函數來更改目前目錄._chdir函數在内部調用setcurrentdirectory,但_chdir還可以調用setenvironmentvariable來添加或修改環境變量,進而使不同驅動器的目前目錄得以保留.
note 3:
可以通過調用getfullpathname來獲得程序的目前目錄:
dword getfullpathname(
pctstr pszfile,
dword cchpath,
ptstr pszpath,
ptstr *ppszfilepart);
例如,要想獲得c驅動器的目前目錄,可以像下面這樣調用getfullpathname:
tchar szcurdir[max_path];
dword cchlength = getfullpathname(text("c:"), max_path, szcurdir, null);
note 4:
createprocess試圖修改字元串時,會引起一個通路沖突(microsoftc/c++編譯器的早期版本把字元串放在可讀/寫記憶體中。是以對createprocess函數的調用不會引起通路沖突).
解決這個問題的最佳方式是:在調用createprocess之前.把常量字元串複制到一個臨時緩沖區.如下所示:
startupinfo si = { sizeof(si) };
process_information pi;
tchar szcommandline[] = text("notepad");
createprocess(null, szcommandline, null, null,
false, 0, null, null, &si, &pi);
還可以使用microsoft c++的/gf和/gf編譯器開關.它們可以消除重複的字元串,以及判斷那些字元串是否放在一個隻讀區域.(還要注意/zi開關,它允許使用visual studio的edit & continue調試功能,它包含了/gf開關的功能.)最佳做法是使用/gf編譯器開關和一個臨時緩沖區.
note 5:
應用程式運作期間,必須關閉到子程序及其主線程的句柄,以避免資源洩漏.當然,系統會在你的程序終止後自動清理這種洩漏.但是,如果是一個編寫精妙的軟體,肯定會在程序不再需要通路一個子程序及其主線程的時候,顯式地調用closehandle來關閉這些句柄.忘記關閉這些句柄是開發人員最容易犯的錯誤之一.
不知道為什麼,許多開發人員都有這樣的一個誤解:關閉到一個程序或線程的句柄,會強迫系統殺死此程序或線程.但這是大謬不然的.關閉句柄隻是告訴系統你對程序或線程的統計資料不再感興趣了.程序或線程會繼續執行,直至自行終止.
note 6:
通過調用getexitcodeprocess來獲得已經死亡的一個程序的退出代碼:
bool getexitcodeprocess(
handle hprocess,
pdword pdwexitcode);
note 7:
windows提供了幾種方式在不同程序之間傳遞資料,其中包括動态資料交換(dynamic data exchange,dde),ole,pipes,mailslots等等.共享資料最友善的方式之一就是使用記憶體映射檔案.
note 8:
如果你希望建立一個新線程,讓它執行一些工作,然後等候結果,可以像下面這樣編碼:
dword dwexitcode;
// spawn the child process.
bool fsuccess = createprocess(..., &pi);
if (fsuccess) {
// close the thread handle as soon as it is no longer needed!
closehandle(pi.hthread);
// suspend our execution until the child has terminated.
waitforsingleobject(pi.hprocess, infinite);
// the child process terminated; get its exit code.
getexitcodeprocess(pi.hprocess, &dwexitcode);
// close the process handle as soon as it is no longer needed.
closehandle(pi.hprocess);
}
note 9:
下代碼示例展示了如何建立一個程序,然後讓它分離出去運作:
// allow the system to destroy the process & thread kernel
// objects as soon as the child process terminates.
note 10:
為什麼windows vista不是隻問一次,然後把安全資料儲存到系統中,并讓使用者随意以管理者的身份來運作應用程式呢?這樣一來,就不必每次都彈出一個uac對話框來打擾使用者了.windows vista不提供這個功能,因為否則的話,它就必須将資料儲存到某個地方(比如儲存到系統資料庫中或者其他某個檔案中).一旦這個存儲區域被成功入侵,惡意程式就可以修改這個存儲,使它總是以提升的權限來運作,而不會向使用者進行任何提示.
note 11:
russinovich詳細介紹了uac的内部機制,比如windows的系統資源虛拟化技術,它使那些在設計時沒有考慮到新的administator權限限制的應用程式能夠更好地相容新的作業系統.