MosheMaurice
MosheMaurice

Reputation: 41

How to convert a string to LPTSTR

I need to create a variable to use in the CreateProcess:

CreateProcess(z7Cmdline, z7Arg, NULL, NULL, FALSE, NULL, NULL, NULL, &startInfo, &processInfo);

The variable z7Arg is a argument list for 7 -zip which contains a file name based on the current date ie: 2017-12-13.zip.

string buArg = "-o""c:\\moshe"" a " + buDir + buFileName + "c:\\moshe\\*.pdf";

I want to copy buArg into z7Arg as a LPTSTR to use in the CreateProcess routine

How do I go about it?

I am new at coding in C++, 30 years ago I programed in IBM Fortran & Assembly language for Grumman Aerospace, but have done little coding since then.

Upvotes: 4

Views: 4851

Answers (3)

Saleh Shekari
Saleh Shekari

Reputation: 41

string buArg = "-o""c:\\moshe"" a " + buDir + buFileName + "c:\\moshe\\*.pdf";
LPTSTR lpt = new TCHAR[31];
lpt = (LPTSTR) buArg.c_str();

tested by MinGW 6.3.0

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 597051

When compiling for Unicode, TCHAR-based APIs map to wchar_t-based functions, and when compiling for ANSI/MCBS, they map to char-based functions instead.

So, in your case, the TCHAR-based CreateProcess() macro maps to either CreateProcessA() taking char* strings, or CreateProcessW() taking wchar_t* strings.

std::string operates with char only, and std::wstring operates with wchar_t only.

So, the simplest solution to your issue is to just call CreateProcessA() directly, eg:

std::string z7Cmdline = ...;
std::string z7Arg = ...;
STARTUPINFOA si = {};
... 
CreateProcessA(z7Cmdline.c_str(), & z7Arg[0], ..., &si, ...);

If you want to call CreateProcessW(), use std::wstring instead:

std::wstring z7Cmdline = ;
std::wstring z7Arg = ...;
STARTUPINFOW si = {};
... 
CreateProcessW(z7Cmdline.c_str(), & z7Arg[0], ..., &si, ...);

In this latter case, if your input must be std:string, then you have to use a runtime conversion, via MultiByteToWideChar(), std::wstring_convert, etc.

Or, you could use std::basic_string<TCHAR> instead:

std::basic_string<TCHAR> z7Cmdline = ;
std::basic_string<TCHAR> z7Arg = ...;
STARTUPINFO si = {};
... 
CreateProcess(z7Cmdline.c_str(), & z7Arg[0], ..., &si, ...);

Upvotes: 3

Paul
Paul

Reputation: 13238

You have to do 2 things:

  1. Convert const char* to const TCHAR*, where TCHAR may be either char or wchar_t depending on whether Unicode is enabled for your project.
  2. Remove const because CreateProcess requires TCHAR*, not const TCHAR*. You can't just use const_cast, you need a writable buffer that you'll pass to CreateProcess.

For that you may use convenient string conversion macros from ATL or MFC. Use it the following way:

CA2T param(buArg.c_str());
CreateProcess(..., param, ...);

or just

CreateProcess(..., CA2T(buArg.c_str()), ...);

Read more about string conversion macros here.

If you don't have access to ATL or MFC in your project and you have Unicode enabled, you'll need to manually convert char* to wchar_t* using MultibyteToWideChar.

Upvotes: 7

Related Questions