CW2A ATL7.0 Notes

Copy from the MSDN, just put here for reference !
~aisnote at gmail dot com

//Example 1 // Convert LPCWSTR to LPCSTR. void

ExampleFunction1(LPCWSTR pszW)
{
 

// Create an instance of CW2A, called pszA,  // and initialize it with pszW.

   CW2A pszA(pszW);
 

// pszA works like an LPCSTR, and can be used thus:

   ExampleFunctionA(pszA); 
 

// Note: pszA will become invalid when it goes out of scope.

}
 

// Example 2 // Use a temporary instance of CW2A. void

ExampleFunction2(LPCWSTR pszW)
{
 

// Create a temporary instance of CW2A,  // and initialize it with pszW.

   ExampleFunctionA(CW2A(pszW));
 

// Note: the temporary instance becomes invalid   // after the execution of the statement above.

}
 

// Example 3 // Incorrect use of conversion macros. void

ExampleFunction3(LPCWSTR pszW)
{
 

// Create a temporary instance of CW2A,  // save a pointer to it and then delete  // the temportary instance.

   LPCSTR pszA = CW2A(pszW);
 

// The pszA in the following line is an invalid pointer,  // as the instance of CW2A has gone out of scope.

   ExampleFunctionA(pszA);
}

A Warning Regarding Temporary Class Instances

It should be stressed that the following is not good code:

LPCTSTR szr = CA2T(szReplaceFile);

Using ATL 3.0 macros, it was acceptable to use:

LPCTSTR szr = A2T(szReplaceFile);  

as the memory allocated by the conversion functions would not be freed until the current function was exited. The same code does not work with the new classes.

This code:

LPCTSTR szr = CA2T(szReplaceFile);  

is equivalent to this:

LPCTSTR szr;
{
   CA2T temp(szReplaceFile);
   szr = temp.

operator

LPTSTR();
}  

As the memory allocated by the temporary object and returned from the cast operator is destroyed when the temporary object is destroyed, using the value in szr will have undesirable results.

Instead, use this code:

CA2T szr(szReplaceFile);  

The cast operator makes the CA2T object look like a LPCTSTR.

Advanced Usage

The default static buffer size is 128 characters. If the buffer size must be changed for a specific conversion, use the EX version of a macro, and specify the buffer size as a template argument.

// Example 4 // Changing the size of the buffer. void

ExampleFunction4(LPCWSTR pszW)
{
 

// Use a 16-character buffer.

   ExampleFunctionA(CW2AEX<16>(pszW));
}

Here is an example of specifying the code page as the second parameter to the constructor for the class.

// Example 5 // Specifying the code page. void

ExampleFunction5(LPCWSTR pszW)
{
 

// Convert to the Macintosh code page

   ExampleFunctionA(CW2A(pszW, CP_MACCP));
}

 

0
3 comments to “CW2A ATL7.0 Notes”
  1. the other reason why it’s a two-bytes inittucrson is because you can write two bytes in one pass (and your code should do it, too, not one byte at a time).of course, using MMX or CMPXCHG8B, for example, you can write more than that at a time, but there isn’t a single inittucrson that writes only five bytes (and there are plenty of functions where you wouldn’t want to overwrite all eight bytes)

  2. Social medias networks plays an important role and brings great revolution in an business that more useful for market . Because its provides good system that freely communicate and shearing views and ideas with other person in all over the world .

Comments are closed.