Using wide strings with ifstream::open or multibyte strings with CreateProcess - c++

I have a piece of code in which I need to use a string with both ifstream::open and CreateProcess, something like
//in another file
const char* FILENAME = "C:\\...blah blah\\filename.bat";
// in main app
std::ifstream is;
is.open(FILENAME);
// ...do some writing
is.close();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
std::string cmdLine = "/c " + FILENAME;
if( !CreateProcess( "c:\\Windows\\system32\\cmd.exe",
cmdLine.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) )
{
return GetLastError();
}
CreateProcess requires a LPCWSTR, so to use the string with CreateProcess I would need to declare the filename and 'cmdLine' as std::wstring, but ifstream::open doesn't take wide strings...I can't figure a way to get around this. I always seem to run into problems with unicode vs multibyte strings.
Any ideas?
Thanks.

I'm assuming you defined UNICODE. You can change STARTUPINFO to STARTUPINFOA and CreateProcess to CreateProcessA and it should work fine (it did for me).
I don't think it'll like the + operation though. Explicitly convert one char array to a string.
std::string cmdLine = (std::string)"/c " + FILENAME;
Finally, you're going to need quotes around the beginning and end of FILENAME if it has a space.
const char FILENAME = "\"C:\\Program Files\\Company\\Program\\program.exe\"";
^ ^ ^
Edit:
Try putting this below your string declaration:
char charCmdLine [MAX_PATH + 3]; //"/c " is 3 extra chars
strncpy (charCmdLine, cmdLine.c_str(), MAX_PATH + 3);
Then, use charCmdLine in CreateProcess instead of cmdLine.c_str().

Related

TCHAR Array to a concatenate LPCSTR

