C++ ShellExecute batch script won't recognize commands - c++

As the title may suggest, i am trying to open a .bat file via "ShellExecute" function.
It works with very basic .bat scripts, such as "hello world" ones, but won't work with others containing commands for running game servers.
For example, here is a batch script for a "Killing Floor 1" server:
ucc server KF-Mountainpass.rom?game=KFmod.KFGameType?VACSecured=true?MaxPlayers=6?GamePassword=genrl -log=server.log
PAUSE
Please note that this batch works perfectly when manually clicking on it, but gives the following error when opened via ShellExecute:
"ucc" is not recognized as an internal or external command, operable program or batch file.
I have also tested on other batch files for other game servers, having the same result.
So, here is a list of what i have tried so far, and did not work:
ShellExecute(NULL, "open", "cmd.exe", "C:\\windows\\system32\\cmd.exe /C C:\\Cartella_Server\\Server_KF1\\System\\KF_Server_Launcher.bat", NULL, SW_SHOW);
ShellExecute(NULL,"open", C:\\Cartella_Server\\Server_KF1\\System\\KF_Server_Launcher.bat", NULL, NULL, SW_SHOW);
ShellExecute(NULL, "open", "cmd.exe", "/C C:\\Cartella_Server\\Server_KF1\\System\\KF_Server_Launcher.bat", NULL, SW_SHOW);
Is there any solution to this?
Thanks in advance for your time :)

It is because the script is loading inside the directory of cmd.exe
Why? Because you didn't fill the working directory field.
Have a look at the 5th parameter of ShellExecute:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx

Most likely, the ucc program is in the same directory as tha launcher script. So it works when you click on it, because your current directory is one with the file, but when you run your program your current directory is different.
Solution - provide proper directory in fifth ShellExecute argument, as per https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

Related

Opening command line program with elevated rights

I am currently writing a DLL injection program (C++) which adds specified certificate to certmgr.exe.
I wrote the whole thing connected with injection (OpenProcess, etc.). When I am executing it - attached to svchost - my DLL is not loading since it seems that it doesn't have such rights as svchost has.
How can I pass rights?
In my DLL file, I am using system("certmgr.exe -add ....") and I think this might be the problem because the command line opens as a separate program (as if I opened it via start).
Use ShellExecute() using the "runas" argument, which will make it try to run as administrator
ShellExecuteA( NULL,
"runas",
"c:\\windows\\certmgr.exe",
"-add ....",
NULL,
SW_SHOWNORMAL
);

Creating a command prompt process that opens at a chosen directory

Need help here, I'm trying to create a process in c++ with the windows api, whats happening is the process is being created which is cmd.exe however I want cmd.exe to open cd'd at a certain directory i.e. root c:\, however the process is opened at the dir of the executable.
I tried passing in "cd \" as the second argument of the CreateProcess function to no avail
Here's a snippet of the code:
TCHAR program[] = TEXT("C:/Windows/System32/cmd.exe");
TCHAR command[] = TEXT("cd /");
STARTUPINFO info;
PROCESS_INFORMATION processInfo;
ZeroMemory(&info,sizeof(STARTUPINFO));
ZeroMemory(&processInfo,sizeof(PROCESS_INFORMATION));
BOOL processResult =
CreateProcess(program,
command, NULL, NULL,
TRUE, CREATE_NEW_CONSOLE,
NULL, NULL,
&info,
&processInfo);
if(!processResult){
std::cerr << "CreateProcess() failed to start program \""
<< program << "\"\n";
exit(1);
}
std::cout << "Started program \""
<< program << "\" successfully\n";
Help would be extremely appreciated!
Thanks
If you want the cd / (or any other command) to have any effect, you need to use either the /k or /c flags for the command prompt. You can look these switches up in the documentation for cmd.exe, but basically, /c runs the command and then terminates, while /k runs the command and keeps the console session open. You almost certainly want /k here.
But really, you should be specifying the directory as the working directory for the process, not executing a change-directory command.
You can do this easily by calling the ShellExecute function, as Raw N suggested. The working directory is one of its parameters. ShellExecute (or ShellExecuteEx) is easier to use than CreateProcess, and should be preferred unless you need some special low-level behavior that you can only get with CreateProcess.
This works with CreateProcess too: pass the path as the lpCurrentDirectory parameter.
Whatever you do, don't hard-code paths! Use the %comspec% environment variable on Windows NT. It would also work to just execute cmd, letting the default search paths do their job.

