C# Send input to separate process through command prompt - c#

I have an application on Windows 10 VPS that I will be only able to have access via the command prompt. After the application is ran, I want it to be able to retrieve input from the command prompt, and give back output. Although, after disconnecting the application should still run in the background.
When I reconnect to the VPS, I want to regain the ability to input and output text through the command prompt, without restarting the application.

Here's some documentation example code of how to create a TCP client in C# if you decide to go the TCP/Sockets route over WCF: http://www.winsocketdotnetworkprogramming.com/clientserversocketnetworkcommunication8q.html
And here's the MSDN page on WCF: https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx
This is the MSDN page on creating a service application: https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
Do note: "The Windows Services project template that is required for this walkthrough is not available in the Express edition of Visual Studio."
But don't let that concern you. All this means is that your program would not be able to register as a windows service, and therefore could not truly operate like a service which can start on boot, pause, stop restart, etc. Howerver, this doesn't stop you from creating a program that automatically starts. You could simply place a shortcut to your program in your startup folder:
C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Hopefully that helps get you started in the right direction!

Related

Launch an exe application from windows service

I have a service which should open an exe application ( C# Application ) based on certain conditions. When the service is started in debugging mode ( Visual studio ) it opens the application. But when it is installed as a service, it does not do so. It fails to open the application. Why is this happening?
What you're trying to do isn't directly possible under normal circumstances - simply launching an app in a new process from your Windows Service code is not going to interact with the GUI of the currently logged in user I'm afraid.
There are ways of communicating between a service and the GUI however.
This discussion might point you in the right direction.
Based on your comments, I think what you are really looking for here is a normal userspace application and a scheduler. You might want to use Windows' own scheduler to run the application every monday, if it is an always-on box, or place the application in Startup. When the application runs, it should check the current day of week, and if it is monday and the application has not previously run this day, the application should start. If not, you can safely terminate the application entirely.
Thanks for the answers! I found out a solution for it and im posting it here.
I created a dummy app which is hidden on startup and it does the exact same function that the service was intended to.
1.create a dummy app (copy paste code from service to form application)
Hide it after start up.
2.start the application right after installation.
3.add registry key so that it starts up after a system reboot.
in simple words, clone service behavior.

Windows Service and Application interaction

TL;DR: I have a service, a gui, and a tray icon. I want to know how to get the tray icon to run on user login/start-up and be able to stop the service and start the gui
Some Background Info:
I have a windows service I've made that uses a xml file to collect
files from other computers on my network and store them on the local
pc (running the service). the xml has some structures called
'profiles' which have info like FileDestination, LocationToTakeFrom,
FileTypeToTake, and IsProfileActive...
The service basically takes all the active profiles and every hour
scans the location for files created within a 1hr window of the
current date/time on local PC.
My GUI allows the user to make profiles / modify profiles, as
well as determine which profile(s) should be active for collection. I
dont want this gui running at all times, so I plan to have a
systemTrayIcon to allow this GUI to be opened and shutdown.
I'm using Visual Studio 2010 .NET 4.0 everything is in C#, I have 1 solution with separate projects(gui and service)
I'm wondering about the following things as far as the System Tray Icon goes:
1) how do I have the icon start on user login (note that this will be distributed via an installer, not just my personal use. so It has to be done via code)
2) Stop a service via sysTrayIcon
3) where to place the SysTrayIcon... do I make a 3rd project? add it in the GUI project? not quite sure here.
4) if SysTrayIcon IS in a seperate project how can I have it create instances of the GUI?
ie how can I start the GUI application from code in a different project
the project requirements are:
-upon installation the GUI must start, after that the Gui should only be accessed through the tray icon.
-user should be able to stop the service any time via system tray icon
Start the client
There are plenty of ways to start an application on Login under Windows. Just grab SysInternals AutoRuns to get an idea. The obvious ones are (a) the good ol' Startup group and (b) one of the
\SOFTWARE\Microsoft\Windows\CurrentVersion\Run*
keys under HKCU and/or HKLM. That's the typical task of an setup utility, which makes sense since you have to install the service anyway. To do it in code:
Registry: Open the reg key, add the entry, close the reg key.
Startup: determine the value of CSIDL_STARTUP or CSIDL_COMMON_STARTUP using SHGetFolderPath, then create a Shell Link in that folder.
Service communication & control
The GUI part implements the TrayIcon and communicates through some channnel with your service. Again, there are plenty of possibilities how to do this, like disk files or memory mapped files, named pipes, even sockets. It would be too broad to list all the ways here, so I'd suggest to decide for one way and ask again if you have specific questions on that one.
To stop your service from code, use the ControlService() function and pass SERVICE_CONTROL_STOP as the dwControl parameter. To start a service, there's another function named (big surprise) StartService() to achieve that. Note that you may need to start an elevated copy of your app to control services. A quick & dirty way is to simply launch net start/stop MyService elevated with the necessary args.

