How do I convert from _TCHAR * to char * when using C++ variable-length args?

前端 未结 4 1419
情话喂你
情话喂你 2021-01-12 05:53

We need to pass a format _TCHAR * string, and a number of char * strings into a function with variable-length args:

inline void FooBar(const _TCHAR *szFmt,          


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 06:30

    this is something I have used before to convert a TCHAR to char, hope it helps, although I wasn't really looking for optimization, so it's not the fastest way.. but it worked!

        TCHAR tmp[255];
    ::GetWindowText(hwnd, tmp, 255);
    std::wstring s = tmp;
    
    //convert from wchar to char
    const wchar_t* wstr = s.c_str();
    size_t wlen = wcslen(wstr) + 1;
    char newchar[100];
    size_t convertedChars = 0;
    wcstombs_s(&convertedChars, newchar, wlen, wstr, _TRUNCATE);
    

提交回复
热议问题