C++ ShellExecute is not working the same way as cmd.exe

I'm running this command
w_icrcom.exe j11 hola
from a command prompt and it is working properly, the exe is running using the arguments.
But when I'm doing the same from a C++ program the w_icrcom.exe is behaving differently. I'm getting an error saying that "abnormal program termination". This though is happening only when you pass arguments to 'w_icrcom.exe'.
It seems to me, that when I execute a command everything is fine, but when some other program is trying to execute the same command it doesn't work and I'm getting the "abnormal program execution" error.
I tried the same set up on ShellExecute calling the notepad.exe and passing arguments and it worked fine. Is there any logical explanation on this issue?
string test_var = ("j11 hola");
ShellExecute(0, "open", "C:\\Users\\PC\\Desktop\\My First\\connect\\bin\\w_icrcom.exe", test_var.c_str(), 0, SW_SHOW);
The next to last parameter of ShellExecute is the 'working directory'. It might need to be set to whatever is the current directory in the cmd prompt that is working.

How to call system() in an opened administrator program and gives it the same privileges?

I am writing a c++ application in Windows that runs as administrator. However, while calling the system() command, it seems that the command doesn't have admin privileges (can't create file in the C:\Program Files (x86)\ directory).
How can I avoid using CreateProcess ?
If you use system you can use:
system("runas /user:<admin-user> \"program.exe\"");
Or ShellExecute:
ShellExecute(hwnd, "runas", "program.exe", 0, 0, SW_SHOWNORMAL);
This Stackoverflow Question
details the CreateProcess method pretty well.

call an exe from within c++ (windows)

I'm using VS2010 and I would like to call an exe file which I've created in another directory.
I've tried the following:
int main(){
system("C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe");
return 0;
};
but I get "The system could not find the file specified" error.
I've tried to run the exe file directly from the command line, and it only works when I'm inside its directory.
Could you please tell me how can I run it from a different directory?
(I'm using win7)
Thanks,
Li.
You should try using CreateProcess Windows API funcion: http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
Try opening the file for reading, just to check that you have the path right:
char* filename = "C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe" ;
FILE* fp = fopen (filename, "rb") ; // Open for reading, binayr mode
if (fp == 0) {
printf ("Duh! File not found\n") ;
exit (0) ;
}
printf ("File found\n") ;
fclose (fp) ;
// Now try the system call, as before:
system(filename);
What happens?
You should be able to use ShellExecute like so: (adjusting the params sent to ShellExecute for your situation) http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx?ppud=4
HINSTANCE hinst = ShellExecute( NULL, _T("open"), commandLine.c_str(), additionalParams.c_str(), NULL, SW_RESTORE );
if(hinst <= (HINSTANCE)SHELLEXERROR)// see: http://msdn2.microsoft.com/en-us/library/bb762153.aspx for further info on the return values
Now given that you are using Win7, you may be having a privilege issue and you need to run at an elevated level (i.e. administrator) you can test this by opening cmd as admin and running your exe from another directory
and as Steve mentioned above you can certainly use CreateProcess.
HTH,
EB
System() may not be able to find cmd.exe to open your environment.
Try using cmd.exe to execute your app via the /C option.
System("C:\\WINDOWS\\system32\cmd.exe /C \"C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe\"");
Try this using CreateProcess. Less (or at least different) environmental dependencies than using system(). At least you will get a nice Win32 error code if this still fails.
http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx
Check your path, and make sure you escape all characters: C:\\Users\Li..
Is the error from running the main program, not from launching modelExample_4pcs.exe? Try commenting out the system() call and see if you get the same error.
Your main program is not on the path when you're outside its folder...
Is modelExample_4pcs.exe trying to load another file from the current working folder, and THAT's what's generating the error?
Maybe try chdir() before the call to system().
Just change to the directory first, like you would do from the command prompt:
system("C: && CD \\Users\\Li\\Desktop\\Debug\\ && modelExample_4pcs.exe");