What type of string is best to use for Win32 and DirectX?

827 Views Asked by At

I am in the process of developing a small game in DirectX 10 and C++ and I'm finding it hell with the various different types of strings that are required for the various different directx / win32 function calls.

Can anyone recommend which of the various strings are available are the best to use, ideally it would be one type of string that gives a good cast to the other types (LPCWSTR and LPCSTR mostly). Thus far I have been using std::string and std::wstring and doing the .c_str() function to get it in to the correct format.

I would like to get just 1 type of string that I use to pass in to all functions and then doing the cast inside the function.

3

There are 3 best solutions below

5
On BEST ANSWER

Use std::wstring with c_str() exactly as you have been doing. I see no reason to use std::string on Windows, your code may as well always use the native UTF-16 encoding.

3
On

If you're using Native COM (the stuff of #import <type_library>), then _bstr_t. It natively typecasts to both LPCWSTR and LPCSTR, and it meshes nicely with COM's memory allocation model. No need for .c_str() calls.

1
On

I would stick to std::wstring as well. If you really need to pass std::string somewhere, you can still convert it on the fly:

std::string s = "Hello, World";
std::wstring ws(s.begin(), s.end());

It works the other way around as well.