Not receiving arguments from batch script - c#

I've created a simple program for moving files around in a system running win 7 embedded. I've run into a strange "bug" with my software and the way it handles startups.
static void Main(string[] args)
{
if (Flagger.GetFlag().Contains("Processing") || args.Contains("batch"))
{
Run();
}
return;
}
The way I've chosen to handle the different ways this program will be executed, I've created a simple way of seeing whether it is being executed as a part of the systems startup procedure, or called by a batch file.
The batch file is meant to be called by a trigger in the SQL-server and run a handful of programs for logging and such. While testing this on my workstation it executes and passes the parameters like it is supposed to do, but in the embedded system, no parameters is given to the program through the batch file.
start Pack.exe -batch
exit
I have tried several different methods of writing the batch file(with/without citation marks, start-exit) but to no avail. What could be causing the batch file to not pass the arguments to the Packer?

Starts a separate window to run a specified program or command.
START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/WAIT] [/B] [command/program]
[parameters]
Could it be, that you're passing qouted string as the first parameter to start command? If so, it handles it like a window title. Compare
start "c:\windows\notepad.exe"
and this
start "test" "c:\windows\notepad.exe"
Then you should just add a title param to start program. Also consider to use cmd /C instead.

Related

Making application run only through launcher [duplicate]

I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.

C# .exe to .dll / execute or run .dll as if it were a .exe

Okay so for example I have 2 projects, Form1.exe & console1.exe, what I want is to execute console1.exe within form1.exe, so I used,
if (isLogin(true))
{
MessageBox.Show($"Welcome!");
Process.Start("console1.exe");
Now the problem with this is, in order to execute console1.exe the user must store that file locally, now the issue with that is, he/she can execute console1.exe without executing form1.exe
So what I did was used ILMerge to merge the .exe's as you would a .dll but it's unable to now find console1.exe, so merging didn't work.
So now my conclusion was to reopen visual studio & create a reference .dll out of console1.exe.
Problem with this is executing the .dll as if it were still a .exe without calling methods separately, so to execute the file as if it were one method.
So basically executing the parent class, which will then follow up with the sub classes.
Now is there anyway I can reference console1.exe and merge it to form1.exe preventing users from running it as a solo application, or is it possible to set rules on console1.exe that it may not execute unless called from form1.exe, or the second conclusion to run it as a .dll reference library in one command not multiple sub classes.
You have two options if you need to start it as a process. Both of which are not fool proof because they use a simple technique of obfuscation.
You change the file extension of the console1.exe to something like console1.notAnExe. You can still start this as an application using the Process.Start() method. You just need to create a ProcessStartInfo object and set the UseShellExecute property to false. Beware because this option means anyone with access to change the extension back to .exe can run the application.
private static void StartAProcess(string proc)
{
var ps = new ProcessStartInfo(proc);
ps.UseShellExecute = false;
var p = Process.Start(ps);
p.WaitForExit();
}
You use a simple technique within your console1.exe start up method Main(string[] args) that reads in a special command line parameter. If this command line parameter exists, you continue processing. If the command line parameter does not exists, you just return. Beware because anyone that knows the command line parameter can still start your console1.exe and pass that command line parameter within a shortcut.
static void Main(string[] args)
{
if (args == null ||
args.Length <= 0 ||
!args[0].Equals(
"/SpecialParameter",
StringComparison.OrdinalIgnoreCase))
{
return;
}
// Do something if the /SpecialParameter exists.....
}
private static void StartAProcess(string proc)
{
var ps = new ProcessStartInfo(proc);
ps.UseShellExecute = false;
ps.Arguments = "/SpecialParameter";
var p = Process.Start(ps);
p.WaitForExit();
}

C# monitor external process state

I am making a launcher app in C# on windows. However the process isn't directly started by my C# application but it uses a url to start it e.g "steam://rungameid/xxxxxxx"
I need it to monitor a process by name (say XYZ.exe) in the following fashion:
Receive an event when XYZ.exe starts
Receive an event when XYZ.exe exits
I just want to minimise and restore the my C# application's form when the application is running and not running respectively
thanks
Make a timer (with your preferred timer method) and poll every 'n' milliseconds (find what's best for you... I'd say for minimizing/restoring from a game, 500 milliseconds could be a good start, but experiment), then you can use something like:
bool processRunning = false;
void timerTickMethod()
{
var procIsRunning = Process.GetProcessesByName("xyz.exe").Any();
if(procIsRunning && !processRunning)
ProcessIsStartedEvent(); // or directly minimize your app
else if(!procIsRuning && processRunning)
ProcessIsEndedEvent(); // or directly restore your app
processRunning = procIsRunning;
}
If you want to make sure it's your xyz.exe that is running, you can pass in the full path to GetProcessesByName (so that if there's other xyz.exe in your system, it won't confuse your app)
Update
I was writing from memory, so maybe GetProcessesByName only work for friendly names (with no exe, or path).
If that's the case (I haven't tried), and you need the full path you could do it like:
var procIsRunning = Process.GetProcesses().Any(x => x.MainModule.Filename == #"c:\your\full\path.exe");

how to debug multiple processes in c#?

I have a c# program1 that starts up another c# program2.exe and executes methods in it. I can't figure out how to debug it. Here is the code from the calling program1:
Task = new Process () ;
Task.StartInfo.FileName = Executable ;
Task.StartInfo.Arguments = AddQuotes (Msg) ;
if (GeneralRegistry.GetWaitForExit ())
{ State.WaitForExit () ;
}
bool OK = Task.Start () ;
Task.StartInfo.Filename is the name of the program2.exe that is executed.
Task.StartInfo.Arguments is xml that includes the name of another xml "model" file. The model defines variables, equations, and data. Program2.exe parses the xml model and executes it.
I'm using VS12 Express. I tried Debug>Attach to Process and attached to Program2.exe after it starts up. I click the "Go" menu item in Program2.exe to start the analysis. Now what? How do I set breakpoints and step through Program2.exe? I do not have the source code for it.
However, I do have source code for Program3. That program is somehow embedded in Program2.exe. Program2.exe has the UI and inputs but the core underlying work is done by Program3.
If both these programs are in the same solution, you can attach the exe to the debugger and step into program2 dll from program 1. Do you initialize program 2's object in program 1?

C# : Making an exe to not run directly

I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.

Categories

Resources