[Convert] uses_conversion

Source: Internet
Author: User

From: http://blog.csdn.net/p40614021/article/details/6778100

 

ATL: conversion macros are a convenient way to convert various character encodings. They are very useful in function calls. The name of the ATL conversion macro is the [Source Type] 2 [New Type] or [Source Type] 2C [New Type] named according to the following pattern. According to the second form, the macro conversion result is a constant pointer (corresponding to "C" in the name ").

To use these macros, you must first include the atlconv. h header file. You can even include this header file in a non-ATL project to use the macro defined in it, because this header file is independent of other parts of ATL and does not need a _ module global variable. When you use a conversion macro in a function, you need to put the uses_conversion macro at the beginning of the function. It defines some local variables required for macro conversion.

Use in MFC to include afxconv. h
Atlconv. h In ATL
-------------
Call uses_conversion. Then, you can use a macro such as ole2t for conversion.

 

Example:

 

Convert cstring In the Unicode Character Set To char *

 

# Include <afxconv. h>

Cstring strunicode (_ T ("Unicode string "));

Uses_conversion;

Char * pszchar = w2a (strunicode ));

 

But !! Use uses_conversion with Caution !!

 

Uses_conversion is a macro definition in ATL. It is used for encoding conversion (cstring is usually used for conversion to the lpcwstr ). Use the header file # include "atlconv. H" in ATL"

Be careful when using uses_conversion. They allocate memory from the stack until the function is called to return, and the memory will not be released. If this macro is repeatedly called tens of thousands of times in a loop, it will inevitably generate stackoverflow.

 

Using a2w or other characters to convert Macros in the loop body of a function may cause stack overflow.

# Include <atlconv. h>
Void FN ()
{
While (true)
{
{
Uses_conversion;
Dosomething (a2w ("somestring "));
}
}
}

Let's analyze the preceding conversion macros.

# Define a2w (LPA )(\
(_ LPA = LPA) = NULL )? Null :(\
_ Convert = (lstrlena (_ LPA) + 1 ),\
Atla2whelper (lpwstr) alloca (_ convert * 2), _ LPA, _ convert )))

# Define atla2whelper atla2whelper

Inline lpwstr winapi atla2whelper (lpwstr lpw, lpcstr LPA, int nchars, uint ACP)
{
Atlassert (LPA! = NULL );
Atlassert (lpw! = NULL );
// Verify that no illegal character present
// Since lpw was allocated based on the size of LPA
// Don't worry about the number of chars
Lpw [0] = '\ 0 ';
Multibytetowidechar (ACP, 0, LPA,-1, lpw, nchars );
Return lpw;
}

The key lies in alloca memory allocation memory.
# Define alloca _ alloca

_ Alloca
Allocates memory on the stack.

Remarks
_ Alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function

Exits. Therefore, do not pass the pointer value returned by _ alloca as an argument to free.

The problem is that the allocated memory is allocated in the function stack. The default stack memory space of the VC compiler is 2 MB. When a function is called cyclically, the memory in the stack is continuously allocated.

Solutions to the above problems:
1. Write character conversion functions by yourself. Do not be lazy.
Function that safely converts a 'wchar 'string to 'lpstr ':
Char * convertlpwstrtolpstr (lpwstr lpwszstrin)
{
Lpstr pszout = NULL;
If (lpwszstrin! = NULL)
{
Int ninputstrlen = wcslen (lpwszstrin );

// Double null termination
Int noutputstrlen = widechartomultibyte (cp_acp, 0, lpwszstrin, ninputstrlen, null, 0, 0, 0) + 2;
Pszout = new char [noutputstrlen];

If (pszout)
{
Memset (pszout, 0x00, noutputstrlen );
Widechartomultibyte (cp_acp, 0, lpwszstrin, ninputstrlen, pszout, noutputstrlen, 0, 0 );
}
}
Return pszout;
}
And so on.

2. Place the character conversion part in a function for processing.

Void FN2 ()
{
Uses_conversion;
Dosomething (a2w ("somestring "));
}

Void FN ()
{
While (true)
{
FN2 ();
}
}

If you do not know this problem, it is difficult to identify the cause of the crash after use.

[Convert] uses_conversion

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.