Windows core programming Reading Notes (1)

Source: Internet
Author: User

Chapter 1: Error Handling

 

Use the getlasterror function to obtain more error information, or use @ err and HR (vs2005) in the Monitoring Box to obtain error information, not just error numbers.

 

Chapter 2: character and string processing

  

In an application, ensure that either Unicode and _ Unicode are defined at the same time, or neither is defined. The former is used by windows, and the latter is used by the C Runtime Library.

For program security, we should try to use a string processing function with the suffix _ S.

After a string function with the suffix of _ s is used, if the size exceeds the size allowed by the buffer, the first character of the entire string is set to '\ 0', and other bytes are filled with 0xfd.

Stringcchcat, stringcchcopy, stringcchprintf, stringcchprintfex series functions will be truncated when the buffer is too small, rather than directly truncated as a Null String like the _ S Series functions.

Call _ crtsetreportmode (_ crt_assert, 0). Then, you can disable all debug assertion failed dialogs that may be triggered by C runtime.

Use _ countof to obtain the number of characters, which is expressed by the CCH prefix in the declaration of the string function. Use sizeof to obtain the number of bytes of a string, which is expressed by CB in the declaration of a string function.

  

Use the WINDOWS function multibytetowidechar to convert a multi-byte string to a wide-byte string.

Multibytetowidechar (
Uint codePage,
DWORD dwflags,
Lpcstr lpmultibytestr,
Int cbmultibyte,
Lpwstr lpwidecharstr,
Int cchwidechar );

The ucodepage parameter is a code page value. The ANSI code page is 1252, the Japanese code page is 932, and the simplified Chinese code page is 936,950, which indicates traditional Chinese.

Dwflags is usually 0.

Lpmultibytestr specifies the string to be converted, and the cbmultibyte parameter specifies the length (number of bytes) of the string ). If the value passed to the cbmultibyte parameter is-1, the function can automatically determine the length of the source string.

Lpwidecharstr specifies the target memory buffer. The maximum length (characters) of the buffer must be specified in cchwidechar ). If you call multibytetowidechar and input 0 to the cchwidechar parameter, the function will not perform the conversion, but return a wide character number, including \ 0.

To convert a multi-byte string to Unicode, follow these steps.

1. Call the multibytetowidechar and lpwidecharstr parameters to pass in null, The cchwidechar parameter to 0, and the cbmultibyte to pass in-1.

2. Allocate a piece of memory. Its size is the return value of the first call multiplied by sizeof (wchar_t ).

3. Call multibytetowidechar again, pass the memory address allocated in step 2 to lpwidecharstr, and pass the returned value of step 1 to cchwidechar.

4. Use the converted string.

5. release memory blocks occupied by Unicode strings.

Use the WINDOWS function widechartomultibyte to convert a wide string to a multi-byte string.

Function prototype:

Int widechartomultibyte (

UintCodePage,

DWORDDwflags,

LpwstrLpwidecharstr,

IntCchwidechar,

LpcstrLpmultibytestr,

IntCchmultibyte,

LpcstrLpdefaultchar,

PboolPfuseddefaultchar);

The usage is similar to the multibytetowidechar function. The difference lies in the last two parameters. When a function encounters a text that is not in the code page specified by codePage, it will replace it with the character pointed to by lpdefaultchar. If this parameter is blank, the system will replace it with the default character, which is usually a question mark, which is dangerous to convert the file name because the question mark is a wildcard.

  PfuseddefaultcharPoint to a Boolean variable. if at least one character cannot be converted successfully, this Boolean variable is set to true. If all characters can be converted successfully, this variable is set to false.

 

Chapter 3 kernel objects

Many windows objects are kernel objects: access token objects, event objects, file objects, file ing objects, I/O completed port objects, job objects, and Mailslot objects, mutex object, pipe object, process object, thread object, semaphore object, waiting timer object, thread pool factory object, etc.

Objects are basically created using functions starting with "CREATE", shared among processes through open functions, and disabled by using the closehandle function.

There is a reference counter inside the object. After each process obtains access to this object, this counter adds one. When a process stops running, this counter is reduced by one. Therefore, if an object created by a process is used by another thread during the process, its lifetime may be longer than that of the process that created it. The system only destroys this object when the counter is changed to 0.

Cross-process Kernel Object sharing:

1. Use the object handle to inherit.

2. Name the object.

3. Copy the object handle.

 

  

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.