What is the simplest way to convert char[] to/from tchar[] in C/C++(ms)?
MultiByteToWideChar but also see "A few of the gotchas of MultiByteToWideChar".
Converting _TCHAR* to char*
The quickest solution is to just change the signature to the standard one. Replace:
int _tmain( int argc, _TCHAR* argv[] )
With
int main( int argc, char *argv[] )
This does mean on Windows that the command line arguments get converted to the system's locale encoding and since Windows doesn't support UTF-8 here not everything converts correctly. However unless you actually need internationalization then it may not be worth your time to do anything more.
TCHAR* to char*
Use _tremove instead of remove. It works on const TCHAR*.
Am I converting properly from const char * to TCHAR*?
(TCHAR*)"Process.exe"
is not a valid type-cast. It will "work" when the project charset is set to ANSI/MBCS, but it will produce garbage if the charset is set to Unicode.
Using TEXT("Process.exe")
is the correct way to make a string literal use TCHAR
characters.
GetBaseAddressByName(aProcs[i], TEXT("Process.exe"));
However, you need to change your pN
parameter to const TCHAR *
(or LPCTSTR
) instead:
void GetBaseAddressByName(DWORD pID, const TCHAR *pN);
void GetBaseAddressByName(DWORD pID, LPCTSTR pN);
A string literal is const data, and you cannot pass a pointer-to-const-data where a pointer-to-non-const-data is expected (without casting the const
away with const_cast
). That is why you were still getting errors when trying to use the TEXT()
/_T()
macros.
How concatenate a char with TCHAR array?
gen_random
should get char array with at least 11 characters (10 for size + 1 for terminating null).
So it should be:
char str[10+1]; //or char str[11];
gen_random(str, 10);
in addition, the format string should be: "%s\\%hs"
, the first is TCHAR*
type (if UNICODE defined wchar_t*
if not char*
) the second is always char*
type.
hs, hS
String. This value is always interpreted as type LPSTR, even
when the calling application defines Unicode.
look here
Note: in Microsoft documentation:
LPSTR
= alwayschar*
LPWSTR
= alwayswchar_t*
LPTSTR
=TCHAR*
(if UNICODE defined:wchar_t*
, else:char*
)
Converting TCHAR to a char* for strstr function
Instead of converting to char *
to use in strstr
, you should use the TCHAR
-equivalent, _tcsstr
; it'll compile to the correct call to either strstr
or wcsstr
.
Related Topics
How to Determine If Returned Pointer Is on the Stack or Heap
What Does Exactly the Warning Mean About Hidden Symbol Being Referenced by Dso
Equivalent of Console.Readline() in C++
Local Type as Template Arguments in C++
Openssl::Ssl_Library_Init() Memory Leak
How to Open a Folder in %Appdata% with C++
Win32 C/C++ Load Image from Memory Buffer
Fine Tuning Hough Line Function Parameters Opencv
Is Clrscr(); a Function in C++
Video Processing with Opencv in iOS Swift Project
C++ Dynamic Array Initialization with Declaration
Is It Illegal to Invoke a Std::Function<Void(Args...)> Under the Standard