顯示具有 WinCE (MFC) 標籤的文章。 顯示所有文章
顯示具有 WinCE (MFC) 標籤的文章。 顯示所有文章

2010年5月3日 星期一

C/C++禁用危險API

這是之前在網路上看到有好心人整理出來的表格
其實這些禁用的API還是可以用,但其實主要目是是為了減少代碼中引入安全漏洞的可能性
如果能避開不用當然是最好的

下面這個表格是禁用的API,各自可以替代的函數

MSDN: http://msdn.microsoft.com/en-us/library/ff468910%28v=VS.85%29.aspx

禁用的API 替代的StrSafe函數 替代的Safe CRT函數
有關字符串拷貝的API
strcpy, wcscpy, _tcscpy, _mbscpy, StrCpy, StrCpyA, StrCpyW, lstrcpy, lstrcpyA, lstrcpyW, strcpyA, strcpyW, _tccpy, _mbccpy StringCchCopy, StringCbCopy,

StringCchCopyEx, StringCbCopyEx

strcpy_s
有關字符串合併的API
strcat, wcscat, _tcscat, _mbscat, StrCat, StrCatA, StrCatW, lstrcat, lstrcatA, lstrcatW, StrCatBuffW, StrCatBuff, StrCatBuffA, StrCatChainW, strcatA, strcatW, _tccat, _mbccat StringCchCat, StringCbCat,

StringCchCatEx, StringCbCatEx

strcat_s
有關sprintfAPI
wnsprintf, wnsprintfA, wnsprintfW, sprintfW, sprintfA, wsprintf, wsprintfW, wsprintfA, sprintf, swprintf, _stprintf StringCchPrintf, StringCbPrintf,

StringCchPrintfEx, StringCbPrintfEx

_snprintf_s

_snwprintf_s


2010年3月9日 星期二

(MFC)Thread- Afxbeginthread

先貼上我成功宣告的thread
光宣告就花了我好久的時間...
CChildView.cpp
UINT CChildView::WorkerThreadProc(LPVOID Param) //Sample function for using in AfxBeginThread
{
CChildView* pView = (CChildView*) Param;

return 0;
}

在要呼叫thread的地方寫上
AfxBeginThread(WorkerThreadProc,(LPVOID)this);
CChildView.h
static UINT WorkerThreadProc(LPVOID Param);

會用很久的原因在於我一直不想把WorkerThreadProc 宣告成成員function
我用成全域function 但是一直不能編譯成功
後來只好放棄改用成員function,但宣告成成員function一定要宣告成靜態
不然會一直編譯失敗...

2010年3月2日 星期二

CString:: right && CString ::left 用法

CString Left(int nCount)

Parameters

nCount
Specifies the number of characters to extract from this CString object.

Return Value

A CString object containing a copy of the specified range of characters. Note that the returned CString object may be empty.

Example

The following example demonstrates the use of CString::Left.

// example for CString::Left CString s(_T("abcdef")); ASSERT(s.Left(2) == _T("ab"));
left和right的用法是相同的
只是算個數的方向不同
left - 是從左數到右
right - 是從右數到左

CString cs = _T("abcdef");
字串.left(個數) =>ex. cs.left(2) = _T("ab");
字串.right(個數) =>ex. cs.right(2) = _T("ef");

ReverseFind 使用

最近會用到ReverseFind,之前有找一下資料
可是都會忘....

Return Value

The index of the last character in this CString object that matches the requested character; –1 if the character is not found.

Parameters

ch

The character to search for.

Remarks

Searches this CStringstrrchr. object for the last match of a substring. The function is similar to the run-time function

Example

// Example for CString::ReverseFind
CString s( "abcabc" );
ASSERT( s.ReverseFind( 'b') == 4 );

這是msdn上的解釋
我每次再回來看都是有點看不懂
所以我又上網找了一下別人的解釋

原來有一個跟ReverseFind 類似的字串符搜尋api 就是Find

CString str = "abcdbc";
int x1 = str.Find('b');
int x2 = str.ReverseFind('b');
x2 = 4;
x1 = 1;


後來我終於知道他們的差異在哪 T_T




Find 是找第一個'b'

ReverseFind 是找最後一個'b'

2010年2月23日 星期二

CString GetBuffer() && GetString 差別

CString中提供兩個成員函式讓我們可以取得其字串的指標
一個是GetString() 另一個是GetBuffer()
CString::GetString()
GetString() 使用時機是當我們需要知道其(char *)指標以取得其字元內容
比方說 某個window API需要(char*)指標做為輸入時 我們就可以使用GetString()
不過要注意的是 GetString() 所傳出來的指標是不能被修改的
他只能用來讓我們知道裡面的字元 而不能讓我們對裡面做修改


CString::GetBuffer()
GetBuffer() 他會create出所指定大小的空間出來 這個空間是可以讓我們修改的
很多時候 有的 API 會要一個(char*)的指標作為輸出
如果我們就因為這樣去產生一個(char*)的buffer 給他 等到資料取出來之後
便無法使用CString 的種種方便功能
因此 比較好的做法 便是用GetBuffer()來產生一個buffer空間給他
等到取出來之後 我們便可以直接使用CString來對他操作
GetBuffer() 使用完後 最好是呼叫一下ReleaseBuffer()做為結束

範例

CFile file;
// FILE_NAME 為事先定義好的檔案名稱
if
(file.Open(FILE_NAME,CFile::modeRead))
{
CString szContent;
int nFileLength = file.GetLength();
file.Read(szContent.GetBuffer(nFileLength),nFileLength);
szContent.ReleaseBuffer();
// 取得檔案內容放在szContent中 我們之後可以對其操作
}

2010年1月27日 星期三

TXT Viewer- fopen()

Open File or URL

Function:
FILE *fopen(
const char *filename, // 絕對路徑檔案名稱
const char *mode // 讀檔方式
);
FILE *_wfopen(
const wchar_t *filename,
const wchar_t *mode
);