0

I never try coding in C for the last four or five years so I don't know how to solve this problem.

My code is like this:

char szExePath[MAX_PATH]; //
//"C:\\Program Files (x86)\\Application Verifier\\vrfauto.dll"
printf("Please input the execution file path:\n");
scanf("%s", szExePath);
//char LPCWSTR
WCHAR wsz[64];
swprintf(wsz, L"%S", szExePath);
LPCWSTR m_szFilename = wsz;

setlocale(LC_ALL, "chs");
_tprintf(m_szFilename);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • this is C, not C++ – CinCout Dec 11 '19 at 05:53
  • 1
    Welcome to Stack Overflow _ As suggested by @CinCout, you should tag your past properly with 'C'. Also, I just Googled the title of your question and found 4-5 SO answers that already exist... You should do the same, in case one of them helps you. – inputforcolor Dec 11 '19 at 06:04

1 Answers1

0

Depending on your project configuration, _tprintf() maps to either printf() when TCHAR maps to char, or maps to wprintf() for wchar_t. Clearly your project is set to map TCHAR to char, which is why you are getting an error about passing a wchar_t* to printf().

But, the code you showed is not using TCHAR, so you should not be using _tprintf() at all. m_szFilename is explicitly a wchar_t[], so just use wprintf() directly instead:

wprintf(m_szFilename);

Also, consider using wscanf() to read the input as a wchar_t[] to begin with and not use an intermediate char[]:

WCHAR szExePath[MAX_PATH]; //
//L"C:\\Program Files (x86)\\Application Verifier\\vrfauto.dll"
printf("Please input the execution file path:\n");
wscanf(L"%259s", szExePath);

setlocale(LC_ALL, "chs");
wprintf(szExePath);

Note, however, that whether you use scanf() or wscanf(), reading stops on whitespace, so if the user enters a path like "C:\Program Files (x86)\Application Verifier\vrfauto.dll", your buffer will receive only "C:\Program. To account for that, you would need to use gets()/_getws() (or the safer gets_s()/_getws_s()) or fgets()/fgetws() instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770