CUnicodeString Class | CodeGuru

CUnicodeString Class

CUnicodeString is a replacement for CString. If you are one of these poor guys that has to write applications that use for example NetXxx functions and don’t want to create a Unicode project, or use ANSI only functions from a unicode project or what ever, you always had to deal with MultiByteToWideChar and WideCharToMultibyte. Maybe […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 14, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

CUnicodeString is a replacement for CString.
If you are one of these poor guys that has to write applications that use for example NetXxx functions and don’t want
to create a Unicode project, or use ANSI only functions from a unicode project or what ever, you always had to deal with
MultiByteToWideChar and WideCharToMultibyte. Maybe you even have to play with native functions witch uses counted unicode
strings (UNICODE_STRING) or LSA (LSAUNICODE_STRING). If you were in such situations and hated the fact that the MFC CString
doesn’t help you with these conversions, than you would love this little class…

CUnicodeString has all the functions that CString provides, so you can use it allmost everywhere where you use CString normaly.
This class works internally only with Unicode strings. Every string assignment is converted to and stored as Unicode.

And the best of it: It’s easy to use… 🙂

Example:

CUnicodeString cs;
WCHAR wc = L"My Wide String";
cs = wc;
CHAR ch = " needs a few ANSI chars to append";
cs += wc;
cs += " ";
LPSTR pstrMyText = cs;
LPWSTR pstrMyUnicodeText = cs;
cs.Empty();
cs = "MyUserName";
NetUserGetInfo(cs,....);
...

This class was written for a upcomming C++ library called SecLIB witch covers the whole NT-Security.
This is one of the reasons why it internally works in Unicode.
BTW: Using System API’s in Unicode is allways faster in NT, because NT internally works also with Unicode only… 🙂

I hope you like it! 🙂

Alexander Keck
MCSE, MCDBA
(MCSD in progress….)

Downloads

Download source – 6 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.