Run more of one command in ShellExecute function (C++) - c++

I'm trying to run the following lines:
hRet = ShellExecute(HWND_DESKTOP,
L"open",
(*) L"C:\\...\\wmplayer.exe C:\\...\\.mp4",
NULL,
NULL,
SW_SHOW);
But the file(.mp4) doesn't open, maybe because I give the two pathes in one parameter.
I tried also to run the following:
(*) L"\"C:\\...\\wmplayer.exe\" \"C:\\...\\.mp4\""
And:
(*) L"C:\\...\\wmplayer.exe\" \"C:\\...\\.mp4"
But to no avail, can you please assist??
Thanks...!

Reading the documentation I'd say the call should be
hRet = ShellExecute(HWND_DESKTOP,
L"open",
L"C:\\...\\wmplayer.exe",
L"C:\\...\\.mp4",
NULL,
SW_SHOW);
The documentation says:
lpParameters [in, optional] Type: LPCTSTR If lpFile specifies an
executable file, this parameter is a pointer to a null-terminated
string that specifies the parameters to be passed to the application.
The format of this string is determined by the verb that is to be
invoked. If lpFile specifies a document file, lpParameters should be
NULL.
so you could pass directly the .mp4 file as the lpFile parameter and leave this NULL (but the default player would be used) or you must pass the player name as file and the movie name as parameter.

Related

C/C++ CreateFileA fails

