Brief description Win CE development characteristics and advice (5)

zhaozj2021-02-16  82

Do not put a Unicode string into an odd memory address

On the Intel Series processor, you can store any variables or arrays in a monarch memory address, and will not cause any fatal errors. But in the h / PC, this is not necessarily possible? You must be cautious about the type of data greater than one byte, including Wchar_T defined as unsigned shorts. When you try to access them, placing them in an odd address will result in overflow.

The editor often reminds you on these issues. You can't manage the stack variable address, and the editor will check to determine if these addresses match if these addresses are matched. Similarly, the running time library must ensure that the memory allocated from the stack always satisfies a Word boundary, so you don't have to worry about it. However, if the application contains the code to copy memory area with the memcpy () function, or use some type of pointer arithmetic to determine the memory address, the problem may appear. Consider the following example:

INT sent_name (tchar * pszname) {char * p, * q; int Nlen = (_ tcslen (pszname) 1) * sizeof (tchar);

P = maloc (Header_size Nlen); if (p) {q = p header_size; _tcscpy ((tchar *) q, pszname);} / * etc * /

This code is allocated from the heap and copy a string, and leaves a header_size size in the beginning of the string. Suppose UNICODE is defined, then the string is a WideChar string. If header_size is an even number, this code will work normally, but if header_size is odd, this code will be wrong because Q Points will be odd.

Note that this problem does not occur when you test this code on the Windows CE emulator in the Intel Series processor.

In this example, just make sure the header_size is an even number, you can avoid problems. However, in some cases you may not do this. For example, if the program is input from a PC, you may have to use the prior defined binary format, although it is not suitable for H / PC. In this case, you must use a function that controls the string instead of the TCHAR pointer with a character pointer. If you know the length of the string, you can replicate the string with a memcpy (). Therefore, the function of using a Byte Analysis Unicode string may be sufficient to determine the length of the string in WideChars.

Translate between ANSI and Unicode strings

If your Windows CE application interface is on the desktop PC, you may have an ANSI string data (for example, a char string) in the PC. This is the fact even if you use the Unicode string in the program.

You can't handle an ANSI string on Windows CE because you don't manipulate their library functions. The best solution is to convert the ANSI string into a Unicode string to use the H / PC, and then convert the Unicode string back to the ANSI string to use the PC. In order to complete these conversions, multibyToWideChar () and widechartomultibyte () Win32 API functions can be used.

For Windows CE 1.0 string conversion, cleavage (HACK)

These Win32API functions have not been completed in Windows CE 1.0 versions. So if you want to support CE 1.0 and support CE 2.0, you must use other functions. Convert ANSI string to Unicode strings to use WSPrintf (), where the first parameter uses a WideChar string and knows "% s" (capital) means a string. Since there is no WSSCANF () and WSPrintfa (), you must think of a UNICODE string back into an ANSI string. Since Windows CE 1.0 is not in Country Language Support (NLS), you may have to help Hack, as shown below: / * Definition / prototypes of conversMulti-byte (ANSI) to widechar (unicode)

Atow () Converts from Ansi to Widecharwtoa () Converts from widechar to Ansi * / # = (_win32_wce> = 101)

#define atow (str, 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) * /

/ * MultibyToWideChar () 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 Returnedmust Include Null Terminator, AS IN MultibyToWideChar () * / for (PA = Stra, PW = Strw, Len = 1; LENW; PA , PW , LENW- -, LEN ) {* pw = (lenw = = 1)? 0: (wchar_t) (* pa); if (! (* pw)) Break;} returnon;

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 returnedMust 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.

#ENDIF / * _WIN32_WCE <101 * / This implementation of this implementation of Windows CE 1.0 is easier than using the WSPrintf () function, because using the WSPrintf () function is more difficult to limit the length of the string pointed to by the target pointer.

- Reprinted from blue ideal digital station

转载请注明原文地址:https://www.9cbs.com/read-13818.html

New Post(0)