Windows core programming Chapter 17-Subsequent

Source: Internet
Author: User
Tags vc runtime

I. ANSI and Unicode knowledge points

2. Use a memory ing file to reverse the content of an ANSI or Unicode text file.

3. share data between processes. For details, refer to the one-day API practice.

I. ANSI and Unicode knowledge points

1,
Determine whether a text is ANSI or Unicode

2,
How to select and compare strings

3,
Support Unicode for Programs

4,
Unicode data types defined by Windows

5,
How to operate Unicode

6,
Conversion between ANSI and Unicode

1. Determine whether a text is ANSI or Unicode

1) determine that if the first two bytes of a text file are 0xff and 0xfe, Unicode is used; otherwise, ANSI is used.

2) use istextunicode for determination. Istextunicode uses a series of statistical and qualitative methods to guess the cached content. Because this is not an exact scientific method, istextunicode may return incorrect results.

2. Call comparestring.
FlagDescription
Norm_ignorecaseCase Insensitive
Norm_ignorekanatype
Do not distinguish hirakana from katakana characters
Norm_ignorenonspace
Ignore non-separated characters
Norm_ignoresymbols
Ignore symbols
Norm_ignorewidth
It does not distinguish between single-byte characters and double-byte characters.
Sort_stringsortUse punctuation marks as common symbols

3. In VC compilation options, select "Character Set" on the property page of the project above vc7.0
"UseUnicodeCharacter Set ", which may be troublesome in vc6.0. You must copy the Unicode version of the VC Runtime Library to the VC path, which is usually the same as XXX. the ANSI of LIB corresponds to xxxu. lib, which is not installed when Vc is installed by default.
1) change the language definition:
In the projectSettings"Preprocessor" on the "C ++" pageDefinitions"Change _ MBCS to _ Unicode
2) modify the entry function:
"Project" on the "Link" PageOptions"Add/entry: "wwinmaincrtstartup.
3) in the Code, the macro in tchar. h is used to process characters. For example, strcpy is replaced by _ tcscpy and tchar is used to represent char,
Use tcharM_mystr [] = _ T ("XXXX ")ReplaceCharM_mystr [] = "XXXX ";
4Note: When debugging Unicode programs, you must select all options for VC during installation. Otherwise, the dynamic library and the corresponding. Lib file will be missing.

4. Unicode data types defined by Windows

Data TypeDescription
WcharUnicodeCharacter
PwstrPointer to a unicode string
PcwstrPointer to a constant Unicode string
The corresponding ANSI data types are char, lpstr, and lpcstr.
The Common Data Types of ANSI/Unicode are tchar, ptstr, and lpctstr.

5. How to operate Unicode

Character Set
FeaturesInstance
ANSIThe operation function starts with Str.Strcpy
UnicodeThe operation function starts with "WCS ".Wcscpy
MBCSThe operation function starts with _ MBS._ Mbscpy
ANSI/UnicodeThe operation function starts with _ TCS._ Tcscpy(C Runtime database)
ANSI/UnicodeThe operation function starts with lstr.Lstrcpy(Windows functions

6. Conversion between ANSI and Unicode

1) convert ANSI to Unicode

(1) The macro L is used for implementation, for example:Clsidfromprogid (L "mapi. folder", & CLSID );
(2)Use the multibytetowidechar function to implement conversion. For example:

Char* Szprogid="Mapi. folder ";
WcharSzwideprogid [2, 128];
CLSIDCLSID;
LongLlen=Multibytetowidechar (cp_acp, 0, szprogid, strlen (szprogid), szwideprogid, sizeof (szwideprogid ));
Szwideprogid [llen]='\ 0 ';

(3) using the a2w macro, for example:
Uses_conversion;
Clsidfromprogid (A2w (szprogid), & CLSID );

2) convert Unicode to ANSI

(1) Use widechartomultibyte, for example:
//Assume that a unicode
StringWszsomestring...
CharSzansistring[Max_path];
Widechartomultibyte(Cp_acp,Wc_compositecheck,Wszsomestring,-1,Szansistring,Sizeof (szansistring ),Null,Null);