I am trying to make an image viewer, and everything works when I hard code the path of the file, but when I try to get the path so I can open files with it, it doesn't work, nothing happens.
When debugging, I found out that CreateFileA returns INVALID_HANDLE_VALUE.
This leads me to believe that the error should be in these 2 lines of code.
LPSTR FileA = GetCommandLine();
HANDLE FileHandle = CreateFileA(FileA, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
When CreateFileA() returns INVALID_HANDLE_VALUE on failure, use GetLastError() to find out WHY it failed. But in this case, GetCommandLine() is likely not returning the path you are expecting.
Assuming that is actually calling the Win32 GetCommandLineA() function, and not some other function in your program, then that function returns the command line that was used to launch the calling process, ie "C:\<path>\myapp.exe <parameters>". If there are no <parameters> present, then you would be trying to open your own program's EXE, not an image file. And if there are <parameters> present, then you would be passing an invalid file path to CreateFileA().
You need to parse the command line to extract just the file path you actually want, THEN you can try to open the file at that path. Have a look at Parsing C Command-Line Arguments and Parsing C++ Command-Line Arguments . Consider using CommandLineToArgvW() to make that easier.

Do I need to specify an exe path as the 1st parameter in lpCommandLine when calling CreateProcessAsUser?

I can't seem to find a definitive answer to this. My goal is to start a process using a user token. Say, the process in question is started as such:
"C:\My folder\My proc.exe" param=1
So when I specify lpCommandLine parameter for the CreateProcessAsUser API, do I need to specify executable path as the 1st parameter as such:
LPCTSTR pStrExePath = L"C:\\My folder\\My proc.exe";
TCHAR buffCmdLine[MAX_PATH];
if(SUCCEEDED(::StringCchPrintf(buffCmdLine, MAX_PATH,
L"\"%s\" %s", pStrExePath, L"param=1")))
bResult = CreateProcessAsUser(
hToken, // client's access token
pStrExePath, // file to execute
buffCmdLine, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, // creation flags
envBlock, // pointer to new environment block
NULL, // name of current directory
&si, // pointer to STARTUPINFO structure
&pi // receives information about new process
);
Or can I omit the exe path and do this?
LPCTSTR pStrExePath = L"C:\\My folder\\My proc.exe";
TCHAR buffCmdLine[MAX_PATH];
if(SUCCEEDED(::StringCchCopy(buffCmdLine, MAX_PATH, L"param=1")))
bResult = CreateProcessAsUser(
hToken, // client's access token
pStrExePath, // file to execute
buffCmdLine, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, // creation flags
envBlock, // pointer to new environment block
NULL, // name of current directory
&si, // pointer to STARTUPINFO structure
&pi // receives information about new process
);
They both seem to work.
Reading the documentation, both cases should work.
From MSDN
If both lpApplicationName and lpCommandLine are non-NULL,
*lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to
retrieve the entire command line. Console processes written in C can
use the argc and argv arguments to parse the command line. Because
argv[0] is the module name, C programmers generally repeat the module
name as the first token in the command line.
I agree that the documentation may be clearer saying that it accepts the argument part of the command line or the full command line in lpCommandLine when lpApplicationName is non-NULL.
UPDATE :
The documentation is better in the case lpApplicationName is NULL
If lpApplicationName is NULL, the first white space–delimited token of the command line specifies the module name...
UPDATE 2 :
There is a nice documentation about these arguments Understanding CreateProcess and Command-line Arguments.
Reading this documentation, I understand that there is a difference between your two cases. When you provide lpApplicationName and arguments in lpCommandLine the child process will parse the command line as it is in lpCommandLine. So if you do not duplicate the exe path, argv[0] will not represent the exe path as usual but param=1.

C++ Winapi CreateProcess issue with command line

I'm using CreateProcess this way:
resultCreate = CreateProcess(Command, CommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
//"Command" contains the executable file to execute
//"CommandLine" contains the parameters to pass to that executable
The parameters are the following:
Param1: "C:\Users\myuser\Desktop\file.dll"
Param2: "file" (module name)
Param3: " " (blank)
So the full CommandLine string would be:
"C:\Users\myuser\Desktop\file.dll" file " "
CreateProcess runs the executable successfully and applies the first two parameters, but when reaches the third one, it throws error
The specified process could not be found.
Function " " could not be called, due to " " doesn't exist in the DLL "(null)"
How can I pass the desired parameters correctly?
When both lpApplicationName and lpCommandLine are used, you need to include the executable path as the first parameter of your lpCommandLine value, even though you are specifying the executable path in the lpApplication value. The lpCommandLine is passed as-is to the spawned process, and most RTLs (especially C-based RTLs, but others as well) expect the first command-line parameter to be the executable path since that is how command-line consoles operate. This is even mentioned in the CreateProcess() documentation:
If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. Console processes written in C can use the argc and argv arguments to parse the command line. Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line.
This is also covered in MSDN Support:
INFO: Understanding CreateProcess and Command-line Arguments
Behavior of CreateProcess() When Creating a 32-bit Process
Case 1:
If the ApplicationName parameter is passed and the CommandLine parameter is NULL, then the ApplicationName parameter is also used as the CommandLine. This does not mean that you can pass additional command-line parameters in ApplicationName string. For example, the following call will fail with a "File Not Found" error:
CreateProcess( "c:\\MyApp.exe Param1 Param2", NULL, ... )
Case 2:
On the other hand, if the CommandLine parameter is non-NULL and the ApplicationName parameter is NULL, then the API attempts to extract the application name from the CommandLine parameter.
Case 3:
The flexibility of the CreateProcess() function (and a possible point of confusion) arises when you pass a valid string pointer to both the ApplicationName and CommandLine parameters. This allows you to specify the application to be executed as well as the complete command line that is passed to the application. One might assume that the command line passed to the created application is a composite of the ApplicationName and CommandLine parameters, but this is not the case. As a result, a process created by CreateProcess can receive a value other than its .exe name as its "argv[0]" parameter. The following is an example of a call to CreateProcess that produces this "abnormal" behavior:
CreateProcess( "c:\\MyApp.exe", "Param1 Param2 Param3", ...)
MyApp's arguments will be as follow:
argv[0] == "Param1"
argv[1] == "Param2"
argv[2] == "Param3"
NOTE: ANSI specifications require that argv[0] should be equal to the application name, but CreateProcess gives the calling application the flexibility to override this rule for 32-bit processes.
Case #3 applies to your situation.
Use something like this instead:
std::string Command = "<exe path>";
std::string CommandLine = "\"" + Command + "\" <parameters>";
// std::string::c_str() returns a const pointer. The first parameter
// of CreateProcessA() is const, but the second parameter must be a
// non-const pointer to writable memory, because CreateProcessW() can
// modify the data...
//
resultCreate = CreateProcessA(Command.c_str(), &CommandLine[0], ...);
std::wstring Command = L"<exe path>";
std::wstring CommandLine = L"\"" + Command + L"\" <parameters>";
// std::wstring::c_str() returns a const pointer. The first parameter
// of CreateProcessW() is const, but the second parameter must be a
// non-const pointer to writable memory, because CreateProcessW() can
// modify the data...
//
resultCreate = CreateProcessW(Command.c_str(), &CommandLine[0], ...);
Alternatively, omit lpApplicationName and specify the complete command line to lpCommandLine only:
std::string CommandLine = "\"<exe path>\" <parameters>";
resultCreate = CreateProcessA(NULL, &CommandLine[0], ...);
std::wstring CommandLine = L"\"<exe path>\" <parameters>";
resultCreate = CreateProcessW(NULL, &CommandLine[0], ...);

How to include argument with CreateProcess API in VC++?

I have called the other application from my main application using createprocess API. But the other process also need some arguments as a parameter.
I created the process as:
BOOL ret= CreateProcess( NULL, szCmdline, NULL, NULL, TRUE, 0, NULL, NULL,&siStartInfo, &piProcInfo);
szCmdline is variable which contians the application full path.
Any idea how to pass the argument with this process.
Thanks,
CreateProcess has both lpApplicationName and szCommandLine arguments. You must pass at least one of the arguments. However you should pass both for security reasons.
lpApplicationName is the name of the executable you wish to run.
szCommandLine is the command line you wish to pass to that executable. It should include the executable as the first item. This will be received by the application as an argument to WinMain or retrieved by the GetCommandLine function (though the system may prepend a fully-qualified path if one is not supplied). For C programs using main or wmain, it will be parsed by the CRT into arguments.
If you pass NULL for lpApplicationName, the system will attempt to locate the executable in szCommandLine, and will use that.
If you pass NULL for szCommandLine, the system will use lpApplicationName for both.
So the command line is the command line. If you have arguments to pass to the command, put them on the command line.
If lpApplicationName is NULL, the first white space–delimited token of
the command line specifies the module name. If you are using a long
file name that contains a space, use quoted strings to indicate where
the file name ends and the arguments begin (see the explanation for
the lpApplicationName parameter).
It is preferable to pass both lpApplicationName and szCommandLine, to ensure that the command line is not misinterpreted by the system and the wrong executable is run. (There was a class of security problems caused by this a few years ago).
Also, when passing both lpApplicationName and szCommandLine, remember that szCommandLine still needs to include the application name as the first argument.
So for instance, if your program is C:\Program Files\My Application\Program.exe and the arguments are /the /arguments, you would set lpApplicationName to "C:\Program Files\My Application\Program.exe", and set szCmdline to "C:\Program Files\My Application\Program.exe" /the /arguments.
What were the security concerns?
Well imagine if someone created a file "C:\Program Files\My.exe". If you omit the quotes, the system interpreted C:\Program Files\My Application\Program.exe /the /arguments as: C:\Program Files\My.exe Application\Program.exe /the /arguments. And you will get a surprise. This type of trick can be used to fool administrators into running programs they did not wish to run, which is a security problem. This does not occur if you pass the lpApplicationName argument.
The CreateProcess function creates a new process, which runs independently of the creating process. However, for simplicity, the relationship is referred to as a parent-child relationship.
The first parameter, lpApplicationName, can be NULL, in which case the executable name must be in the white space–delimited string pointed to by lpCommandLine.
this way you can send more than one arguments in CreateProcess API.
sprintf(exePath,"Project.exe %s %s \"%s\" \"%s\" \", appName,serverid,srjPath,caseName);
if( !CreateProcess( NULL, // No module name (use command line).
exePath, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
NORMAL_PRIORITY_CLASS,// No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
when revived this command line arguments you should parsing this argument and separated by
A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").
for parsing command line argument visit this site : http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
and creatprocess API visit this :http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx
You can form a string consisting of program name + arguments like this
wstring cmd;
cmd.assign(L"\"C:\\Program Files\\MyProgram.exe\" arg1 arg2 arg3 arg4");
if(!CreateProcess(NULL,(LPWSTR)cmd.c_str(),NULL,NULL,1,0,NULL,NULL,&si,&pi))
{
return -1;
}
Use ShellExecute(Ex). The Control is much simpler, and since we have UAC the Control how to Launch a process is easier to.
With ShellExecuteEx aou also get the process handle if you Need it.
Just my 2 Cents.

CreateProcess, first parameter question

I am trying to create a process called s2.exe. However I'm having trouble figuring out what needs to be passed in as the first argument. I tried putting the name and location of where the process would be located but I get "Error 2 starting CC". What exactly should go in the first parameter? (According to MSDN it is the path to the module.)
int main()
{
PROCESS_INFORMATION po;
STARTUPINFO s;
GetStartupInfo (&s);
if(CreateProcess(L"c:/s2", NULL, NULL, NULL,
false, 0, NULL, NULL, &s, &po) == FALSE)
{
printf("Error %d starting CC\n", GetLastError());
return -1;
}
}
A value of 2 returned from GetLastError() indicates ERROR_FILE_NOT_FOUND. You need to pass in c:/s2.exe, assuming that s2.exe is actually in the c:\ drive.
Note that the MSDN documentation for the lpApplicationName parameter in CreateProcess() says:
The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.
Also note that void main() is not standard C++. However, int main() is standard C++, and allows you to return some kind of exit code. (I edited your code snippet to reflect that.)
MSDN says about the lpApplicationName of the CreateProcess function:
This parameter must include the file name extension; no default extension is assumed.
You need L"c:\s2" rather than L"c:/s2". Microsoft prefers backslashes in paths. Forward slashes are accepted in some contexts but are generally problematic in code.