微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > Win CE开发特性及忠告

Win CE开发特性及忠告

时间:09-12 来源:互联网 点击:

字符串。因此,采用逐个字节分析Unicode字符串的函数也许足以确定字符串在widechars中的长度。

在ANSI和Unicode字符串之间进行翻译

如果你的Windows CE应用程序接口于台式PC,也许你必须操作PC机中的ANSI字符串数据(例如,char字符串)。即使你在程序中只用到Unicode字符串,这都是事实。

你不能在Windows CE上处理一个ANSI字符串,因为没有操纵它们的库函数。最好的解决办法是将ANSI字符串转换成Unicode字符串用到H/PC上,然后再将Unicode字符串转换回ANSI字符串用到PC上。为了完成这些转换,可采用MultiByteToWideChar()和WideCharToMultiByte () Win32 API 函数。

对于Windows CE 1.0的字符串转换,劈开(hack)

在Windows CE 1.0 版本中,这些Win32API函数还没有完成。所以如果你想既要支持CE 1.0又能支持CE 2.0,就必须采用其它函数。将ANSI字符串转换成Unicode字符串可以用wsprintf(),其中第一个参数采用一widechar字符串,并且认识%S(大写),意思是一个字符串。由于没有wsscanf() 和 wsprintfA(),你必须想别的办法将Unicode字符串转换回ANSI字符串。由于Windows CE 1.0不在国家语言支持(NLS)中,你也许得求助于hack,如下所示:

/*

Definition / prototypes of conversion functions

Multi-Byte (ANSI) to WideChar (Unicode)

atow() converts from ANSI to widechar

wtoa() converts from widechar to ANSI

*/

#if ( _WIN32_WCE >= 101)

#define atow(strA, strW, lenW) \

MultiByteToWidechar (CP_ACP, 0, strA, -1, strW, lenW)

#define wtoa(strW, strA, lenA) \

WideCharToMutiByte (CP_ACP, 0, strW, -1, strA, lenA, NULL, NULL)

#else /* _WIN32_WCE >= 101)*/

/*

MultiByteToWideChar () and WideCharToMultiByte() not supported o-n Windows CE 1.0

*/

int atow(char *strA, wchar_t *strW, int lenW);

int wtoa(wchar_t *strW, char *strA, int lenA);

endif /* _WIN32_WCE >= 101*/

#if (_WIN32_WCE 101)

int atow(char *strA, wchar_t *strW, int lenW)

{

int len;

char *pA;

wchar_t *pW;

/*

Start with len=1, not len=0, as string length returned

must include null terminator, as in MultiByteToWideChar()

*/

for(pA=strA, pW=strW, len=1; lenW; pA++, pW++, lenW--, len++)

{

*pW = (lenW = =1) ? 0 : (wchar_t)( *pA);

if( ! (*pW))

break;

}

return len;

}

int wtoa(wxhar_t *strW, char *strA, int lenA)

{

int len;

char *pA;

wchar_t *pW;

/*

Start with len=1,not len=0, as string length returned

Must include null terminator, as in WideCharToMultiByte()

*/

for(pA=strA, pW=strW, len=1; lenA; pa++, pW++, lenA--, len++)

{

pA = (len==1)? 0 : (char)(pW);

if(!(*pA))

break;

}

return len;

}

#endif /*_WIN32_WCE101*/

这种适合于Windows CE 1.0的实现办法比使用wsprintf()函数要容易,因为使用wsprintf()函数更难以限制目标指针所指向的字符串的长度。

选择正确的字符串比较函数

如果你要分类Unicode标准字符串,你会有以下几个函数可供选择:

wcscmp(), wcsncmp(), wcsicmp(), 和wcsnicmp()

wcscoll(), wcsncoll(), wcsicoll(),和wcsnicoll()

CompareString()

第一类函数可用来对字符串进行比较,不参考当地(Locale)或外文字符。如果你永远不想支持外文,或者你仅仅想测试一下两个字符串的内容是否相同,这类函数非常好用。

第二类函数使用现有的当地设置(current locale settings)(系统设置,除非你在字符串比较函数之前调用了wsetlocale()函数)来比较两个字符串。这些函数也能正确分类外文字符。如果当地的字符C(C locale)被选定,这些函数与第一类函数就具有了相同的功能。

第三类函数是Win32函数CompareString()。这个函数类似于第二类函数,但是它允许你指定当地设置(the locale)作为一个参数,而不是使用现有的当地设置(current locale settings)。CompareString()函数允许你选择性地指定两个字符串的长度。你可以将第二个参数设置为NORM_IGNORECASE,从而使函数比较字符串时不比较大小写。

通常,即使不将第二个参数设置为NORM_IGNORECASE,CompareString()函数也不用来区分大小写。我们经常用wcsncoll()函数来区分大小写,除非使用当地的字符C(C locale)。所以,在我们的代码中,不使用CompareString()函数来区分大小写,而用wcsncoll()函数来区分大小写

不要使用相对路径

与Windows NT不一样,Windows CE没有当前目录这个概念,

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top