char与wchar_t函数对照表
http://blog.csdn.net/nocml/article/details/6782407// ANSI string functions, originally from STRING.H
size_t __cdecl strlen(const char *);
int __cdecl strcmp(const char *, const char *);
char * __cdecl strcat(char *, const char *);
char * __cdecl strcpy(char *, const char *);
_CRTIMP char * __cdecl strchr(const char *, int);
_CRTIMP size_t __cdecl strcspn(const char *, const char *);
_CRTIMP char * __cdecl strncat(char *, const char *, size_t);
_CRTIMP int __cdecl strncmp(const char *, const char *, size_t);
_CRTIMP char * __cdecl strncpy(char *, const char *, size_t);
_CRTIMP char * __cdecl strstr(const char *, const char *);
_CRTIMP char * __cdecl strtok(char *, const char *);
_CRTIMP int __cdecl _stricmp(const char *, const char *);
_CRTIMP int __cdecl _strnicmp(const char *, const char *, size_t);
_CRTIMP char * __cdecl strpbrk(const char *, const char *);
_CRTIMP char * __cdecl strrchr(const char *, int);
_CRTIMP size_t __cdecl strspn(const char *, const char *);
_CRTIMP char * __cdecl _strdup(const char *);
_CRTIMP char * __cdecl _strnset(char *, int, size_t);
_CRTIMP char * __cdecl _strrev(char *);
char * __cdecl _strset(char *, int);
_CRTIMP char * __cdecl _strlwr(char *);
_CRTIMP char * __cdecl _strupr(char *);
_CRTIMP int __cdecl tolower(int);
_CRTIMP int __cdecl toupper(int);
// Widechar string functions, originally from STRING.H
_INTRIMP size_t __cdecl wcslen(const wchar_t *);
_INTRIMP int __cdecl wcscmp(const wchar_t *, const wchar_t *);
_INTRIMP wchar_t * __cdecl wcscat(wchar_t *, const wchar_t *);
_INTRIMP wchar_t * __cdecl wcscpy(wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcschr(const wchar_t *, wchar_t);
_CRTIMP size_t __cdecl wcscspn(const wchar_t *, const wchar_t *);
_CRTIMP size_t __cdecl mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count);
_CRTIMP size_t __cdecl wcstombs(char *mbstr, const wchar_t *wcstr, size_t count);
_CRTIMP wchar_t * __cdecl wcsncat(wchar_t *, const wchar_t *, size_t);
_CRTIMP int __cdecl wcsncmp(const wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl wcsncpy(wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl wcspbrk(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcsrchr(const wchar_t *, wchar_t);
_CRTIMP size_t __cdecl wcsspn(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcsstr(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcstok(wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl _wcsdup(const wchar_t *);
_CRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *);
_CRTIMP int __cdecl _wcsnicmp(const wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl _wcsnset(wchar_t *, wchar_t, size_t);
_CRTIMP wchar_t * __cdecl _wcsrev(wchar_t *);
_CRTIMP wchar_t * __cdecl _wcsset(wchar_t *, wchar_t);
_CRTIMP wchar_t * __cdecl _wcslwr(wchar_t *);
_CRTIMP wchar_t * __cdecl _wcsupr(wchar_t *);
// character classification function prototypes, CTYPE.H
_CRTIMP wchar_t towupper(wchar_t);
_CRTIMP wchar_t towlower(wchar_t);
http://blog.csdn.net/wenhm/article/details/4394916
看到大家在代码中有很多用wcscpy、strcpy等的,这里总结下给大家参考下,希望对大家有帮助(这里的API都是windows平台下的,linux下的应该也有类似的替换函数):
l _tcscpy、_tcscpy_s 都是不安全的,换成_tcsncpy /_tcsncpy_s好些,最好用_tcsncpy_s
l wcscpy、wcscpy_s都是不安全的,换成wcsncpy / wcsncpy_s好些,最好用wcsncpy_s
非unicode 函数类似,linux版本类似。
下面是一些会导致出错的代码,直接都可以运行的,有兴趣的可以试试。
#define PARRAYSIZE(array) ((sizeof(array)/sizeof(array[0])))
void CWindowsApiTest::testwcscpy()
{
WCHAR strBuf1[] = L"你是一个好人!你是一个好人!";
WCHAR strBuf2[10] = {0};
wcscpy(strBuf2, strBuf1);
int iStrLen = wcslen(strBuf2);
bool bResult = (iStrLen <= 10);
CPPUNIT_ASSERT_EQUAL_MESSAGE("内存越界了!iStrLen变成14了.", bResult, true);
}
void CWindowsApiTest::testwcscpy_s()
{
WCHAR strBuf1[] = L"你是一个好人!你是一个好人!";
WCHAR strBuf2[10] = {0};
wcscpy_s(strBuf2, 10, strBuf1);
int iStrLen = wcslen(strBuf2);
bool bResult = (iStrLen <= 10);
CPPUNIT_ASSERT_EQUAL_MESSAGE("内存越界了!但是Debug版本会提示用户越界", bResult, true);
}
void CWindowsApiTest::testwcsncpy()
{
WCHAR strBuf1[] = L"你是一个好人!你是一个好人!";
WCHAR strBuf2[10] = {0};
wcsncpy(strBuf2, strBuf1, PARRAYSIZE(strBuf2));
int iStrLen = wcslen(strBuf2);
bool bResult = (iStrLen <= 10);
CPPUNIT_ASSERT_EQUAL_MESSAGE("内存越界了!没有地方放置结束符,iStrLen:28,正确用法:wcsncpy(strBuf2, strBuf1, PARRAYSIZE(strBuf2)-1);", bResult, true);
}
void CWindowsApiTest::testwcsncpy_s()
{
WCHAR strBuf1[] = L"你是一个好人!你是一个好人!";
WCHAR strBuf2[10] = {0};
wcsncpy_s(strBuf2, PARRAYSIZE(strBuf2), strBuf1, PARRAYSIZE(strBuf2) );
int iStrLen = wcslen(strBuf2);
bool bResult = (iStrLen <= 10);
CPPUNIT_ASSERT_EQUAL_MESSAGE("内存越界了!正确用法:wcsncpy_s(strBuf2, PARRAYSIZE(strBuf2), strBuf1, PARRAYSIZE(strBuf2) - 1);", bResult, true);
}