0

I wants to add a small additional options to a big unit, so I do not want to process a large amount of code.

TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
GetTempPath(MAX_PATH,          // length of the buffer
    lpTempPathBuffer); // buffer for path 
GetTempFileName(lpTempPathBuffer, // directory for tmp files
    TEXT("QRCode_"),     // temp file name prefix 
    0,                // create unique name 
    szTempFileName);  // buffer for name 

I want to change szTempFileName to optional wstring/std::string/wchar_t* parametr .

Solution:

  1. change TCHAR to wchar_t

  2. wcscpy(wchar_t[], wchat_t*);

Jakooop
  • 19
  • 1
  • 7
  • 1
    It makes no sense to change a `wchat_t*` to a `TCHAR` and a `TCHAR` is a single character where the `wchat_t*` can be many. Do note that a `TCHAR[]` and a `TCHAR*` are interchangeable for the most part. – NathanOliver Oct 28 '16 at 11:40
  • Do you mean you only want a single character from the wide-character string? – Some programmer dude Oct 28 '16 at 11:42
  • No my mistake, I try to convert wchar_t* to TCHAR[] wchar_t* w = L"somewchart"; TCHAR t = .... – Jakooop Oct 28 '16 at 11:44

2 Answers2

0

Typically, there's no reason to use TCHARs at all. If you're working with wchar_t anyway, simply call the Unicode variants of the Winapi functions directly by adding a W suffix:

// Use wchar_t instead of TCHAR.
wchar_t szTempFileName[MAX_PATH];
wchar_t lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
// Call GetTempPathW directly.
GetTempPathW(MAX_PATH, lpTempPathBuffer);
// Use L"..." instead of TEXT("...").
GetTempFileNameW(lpTempPathBuffer, L"QRCode_", 0, szTempFileName);
nwellnhof
  • 32,319
  • 7
  • 89
  • 113
0

Stick with what you have and convert to the appropriate string type at the end. Getting windows to play nicely and correctly with the C++ string types will probably turn your hair grey.

Use the following typedef to create your string type that you can use throughout your code instead of std::string or std::wstring:

typedef std::basic_string<TCHAR> tstring;

Construct your string from the buffer directly:

tstring RealFileName(szTempFileName);
IanM_Matrix1
  • 1,564
  • 10
  • 8