开发者

Making TCHAR* compatible with char*

开发者 https://www.devze.com 2023-02-11 18:36 出处:网络
So a 开发者_开发问答quick question how do I make TCHAR* (or wchar_t as its a macro) work with char*?

So a 开发者_开发问答quick question how do I make TCHAR* (or wchar_t as its a macro) work with char*?

I am using the unicode character set.

The code that is the problem is:

 TCHAR* D3DDevTypeToString(D3DDEVTYPE devType) {
   switch (devType) {
      case D3DDEVTYPE_HAL:    
      return TEXT("D3DDEVTYPE_HAL");
      case D3DDEVTYPE_SW:     
      return TEXT("D3DDEVTYPE_SW");
      case D3DDEVTYPE_REF:    
      return TEXT("D3DDEVTYPE_REF");
      default:                
      return TEXT("Unknown devType");
      }
   }

The obvious solution is to change TCHAR* to char* but I would like to keep it as TCHAR* if possible.

Thanks.


And yes I am using the unicode character set.

Then you cannot make TCHAR compatible with char. Because if you're using UCS, then TCHAR is wchar_t. The type char is not related in any way to wchar_t. You could do some work to convert the string (using e.g. WideCharToMultiByte), but then you'd lose Unicode support.


TCHAR could be wchar_t or char, depeding upon whether the macro UNICODE is defined or not.

  • If the macro UNICODE is defined, then TCHAR means wchar_t. In this case, you cannot use TCHAR in place of, or with, char. It's dangerous!
  • If it's not defined, then TCHAR means char. In this case, you can use TCHAR in place of, or with, char. After all, they're same now.


Write a converter from UTF-16 to UTF-8 (or use the win32 API function if that's what it does). Make it generic so it can work on char*, wchar_t*, std::string, and std::wstring. Write a templated function string_cast and let the second parameter be figured out by the compiler. Override string_cast for the different combinations you will be using. Convert between TCHAR* to std::string with string_cast and then it doesn't matter how TCHAR is defined (assuming you overrode for both char and wchar_t.

0

精彩评论

暂无评论...
验证码 换一张
取 消