File System watcher in Windows Service or Console Application?

I am implementing a file system watcher, my requirement is to watch for a given local folder on a machine and then do a small task (for example open a certain page in web browser). The file(s) in the given directory would be generated randomly sometimes every two hours or four etc. This tool should be automated in the sense that a user does not have to start it. So my question is, Should I implement this in a windows service which always will be running or in a console application.Preference is to do it in a console application but then it would need to started by a user right? Please advice
You can automatically call any type of application (Console, Windows, etc.). What it gets down to with a Windows Service is whether you want it to be running before anyone logs in.
Only a Windows service runs while no one is logged in. A console application (while it can be set to run on login) must have someone log in in order to run.

c# make a connection between a windows service and a c# project

I have a c# project that prints or take some values from the keyboard to a console application. Can someone tell me how could I start this application from a windows service? I mean..when the computer is turned on I would like to pop up on my desktop the console application in which i can write values and see the result? Need some help. Please print a little code if you have one. Thx!
You just put the shortcut of the application in the Startup folder and it'll work. This is the simplest way, no code change needed
Windows Services usually run non-interactive under a specific system account rather than a desktop user account.
If you want the program to start when a user logs on you can always put a registry key in:
HKLM\Software\Microsoft\Windows\Run
And it will start when a user logs on.

How to run a command-line program on a remote server

I have written a tool that is run on the command line.
I can test it on the command line on my machine, and it works fine.
I then remote into the target server machine and run it on the command line there, and it still works.
However...this tool needs to remain running (its a monitoring tool).
I can't leave my remote session to that machine open for too long, as other people need to use it, and when I close my remote session, that command line tool ends.
I don't know how to, and would like to avoid needing to, write a windows service, but essentially I need that capability. I need to almost wrap my command line tool with a windows service that can remain running on the target machine while I disconnect.
Can anyone provide any insight/strategy on how to do this?
Edit:
Both of the below posts are helpful.
If it's Windows Server 2003 or earlier you can use Srvany.exe to run any application as a service. See here for details.
If it's Windows Server 2008, SrvAny isn't supported officially, but you might be able to hack it to work by copying it from Win2003 Resource Kit or earlier and then combining it with the instructions in Thomas answer in some way.
However, writing a proper Windows Service is really quite simple and it's the best way to make sure that it'll coexist happily with everything else.
Just create a Windows Service project in Visual Studio. In the OnStart you create a thread that runs your code. In the OnStop you use a flag to stop the service. Then you can make it better later.
See if this page helps:
http://www.msfn.org/board/topic/83272-how-to-run-a-program-as-a-service/
An alternate solution is to use the PsExec to launch your monitoring tool on the remote machine in detached mode, i.e.:
PsExec.exe \\remote_machine -d C:\...\monitoring_tool.exe
PsExec.exe launches monitoring_tool.exe on the remote machine and then terminates itself. It does not wait for monitoring_tool.exe to terminate.

Categories

Resources