I am reading a ini file and want to execute a external program (VBS File) after that. But I am having problems with the string types.
This is my code.
LPCTSTR path = _T(".\\my.ini");
TCHAR fileName[500];
int b = GetPrivateProfileString(_T("Paths"), _T("filename"), _T(""), fileName, 500, path);
// fileName = myscript.vbs
// I need to execute "wscript myscript.vbs arg1"
// Execute script file. Doesnt work.
WinExec("wscript " + fileName + " arg1", MINIMZIED);
// OR. Doesnt work.
system("wscript " + fileName + " arg1");
This doesnt work. WinExec wants a LPCSTR but I have the fileName in a TCHAR[] and want to concatenate with some other string.
How can I convert or concatenate it correctly?
From the WinExec() documentation:
This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.
Which is CreateProcessW() in your case.
Alternatively, you can use _wsystem().
You need to concatenate the strings using another buffer, for example:
LPCTSTR path = _T(".\\my.ini");
TCHAR fileName[500];
TCHAR command[520];
int b = GetPrivateProfileString(_T("Paths"), _T("filename"), _T(""), fileName, 500, path);
_stprintf_s(command, 520, _T("wscript %.*s arg1"), b, filename);
Then you can use command as needed, eg:
STARTUPINFO si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_MINIMIZED;
PROCESS_INFORMATION pi = {};
if (CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
Or:
#ifdef UNICODE
#define system_t(cmd) _wsystem(cmd)
#else
#define system_t(cmd) system(cmd)
#endif
system_t(command);

CreateProcess fails with error 0

I want to start a process. To do so I called the CreateProcess method like this:
wchar_t *path = (wchar_t*) malloc(sizeof(wchar_t) * 500);
const char* const_path = "C:/Windows/notepad.exe";
for (int i = 0; i < strlen(const_path); i++)
{
path[i] = const_path[i];
}
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL,
path,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
std::stringstream s;
s << "Could not start - Code " << GetLastError();
ui.processStateLabel->setText(s.str().c_str());
return;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Problem is: it gives me "Could not start - Code 0", which makes no sense, because error code 0 is successful operation and obviously (no window opened) the operation was not successful. What do I have to do to get this working?
As you already figured out, the string is not terminated. You might also want to quote the path.
You should call GetLastError before the std::stringstream object is created because you don't know what the constructor does under the hood, that will hopefully provide a better error code.
I forgot that strlen Method does not include the 0 termination, which is needed. Thus changing strlen(const_path) to strlen(const_path) + 1 in the for-loop is the solution.

How to convert std::wstring to LPCTSTR in C++?

I have Windows registry key value in wstring format. Now I want to pass it to this code (first argument - path to javaw.exe):
std::wstring somePath(L"....\\bin\\javaw.exe");
if (!CreateProcess("C:\\Program Files\\Java\\jre7\\bin\\javaw.exe", <--- here should be LPCTSTR, but I have a somePath in wstring format..
cmdline, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
printf("CreateProcess failed\n");
return 0;
}
How can I do that?
Simply use the c_str function of std::w/string.
See here:
http://www.cplusplus.com/reference/string/string/c_str/
std::wstring somePath(L"....\\bin\\javaw.exe");
if (!CreateProcess(somePath.c_str(),
cmdline, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
printf("CreateProcess failed\n");
return 0;
}
LPCTSTR is an old relic. It's a hybrid typedef that either defines char* if you are using multi-byte strings or wchar_t* if you are using Unicode. In Visual Studio, this can be changed in general project's settings under "Character Set".
If you are using Unicode, then:
std::wstring somePath(L"....\\bin\\javaw.exe");
LPCTSTR str = somePath.c_str(); // i.e. std::wstring to wchar_t*
If you are using multi-byte, then use this helper:
// wide char to multi byte:
std::string ws2s(const std::wstring& wstr)
{
int size_needed = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), 0, 0, 0, 0);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), int(wstr.length() + 1), &strTo[0], size_needed, 0, 0);
return strTo;
}
i.e. std::wstring to std::string that will contain multi-byte string and then to char*:
LPCTSTR str = ws2s(somePath).c_str();
The safest way when interacting from stdlib classes with TCHARs is to use std::basic_string<TCHAR> and surround raw strings with the TEXT() macro (since TCHAR can be narrow and wide depending on project settings).
std::basic_string<TCHAR> somePath(TEXT("....\\bin\\javaw.exe"));
Since you won't win style contests doing this ... another correct method is to use explicitly the narrow or wide version of a WinAPI function. E.g. in that particular case:
with std::string use CreateProcessA (which uses LPCSTR which is a typedef of char*)
with std::u16string or std::wstring use CreateProcessW (which uses LPCWSTR which is a typedef of wchar_t*, which is 16-bit in Windows)
In C++17, you could do:
std::filesystem::path app = "my/path/myprogram.exe";
std::string commandcall = app.filename.string() + " -myAwesomeParams";
// define si, pi
CreateProcessA(
const_cast<LPCSTR>(app.string().c_str()),
const_cast<LPSTR>(commandcall.c_str()),
nullptr, nullptr, false, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr,
&si, &pi)
Finally decided to use CreateProcessW as paulm mentioned with a little corrections - values need to be casted (otherwise I get error):
STARTUPINFOW si;
memset(&si, 0, sizeof (STARTUPINFOW));
si.cb = sizeof (STARTUPINFOW);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof (PROCESS_INFORMATION));
std::wstring cmdline(L" -jar install.jar");
if (!CreateProcessW((LPCWSTR)strKeyValue.c_str(),
(LPWSTR)cmdline.c_str(), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
0, // Set handle inheritance to FALSE.
CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
printf("CreateProcess failed\n");
return 0;
}

How to redirect in, out and err streams from process created with CreateProcess function? [duplicate]

I tried using CreateProcess to run a simple command like hg > test.txt. I tried running the string as a whole (as opposed to separating it into an application name and its parameters). Why does CreateProcess(0, "notepad.exe test.txt", ...) work but CreateProcess(0, "hg > test.txt", ...) does not?
The code below creates a console-less process with stdout and stderr redirected to the specified file.
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE h = CreateFile(_T("out.log"),
FILE_APPEND_DATA,
FILE_SHARE_WRITE | FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL ret = FALSE;
DWORD flags = CREATE_NO_WINDOW;
ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &si, sizeof(STARTUPINFO) );
si.cb = sizeof(STARTUPINFO);
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = NULL;
si.hStdError = h;
si.hStdOutput = h;
TCHAR cmd[]= TEXT("Test.exe 30");
ret = CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, NULL, NULL, &si, &pi);
if ( ret )
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
return -1;
}
You can't use stdout redirection in the command line passed to CreateProcess. To redirect stdout you need to specify a file handle for the output in the STARTUPINFO structure.
You are also making another, more subtle, mistake. The second parameter, lpCommandLine must point to writeable memory because CreateProcess overwrites the buffer. If you happen to be using the ANSI version of the function then you will get away with this, but not for the Unicode version.
The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.
Microsoft has an example how to redirect the standard output:
http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx.
CreateProcess() launches processes, it is not a command line itnerpreter. It doesn't know what ">" is and won't do the stream redirection for you. You need to open the file test.txt yourself and pass the handle to it to CreateProcess inside the STARTUPINFO structure:
CreateProcess
STARTUPINFO
you should run process cmd.exe with params "/c command line".
This will redirect the output to a file or to organize a pipeline through CreateProcess.