(2) Use the w2a macro, for example:
Uses_conversion;
Ptemp = w2a (wszsomestring );
Note the possible problems during conversion:
Because ANSI is converted to Unicode, if a2w or multibytetowidechar (the first parameter is cp_acp) is used, the imported ANSI string is treated as a multi-bytes String Based on the default conversion table, if it is a Chinese character (Windows is a Chinese character by default), a byte greater than 0x87 may be considered as a Chinese character together with the next byte, and then converted to the same UNICODE Chinese character based on the Unicode encoding of the Chinese character, if the corresponding encoding cannot be found, it is generally replaced by a default character (generally the question mark "? "), From this point of view, if you convert a piece of data to another, the conversion is very complex and may be irreversible, and the ANSI code you have encrypted is quite messy, there are many byte> 0x87, the conversion is irreversible.
We recommend that you write the statement as follows:
CharLpansi [count];
WcharLpunicode [count];
IntI=0;
While (lpansi [I]! ='\ 0'){
 Lpunicode [I]=(Wchar) lpansi [I];
}
Lpunicode [I]=L' \ 0 ';
And then convert it back in the same way, because for 0 ~ The Unicode code corresponding to the ANSI string 0x87 is the same 16-bit value. For others, your string is encrypted and there is no need to convert it to display the same character, it is processed in the same way. In fact, if the strings in the middle do not need to be displayed or otherwise, directly reutrn
(Lpwstr) lpansi;Or in the past,
You can simply accept it.

2. Use a memory ing file to reverse the content of an ANSI or Unicode text file.

Note: copy the specified file first before reversing to prevent the file from being irreversible.

Ø because a text file does not end with 0 characters, the last character of a string in C format must be 0. Therefore, when creating a file ing object, the length of the file is increased by a wide character (used to store the character 0 ).

Ø in a text file, the carriage return line break "\ r \ n" at the end of each line will also be reversed, after _ strrev is used in reverse order, convert "\ n \ r" to "\ r \ n ".

Bool filereverse (lpctstr pszpathname) {// open the file for reading and writinghandle hfile = createfile (pszpathname, generic_write | generic_read, 0, null, open_existing, callback, null ); if (invalid_handle_value = hfile) {afxmessagebox (_ T ("file cocould not be opened. "); Return false;} // create the file-mapping object. DWORD dwfilesize = getfilesize (hfile, null); handle hfilemap = createfilem Apping (hfile, null, page_readwrite, 0, dwfilesize + sizeof (tchar), null); If (null = hfilemap) {afxmessagebox (_ T ("file map cocould not be opened. "); closehandle (hfile); Return false;} // get the address where the first byte of the file is mapped into memory. pvoid pvfile = mapviewoffile (hfilemap, file_map_write, 0, 0); If (null = pvfile) {afxmessagebox (_ T ("cocould not map view of file ")); closehandle (hfil Emap); closehandle (hfile); Return false;} // does the buffer contain ANSI or unicodeint iunicodetestflags =-1; bool bistextunicode = istextunicode (pvfile, dwfilesize, & iunicodetestflags ); if (! Bistextunicode) {// write a character 0 to the end of the file and reverse the string pstr pchansi = (pstr) pvfile; pchansi [dwfilesize/sizeof (char)] = 0; // reverse the contexts of the file_strrev (pchansi); // _ strrev (pvfile) For ANSI ); // convert all "\ n \ r" combinations back to "\ r \ n" to preserve // The normal end-of-line sequence. pchansi = strstr (pchansi, "\ n \ r"); While (pchansi! = NULL) {* pchansi ++ = '\ R'; * pchansi ++ =' \ n'; pchansi = strstr (pchansi, "\ n \ r ");}} else {// write a character 0 to the end of the file and reverse the string pwstr pchunicode = (pwstr) pvfile; pchunicode [dwfilesize/sizeof (wchar)] = 0; if (iunicodetestflags & is_text_unicode_signature )! = 0) {// if the first character is the Unicode BOM (byte-order-mark) // 0 xfeff, keep the character at the ining of the file. pchunicode ++;} _ wcsrev (pchunicode ); // convert all "\ n \ r" combinations back to "\ r \ n" to preserve // The normal end-of-line sequence. pchunicode = wcsstr (pchunicode, l "\ n \ r"); While (pchunicode! = NULL) {* pchunicode ++ = L' \ R'; * pchunicode ++ = L' \ n'; pchunicode = wcsstr (pchunicode, L "\ n \ r") ;}// clean up everything before exiting. unmapviewoffile (pvfile); closehandle (hfilemap); // Remove trailing zero character added earlier. setfilepointer (hfile, dwfilesize, null, file_begin); setendoffile (hfile); closehandle (hfile); Return true ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.