2

i must say im new to win32 c++ programming so i face a problem that
some code compile in Multi-Byte Character Set and not in Unicode Character Set.
how can my code support both ?
for example this NOT compiles in Multi-byte only in Unicode and the commented vector only in MultiByte:

 //vector<char> str2(FullPathToExe.begin(), FullPathToExe.end()); vector<wchar_t> str2(FullPathToExe.begin(), FullPathToExe.end()); str2.push_back('\0'); if (!CreateProcess(NULL, &str2[0], NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) 

4 Answers 4

7

Use TCHAR as the character type (e.g. std::vector<TCHAR>), which is:

A WCHAR if UNICODE is defined, a CHAR otherwise.

This type is declared in WinNT.h as follows:

#ifdef UNICODE typedef WCHAR TCHAR; #else typedef char TCHAR; #endif 
Sign up to request clarification or add additional context in comments.

1 Comment

where to use it in the vector?
4

You don't have to support both, unless your application must support Windows Mobile or desktop version like Windows 95 or older.

If you write for current desktop or server Windows, supporting "Unicode" is enough. Just go for wchar_t!

Comments

1

By "new to Win32 C++ programming", I'm assume you mean you don't have an existing large program using "ANSI" strings that you need to maintain. If so, then why do you want to build an "ANSI" version? Just do everything with wchar_t.

vector<wchar_t> str2(FullPathToExe.begin(), FullPathToExe.end()); str2.push_back(L'\0'); // Note the prefix. if (!CreateProcessW(NULL, // Note the W; explicit is better than implicit. &str2[0], NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) 

If you need to work with multi-byte strings (e.g., for reading files, or for working with third-party libraries that use char rather than wchar_t), then convert them using WideCharToMultiByte and MultiByteToWideChar.

Comments

0

You could use the macros/typedefs provided by microsoft and add your own, to support both.

TCHAR -> typedef to char/wchar_t _TEXT() -> creates a text constant either wide or multibyte _TEXT("hallo") 

Probably usefull to add so you could use a String class instead of a vector for text manipulations:

#ifdef UNICODE typedef std::wstring String; #else typedef std::string String; #endif 

2 Comments

Why not just typedef std::basic_string<TCHAR> String;?
@Georg this is even shorter shorter. nice!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.