0

I got a code from collegaue that was written a time ago, and is confusing. The code is:

TCHAR Curr_dir[100];  
char* input_file;  
DWORD a = GetCurrentDirectory(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

The problem is that Curr_dir is not the type that needed to wcstombs. Is there any other functions that can do what wcstombs does with this type of variable? Or a way to convert it?

harper
  • 13,345
  • 8
  • 56
  • 105
vered
  • 19
  • 5
  • Possible duplicate of [Errors using TCHAR,cannot convert to wchar\_t](https://stackoverflow.com/questions/21257851/errors-using-tchar-cannot-convert-to-wchar-t) - also, this seems like a C question. – kabanus Nov 19 '17 at 09:46
  • @kabanus Though, the Win32 API is a C API it is prepared for / used in C++ as well. The questioner probably writes a C++ program. Thus, let him go this time... (Btw. thanks for the link - I "liked" the accepted answer.) – Scheff's Cat Nov 19 '17 at 13:01

1 Answers1

0

It looks like you have a code that once supported multi-byte and UNICODE but decayed after stopped being compiled for multi-byte (no more supporting Windows 95).

This code snipped makes sense only if you have _UNICODE defined. In this case it end like this after the pre-processor:

wchar_t Curr_dir[100];  
char* input_file;  
unsigned long a = GetCurrentDirectoryW(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

In this case the GetCurrentDirectoryW returns a UINCODE (wide string) in Curr_dir and it's converted, for some whatever reason, to a multi-byte string. And the types of the chars match.

But if _UNICODE is not defined it the code changes to the following:

char Curr_dir[100];  
char* input_file;  
unsigned long a = GetCurrentDirectoryA(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

And GetCurrentDirectoryA now switches to the ANSI version of the API it makes no sense to call wcstombs anymore.

Usually the MDSN documentation has a section with a table with all versions of a string function under Generic-Text Routine Mappings ( for example strcmp-wcscmp-mbscmp) There is no '_t' version of the wcstombs so you need to use #ifdef _UNICODE.

But even if the the multi-byte version of the Win32 API may be still supported it makes no sense to keep using them.

More: https://www.codeproject.com/articles/76252/what-are-tchar-wchar-lpstr-lpwstr-lpctstr-etc

Mihayl
  • 3,821
  • 2
  • 13
  • 32