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

前端 未结 4 1530
情话喂你
情话喂你 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:13

    Use %hs or %hS instead of %s. That will force the parameters to be interpretted as char* in both Ansi and Unicode versions of printf()-style functions, ie:

    inline void LogToFile(const _TCHAR *szFmt, ...)
    {  
      va_list args;
      TCHAR szBuf[BUFFER_MED_SIZE];
    
      va_start(args, szFmt);
      _vstprintf_s(szBuf, BUFFER_MED_SIZE, szFmt, args);
      va_end(args);
    }  
    
    {
      char *foo = "foo"; 
      char *bar = "bar"; 
      LogToFile(_T("Test %hs %hs"), foo, bar); 
    }
    

提交回复
热议问题