char:電腦程式設計語言(c、c++、java、VFP等)中可容納單個字元的一種基礎資料型別 (Elementary Data Type)。
TCHAR:為了滿足Unicode編碼,對char的擴充,即_T(“str”)表示TCHAR類型
C++支援兩種字串,即常規的ANSI編碼(使用""包裹)和Unicode編碼(使用L""包裹),這樣對應的就有了兩套字串字串處理函數,比如:strlen和wcslen,分別用於處理兩種字串char和TCHAR類型
winnt.h標頭檔中:
typedef WCHAR TCHAR, *PTCHAR;
表明 TCHAR 與 WCHAR 屬同一類型
char szA[100]; // ANSI string buffer
WCHAR szW[100]; // Unicode string buffer
// Normal sprintf:all strings are ANSI
sprintf(szA, "%s","ANSI Str");
// Converts Unicode string to ANSI
sprintf(szA,"%S",L"Unicode Str");
// Normal swprintf:all strings are Unicode
swprintf(szW,L"%s",L"Unicode Str");
// Converts ANSI string to Unicode
swprintf(szW,L"%S", "ANSI Str");
注意:大寫S 和小寫s 的使用
===========================
應用執行個體:通過system函數程式調用啟動msc程式
void WSUS::OnBnClickedOk(){CString strPath = NULL;// 申請路徑字串(TCHAR)char strChar[256];// 申請路徑字串(char)m_CustomEdit.GetWindowTextW(strPath);// 擷取路徑儲存到strPathstrPath.Replace(_T("\\"), _T("\\\\"));// 替換strPath中"\"為"\\",注意轉換符//sprintf(strChar, "%s %S", "mmc.exe", strPath);// TCHAR轉換char類型sprintf(strChar, "mmc.exe \"%S\"", strPath);// TCHAR轉換char類型MessageBox(strPath, _T("title"));system(strChar);// 系統函數調用啟動msc程式//WinExec((LPCSTR)_bstr_t(strPath), SW_SHOW);// 調用exe程式}
樣本步驟:
1、擷取msc程式路徑strPath
2、替換strPath中"\"為"\\"字元
C:\Windows\System32\gpedit.msc
首先,通過 strPath.Replace(_T("\\"), _T("\\\\")); 轉換成:
C:\\Windows\\System32\\gpedit.msc
然後,通過 sprintf(strChar, "%s %S", "mmc.exe", strPath); 拼接字串為:
mmc.exe C:\\Windows\\System32\\gpedit.msc
3、system函數調用啟動msc程式
system(strChar);
4、啟動結果如下