What should the second parameter of CreateProcess be?

I am trying to start a server using CreateProcess(). Here is the Code:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
int result;
STARTUPINFO si;
PROCESS_INFORMATION pi;
CreateProcess("C:\\AP\\DatabaseBase\\dbntsrv.exe", "*** WHAT SHOULD I PUT HERE***", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
return 0;
}
I did not understand from the documentation what the 2nd parameter should be. Can you please help me with it?
Thank You
From MSDN:
lpCommandLine [in, out, optional]
The command line to be executed. The maximum length of this string is 32,768 characters, including the Unicode terminating null
character. If lpApplicationName is NULL, the module name portion of
lpCommandLine is limited to MAX_PATH characters.
The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a
pointer to read-only memory (such as a const variable or a literal
string). If this parameter is a constant string, the function may
cause an access violation.
The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the
command line.
So NULL is OK there, at least. As soon as you don't pass arguments.
You use it to pass arguments to the .exe defined by the first parameter:
An example would be to call the cmd.exe and then run a script or use a zip utility:
void runCmd(const tstring& cmdString, STARTUPINFO &si, PROCESS_INFORMATION &pi)
{
ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
si.cb = sizeof(si);
tstring cmd_exe_path(win_dir);
cmd_exe_path.append( _T("\\System32\\") ).append(CMD_PROCESS);
tstring argline( _T("/c ") );
argline += cmdString;
tstring curr_dir( cmdString.substr( 0, cmdString.rfind( _T('.') ) ) );
curr_dir.erase( curr_dir.find_last_of( _T("/\\") ) );
size_t pos = curr_dir.find( _T("\"") );
while( pos != tstring::npos )
{
curr_dir.erase( pos, pos + 1 );
pos = curr_dir.find( _T("\"") );
}
//USE FULL PATHS FOR SAFETY... Include wrapping quotes if spaces required in path
LOG(LOG_INFO,_T("runCmd(): Calling %s %s Dir[ %s ]"),cmd_exe_path.c_str(),argline.c_str(), curr_dir.c_str());
if( !CreateProcess( cmd_exe_path.c_str(), &argline[0], NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
NULL,curr_dir.c_str(),&si,&pi ) ) //this generates warning C6335 - resource leak... however handles should be closed by owner
{
DWORD dw = GetLastError();
std::string error( "runCmd(): Failed to create Shutdown process - error code is " );
error.append(boost::lexical_cast<std::string>(dw));
LOG(LOG_INFO,error.c_str());
throw std::exception(error.c_str());
}
LOG(LOG_INFO,"runCmd(): process starting with PID[%d] TID[%d]",pi.dwProcessId,pi.dwThreadId);
}