Here is my problem, i have a currently running program that runs in the background and is only accessible via a cmd window in which you pass commands to control said program. What im trying to do is send a command from my c# winform to the cmd which then executes the command. Im basicly trying to code a gui for this program.
Cheers.
Well, it's a little more involved, but this can be done by using the Windows Api.
Which means, you need to import some native libraries via P/Invoke.
First you need to get a handle to the console window. You can do this via FindWindow (P/Invoke signature).
Next to send some key-strokes there's multiple options. The most pretty one I guess is to use SendMessage (P/Invoke signature) with the WM_KEYDOWN and WM_KEYUP messages and the VK_ key as lParam argument.
Yes it is running -- You aren't going to be able to send commands to a running app unless it's designed to accept them somehow.. It either needs to have an open port or some way to 'send the command' to the running app.
Most apps that "you can't modify" can only be called with specific parameters at startup, unless they were designed to take input at runtime.
Related
I'm not going to go into details why am trying to do this, instead of making the main application do the work. I think it's currently easier for me. But I'm not going to use this technique in the future.
In my case, the main form has a button that opens another form. In the second one for you can adjust the amount, pause, resume and stop the work of the console application (sound totally useless (and maybe stupid), but, like I said, I'm not going to go into details why). This means that the application must have access to all the variables and resources of the whole program and vise versa.
I know how to launch a new form trough a main form, but I don't know how to launch a console application.
EDIT:
I forgot to mention, that the console application is a part of the solution.
Your requirement is a bit vague; "the application must have access to all the variables and resources of the whole program and vise versa". 'Variables and resources' cannot be shared across processes, you will instead need interprocess communication of some form.
If your console app merely needs to communicate back to the calling forms app that a RPC has succeeded then use exit codes in the console app, see: How do I return a value from a console application to a service in .NET?
Otherwise this has been answered before: Getting the ouput from Console window into Winform application
You'll need to either create a console emulator (time consuming and difficult to get right), or fire up cmd.exe in another process and use remote procedure calls (or another inter process communication method) to communicate between the two processes
If you want to communicate between the two processes, take a look at this library here:
https://github.com/TheCodeKing/XDMessaging.Net
It allows you to send messages from one app to the other. For example, App1 sends a message "stop" on the channel "randomkey" to ConsoleApp1, ConsoleApp1 can listen on the channel "randomkey" and intercept the "stop" message and stop its current processing.
If you wanted to just open the console window, just use System.Diagnostics.Process.Start();
You can just call Main directly. Beware of doing this on the UI thread directly though!
SomeConsoleApp.Main(new string[]{"-O", "File 1.txt", "-some-parameter"});
Or if you only have an exe, you can do:
System.Diagnostics.Process.Start("someconsoleapp.exe");
How would I go about modifying my c# console application to retrieve values or have methods or events triggered by a shell command.
Basically after the applications loads I want the ability to be able to execute functions in the application as well as pass data to those functions.
Any Direction?
.NET 3.5 Adds Named Pipes Support, I think it can be a solution.
EDIT: better link.
You may use SingleInstance pattern - C# : how to - single instance application that accepts new parameters? .
you should accept input from the console using something like:
Console.ReadLine();
it would work, then the user types a command and clicks enter and you parse the command and execute the actions you need to execute.
it's a bit old fashion like DOS programs, and keep in mind that depending on your design the Console application you are writing would hang until the user enters value and presses ENTER key, means cannot execute anything else because the thread which callls Console.ReadLine waits that command to come from the user.
I'm running a java application from within a C# application. My goal is to fill a couple of input boxes inside the java app automatically using the C# code.
I assume the java application uses Swing for its UI. I've tried using SendMessage and WM_KEYDOWN but nothing happens. Since it's a swing app, the input doesn't have a handle, so I'm using the handle of the entire window.
Any idea how this can be done?
Thanks!
Try sending WM_CHAR with a character to see if that works.
If it doesn't you could use Spy++ to see what messages are being sent.
I am writing a windowed .NET app in C#, which runs a third party console application via the Process class, as hidden as possible (CreateNoWindow, RedirectStandardOutput, etc.).
I've redirected it's StandardInput, so I can write whatever string I want, but not function keys or other type of special keys, as they don't have a character representation. As for me, I have to send keys F1 to F4 to the console app. The solutions I've found are for windowed app (PostMessage, SendMessage).
How can I do this for my console application?
Is there anything to do with the Handle of the Process?
You can't. Function keys cannot work with stdin, an app has to call ReadConsoleInput() to be able to detect them. That no longer works when you start the process without a console window.
Sendkeys.SendWait may solve your problem.
Does SendKeys work ("{F1}")? (the console will have to be active, though).
This isn't directly an answer to your question, but have you considered using MSMQ?
If your windowed application can receive those key-presses, it could pass that fact on to your console application using MSMQ. You may not have to actually pass the key-press, just the the fact that they were pressed.
I have found this techrepublic article helpful in getting the basics of MSMQ working.
How can I make my already running C# Windows Form Application be able to receive commands from the command line while it is already running?
For example: if my application is playing a video now then I want to be able to type on the command line "MyApp /stop" so that while the application is still running it stops the playing the video without exiting from current session.
From your question it seems that your first process is still running and you start a second instance of it, and you wish that instance to communicate with the first.
What you are looking for is called inter-process communication (IPC). The standard way of doing this in .NET is to use Windows Communication Foundation (WCF).
One way would be to make your app a singleton, and whenever another instance is run, it will pass arguments to the already running process.
Example: http://www.codeproject.com/KB/cs/SingletonApplication.aspx
By sending a command like that, you'd be firing up another process. Certain command line arguments could do some kind of IPC to signal the "main" instance of the running app.
Without changing your design structure and assuming your application is a standalone application (running on a local PC),
One method is to make one thread of your application wait (WaitOne()) for a named mutex or semaphore (link text).
When you start your application (your second instance), you parse your commandline (via the args arguments). If the args[0] contains your "/stop" command, you "Release()" the named mutex/semaphore. Then your thread (in the first instance) will be waken to stop the playing the video.
Then again (having said all the above), a more simple solution is to have a STOP button in your application where the user can click on it.