I wrote a main program in C#, and I worte also a small tool program also in C#.
I want that the tool program will be able to execute under some conditions:
- When called from my main program.
- When called from open some suffix of file (e.g. “*.abc”)
But if the user open my program directory he can run the tool program, and I do not want him to be able to do it. I want him to be able to run the program under the conditions above. And if he ran the program manually, the program automatically shut.
Is there any way to do that?
To check if your main program called it, you could pass the main program’s
ProcessIDas a commandline argument, then in your small program, check if thatProcessIDexists and if its process name is the name of your main program. This isn’t spoof-proof, but might be a bit trickier to fake than just passing a static number/string.In addition, you could encrypt the number and pass that, then decrypt it and check the above. It’s pretty much impossible to prevent a determined hacker from running your program on its own, but you can raise the bar of how tricky it is to do it. You’d also want to obfuscate your code, otherwise a quick Reflector call will show exactly what characters are being passed.
Alternatively, if possible, you could just make the small program a DLL and call it from your main program like that. This would need a bit of refactoring, but would force your program to be open. As for opening a
*.abcfile, your program can check the command line arguments to see if a filename was passed through. This can then be processed automatically by your app and the DLL calls can be made.