Making application run only through launcher [duplicate] - c#

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.

Related

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();
}

How to open another program from my first in C#?

I want to open a second program from my first program and still be able to work on my first program. And another thing is to close the second program from my first. Is there a way to do this?
For running the program:
You need using System.Diagnostics;
Process open_exe(string path)
{
Process to_open;
to_open.StartInfo.FileName = path;
to_open.Start();
return (to_open);
}
For closing the program:
void close_exe(Process p, bool force = false)
{
if(force)
{
p.Kill();
}
else
{
p.CloseMainWindow();
}
}
When calling open_exe, it returns a Process, which you can use in the close_exe function as the argument.
Addition: on the close_exe function, you can either call it by:
close_exe(process);
This will use the default value for force as false, and will not force it to close
close_exe(process, true);
This will not use the default value for force, and use true, and will thus force it to close
You can do it just for one line (with "using System.Diagnostics")
Process.Start(pathToAnotherProgramm);
As to launching another exe-file see the following:
codeproject, stackoverflow
As to closing the application I would say that the way to close the app depends on your needs.
Two ways come to my mind immediately.
The first is to ungracefully kill the exe as a process. You can kill
any exe either by id or it's exe-name.
The second one is to set up a
communication between two processes with the help of WCF for
interprocess communication.
You can easily google about killing processes in C# as well as about setting up a communication between two processes.

To whom static int Main return value?

I have written a program by changing void to int
class Program
{
static int Main(string[] args)
{
return -1;
}
}
To whom this value will get return ...is it CLR? If so, how... because a called function can return a value to a calling function.
If this is the called function, then who is the calling function..is it CLR?
How it is happening ?
Does CLR returns any value to someone, e.g. OS?
If this is the called function, then who is the calling function..is
it CLR? How it is happening ?
The Main method is called by the CLR. How is this happpening?
Initially, windows examines the exe's file's header to determine whether to create a 32-bit process or a 64-bit process, or a WoW64 process.
Then windows loads the x86, x64, or IA64 version of MSCorEE.dll into process's address space.
After this, the process's primary thread calls a method defined inside MSCorEE.dll. This method initializes the CLR, loads the exe assembly, and then calls its entry point method Main.
The method Main can return nothing, void or and int. If we choose to return an int, then usually we select to return 0, at the end of Main, which means that the execution of the Program was successful. If Main return another negative int, then we have an error. Both of them are conventions that are used widely.
That being said, it is clear that the value that Main returns is returned to the process's primary thread.
Update
How we could read this value?
We could build a batch file that will execute our executable. (I have given it the name ExecutableName, you should change it correspondingly.) We can read the value that Main returns using the %ERRORLEVEL%. If it is not 0, then the message Failed with error code with the corresponding error value will be printed to the screen.
#echo off
ExecutableName.exe
IF NOT %ERRORLEVEL% == 0 goto error
echo OK
goto end
:error
echo Failed with error code %ERRORLEVEL%.
:end
If you compile this piece of code it will produce a .exe program.
This is called by the operating system.
The return value will be passed back to the operating system when your program terminates.
You can use this return value for example in a batch script (*.bat) by observing %ERRORLEVEL%
If you start your .exe from another .Net program using the Process class, you'll find this return value in Process.ExitCode after your .exe has finished.

Not receiving arguments from batch script

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.

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