Wide character functions and secure CRT functions in Windows Programming

Source: Internet
Author: User

Wide character types and functions are newly added to the C and C ++ Standards (ANSI/ISO/IEC c 1999 and ISO/iec c ++ 1998/2003, they are used to support International Unicode (1993) character sets. Microsoft started to strictly implement the new C/C ++ standard from Visual C ++ 2005.

Security CRT functions are extensions of the C/C ++ language by Microsoft. Some of the content was submitted to ISO as the revision suggestions for the next C/C ++ standard version on April 9, 2003. In vc05/08, if these security functions are not used, the compiler reports an alert error.

1) Common wide character Functions

Because Windows NT/2000/XP adopts UNICODE character encoding, the characters are both dubyte characters. Therefore, in MFC programming, we generally need to use the double-byte character type wchar_t, the corresponding string, and its pointer type lpcwstr and lpctstr, and add the L conversion character before the constant string, string Length Calculation functions cannot use strlen instead of wcslen, and sprintf cannot be used for string format printing functions, instead, use swprintf (the character and string format are also changed from % C and % s to % LC and % ls ).

The wchar_t type is the built-in data type and keyword in the Standard C ++, And the typedef type in the c99 standard. The equivalent data type is related to the specific implementation, in Win32 and Vc, it is defined:

Typedef unsigned short wchar_t; (double-byte unsigned short integer)

Below are some common wide character functions (included in the standard library of ISO c99/iso c ++ ):

# Include <wchar. h>

Size_t wcslen (const wchar_t * s );

Int wprintf (const wchar_t * format ,...);

Int wscanf (const wchar_t * format ,...);

Int swprintf (wchar_t * s, size_t N, const wchar_t * format ,...);

Int swscanf (const wchar_t * s, const wchar_t * format ,...);

Long int wcstol (const wchar_t * nptr, wchar_t ** endptr, int base );

Float wcstof (const wchar_t * nptr, wchar_t ** endptr );

Double wcstod (const wchar_t * nptr, wchar_t ** endptr );

# Include <stdlib. h>

Errno_t _ itow_s (INT value, wchar_t * buffer, size_t sizeincharacters, int Radix );

Errno_t _ ultow_s (unsigned long value, wchar_t * STR, size_t sizeofstr, int Radix );

Size_t mbstowcs (wchar_t * wcstr, const char * mbstr, size_t count );

Size_t wcstombs (char * mbstr, const wchar_t * wcstr, size_t count );

2) Common Security CRT Functions

Security CRT (C Runtime Library = C Runtime Library) function is an extension of Microsoft's C/C ++ language. It adds the suffix "_ s" after the original function name, generally Returns Error code, and adds the original function return value as a parameter to the end of the function input parameter list; for functions with buffer parameters, an input parameter indicating the buffer size is added to prevent memory overflow.

In vc05/08, if these security functions are not used, the compiler reports an alert error.

The following are some common security CRT functions:

Char * gets_s (char * buffer, size_t sizeincharacters); // <stdio. h>

Wchar_t * _ getws_s (wchar_t * buffer, size_t sizeincharacters); // <stdio. h> or <wchar. h>

Errno_t _ itoa_s (INT value, char * buffer, size_t sizeincharacters, int Radix); // <stdlib. h>

Errno_t _ itow_s (INT value, wchar_t * buffer, size_t sizeincharacters, int Radix); // <stdlib. h>

Errno_t _ ultoa_s (unsigned long value, char * STR, size_t sizeofstr, int Radix); // <stdlib. h>

Errno_t _ ultow_s (unsigned long value, wchar_t * STR, size_t sizeofstr, int Radix); // <stdlib. h>

Int printf_s (const char * Format [, argument]...); // <stdio. h>

Int wprintf_s (const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Int scanf_s (const char * Format [, argument]...); // <stdio. h>

Int wscanf_s (const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Int sprintf_s (char * buffer, size_t sizeofbuffer, const char * Format [, argument]...); // <stdio. h>

Int swprintf_s (wchar_t * buffer, size_t sizeofbuffer, const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Int sscanf_s (const char * buffer, const char * Format [, argument]...); // <stdio. h>

Int swscanf_s (const wchar_t * buffer, const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Int fprintf_s (File * stream, const char * Format [, argument]...); // <stdio. h>

Int fwscanf_s (File * stream, const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Int fscanf_s (File * stream, const char * Format [, argument]...); // <stdio. h>

Int fwscanf_s (File * stream, const wchar_t * Format [, argument]...); // <stdio. h> or <wchar. h>

Errno_t strcpy_s (char * strdestination, size_t sizeinbytes, const char * strsource); // <string. h>

Errno_t wcscpy_s (wchar_t * strdestination, size_t sizeinwords, const wchar_t * strsource); // <string. h> or <wchar. h>

Errno_t fopen_s (File ** pfile, const char * filename, const char * mode); // <stdio. h>

Errno_t _ wfopen_s (File ** pfile, const wchar_t * filename, const wchar_t * mode); // <stdio. h> or <wchar. h>

Errno_t mbstowcs_s (size_t * pconvertedchars, wchar_t * wcstr, size_t sizeinwords, const char * mbstr, size_t count); // <stdlib. h>

Errno_t wcstombs_s (size_t * pconvertedchars, char * mbstr, size_t sizeinbytes, const wchar_t * wcstr, size_t count); // <stdlib. h>

Errno_t rand_s (unsigned int * randomvalue); // <stdlib. h>

The following is the definition of data types used by several security function prototypes:

# Include <crtdefs. h>

Typedef int errno_t;

Typedef unsigned short wchar_t;

# Ifdef _ win64

Typedef unsigned _ int64 size_t;

# Else

Typedef _ w64 unsigned int size_t;

# Endif

Related Article

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.