C# - Detect if application has been launched by another program - c#

I've made a Launcher for my game, and the launcher auto-download and patches the game.
But, how can I check if the game has been launched by the Launcher? Is there a way to do it without using Command Line Arguments?

If your launcher is .net application, you can start your application not from command line, but by calling Main() function (from exported class).
If your launcher is not .net application and your application works on Windows only, then you can try to find launcher's window (using FindWindow function) from main application, send message using SendMessage WinApi function, and check result.
These articles might be usefull:
Importing .net classes
Finding windows
Sending messages
Importing dll functions

Related

Is it possible to capture the console screen under Windows?

I want to make a GUI Windows application that can run console applications. When this happens, the console window should not be shown. Instead its content should be shown in a visual component (memo/richedit). This component should show exactly the same content which would appear in the console window, even the color of the text and its background should be displayed. So this visual component should work exactly as a console window. I know that the standard output can be captured but many console applications do not use it. Is it possible to capture the output of a console application this way? Are there Windows API calls that can handle this?
I use Delphi XE2 but C# code would also be helpful.
You have to run the console mode program with stdout redirection to a pipe that your Delphi program will create. Your Delphi program can read the pipe to get the console mode program output and do whatever it needs to. By the way, this works not only with Delphi but also with any language able to create pipe and run program with I/O redirection.
If you need Delphi code to do that, have a look at this reference.
There is a ready-to-run component on GitHub: DosCommand
The Demo shows two ways how to do what you describe.
I am not sure if it works for older versions like XE2, but at least you can give it a try.
Traditionally you would call CreateProcess with stdin/stdout set to pipes you created. This should work for most programs but not for anything that uses a ncurses style "GUI" and you also lose the color information. An example can be found on MSDN.
Windows 10 (1809?) added support for pseudoconsoles. This is used by the new Terminal application and is your best bet for full console compatibility.
The last alternative is to inject into the child process and hook WriteFile, ReadFile and all the console functions but this is ugly and error-prone.

How to send a number from a form in C# to a console program in C++

I need to run an unmanaged C++ application, this application must be connected to a library in C# (dll which when loading must show a form with a button) and wait for form close event to terminate.
While the form is loaded, at the moment of pressing the button the click event should be executed, which should execute a function that can send a number of return to the console application in C ++ and this application will show the resulting number in the output of the console.
What I need is to solve point 5. shown below, I read a lot of documents but almost all refer to the interoperation between a C++ dll and a C# application and not the reverse (Return an integer value from C# dll to a C++ console application) is what is needed here.
The execution must be dynamic, that is, while the form is displayed, a communication should be established from a function of the C# library and a function of the parent C++ console application.
All the examples that I have been able to find do almost the same, load the libraries, establish the communication and, from the C++ console application they call a function of the C# library and show the resulting return value and the application ends, but no example shows how to load the C# library, show the form, press a button, and send a integer number as a result to the C++ parent console application and show the result in the output of the console.
If you have any idea how to implement the Callback, I would be very grateful.
What I have tried:
What has been achieved is:
Load C++ console application (unmanaged), achieved
Load a C# library from the C++ console application, achieved
Import and communicate with some functions exported from the C# library, achieved
When loading the C# library, from this library a form with an button is shown, achieved
While the form is displayed, press the button and execute a function in C# that returns an integer to the C++ console application and print the result in the output of the console from the C++ console application, pending
One proposition: When your app C# want to send a value, it writes it in a file in folder C:\Temps\token.txt.
Then, if your application C++ detects a file named token.txt (by a loop for each 10s for example) in C:\Temps, it reads it and gets the value
There are 2 solution not related to network or interprocess communications at least
call unmanaged callback or delegate from managed code (see "How to: Marshal Callbacks and Delegates By Using C++ Interop")
use an event object to signal pressed button and a named shared memory to pass a value back
Please note that implementing complex things to doing simple ones is the characteristic of not well designed application.

Running .Net application from command prompt window

I have a .Net windows C# application, fairly complete and working, that I have been asked to support calling it from another program like LabView. I have added the ability to parse command line arguments at startup so I can detect that it is supposed to behave like a console application and be provided with enough information to function.
What I would like to do is have the program print out to the console its results and have the calling program pipe it or just pull the data and use it.
The printing to the console works fine but when I start the program up, it tells the command prompt window that it is done (a new prompt immediately shows up and the command prompt is waiting for input). In the process it also closes the redirection that was part of the startup line. Is there anyway to keep it from telling the calling program that it has completed before it has actually finished?
The simple solution is to pass a file to save the data to but I would prefer not to have to do that. I could also do a separate version that is a console app, but that means supporting two separate programs.
Thanks
Instead of invoking the program directly, you can use cmd /c myapp.exe.

Supress cmd windows which open while C# program runs

I have written a C# application, which communicates with C++ dll to process some things. The C++ .dll actually has some commands system(TestApplication1) , system(TestApplication2) etc.
While I run my C# application, and send a call to C++ function, these cmd windows open and close to do their work. Since my C# application is meant for a GUI front-end, I want to supress these windows from loading up.
I remmeber in C# when we load some process, we have the option to pass ProcessWindowStyle=Hidden in the arguments, but I am not sure how it can be done in this scenario.
I wouldn't prefer to make my application full screen, make it TopMost, and Maximized. Any suggestions?

How to launch external application that keeps alive after closing the calling program in C#?

My target is to call an external program from my C# application and keep it alive even when my application is closing.
Both applications don't share anything and I could accomplish the same task by running a command from the command line by passing in a few arguments.
I've been trying to use the process class and I've been fiddling around with DTE, both let me create an instance of the external program (Visual Studio to be precise) but they will close as soon as the calling application is closing.
Is there any other way? Maybe a call to Windows, that will launch that program instead of my application?
Use Process.Start.

Categories

Resources