Converting a C# (Windows application) into commandline application? - c#

I am looking to convert a C# (Windows platform application) into a commandline version.
The scenario is: I have implemented a C# (Windows application) in VS 2010. The output of this application is to generate a txt (log) file (in simple explanation).
Now the case is, there is one other application which need to use my this C# application, by calling my C# application from the command line at the run time.
My question is, how is it possible to convert an already existing C# application into commandline application, so that this C# application can be called from the calling (other) program? There is one input parameter which need to be passed on the commandline to my C# application. And then this C# application will process the data according to input parameter and then generate the output log(txt) file.
Added explanation
I am really impressed by the solutions here. Just a bit more expertise is required from readers. I want one application only to work as both commandline application as well Windows-application (forget to mention it before, sorry!), depending on the number of input parameter pass to the application. From this point of view, I have two options to implement it,
1) Make separate functions for both applications (commandline and windows-forms). Call them according to the input parameter pass. In each function implement the complete functionality of each application without disturbing (or going into the code of other application). Also I will be able to re-use 2 main functions, already built in windows-form application into my commandline application after some editing.
Disadvantage: This will make the code size nearly 50% more than case 2.
2) The second idea is same as describe by one of the expert here, to use the same application/functions for commandline as that of already built windows-form application. The only way to distinguish is to look at the input parameter pass, and decide accordingly whether to show the GUI interface or just use the commandline input (and do processing).
Disadvantage: This case will make the code bit messy and difficult to maintain/implement due to extra adding of check for number of input parameter decisions.
Which strategy should I follow for implementation?

Sure - just:
Create a new VS2010 command-line project
You'll now have a "main ()" (or, in MS-Land, "_tmain()") function instead of a root class.
Cut and paste the relevant code into "main()" (or into a function called by main (), or into a class created from main() - your choice).
Do a search-and-destroy mission to find anyplace where you're doing GUI input, and substitute command line parameters instead.
Parse your command line.
Voila! Done!

You don't have to convert it. Your application can stay as a Windows application. You simply need to handle command line arguments.
To get the command line arguments from ANYWHERE in the application, just use Environment.GetCommandLineArgs();

You want to get value from command line is not a good reason to convert winform app to console app. You may use,
string[] args = Environment.GetCommandLineArgs();
However you can change application type by opening project properties (right click on project name) and change the Output type.

Just don't show the GUI if you get paramater passed in, as when called from the other program.

Related

How to pass argv[0] with Process.Start in C#

I'm writing a wrapper program in C# with Mono under Ubuntu. The key of a successful wrapper is to be as transparent as possible. One of the requirement is to pass argv through exactly.
When use Process.Start to start the wrapped program, how can we pass argv[0] to the wrapped program? It seems that we can't pass argv[0] at all, it is just
set to the wrapped program's executable full path.
Some program's behavior may depends on argv[0]. This is true for AOSP's prebuilt clang tool set. For example, under
prebuilts/clang/host/linux-x86/clang-4691093/bin$
there is lld, this program obvious check argv[0] to decide which symbol link you invoke (ld.lld/ld-link/ld64.lld). If you don't invoke lld through predefined symbol links, lld will report error.
With C/C++, it is easy to set argv[0] to the symbol link's name to successfully invoke lld. But can we do this in C#?

C# application which installs and allows for global command line

I would like to build a simple .NET application that installs on a windows machine which will allow the end user to open a command prompt and type "google test" which will open google and search for test. But I'm not sure how to install a global "keyword" such as "google". Is this down via Environment variables? How does this link up to my .NET application?
Any advice or pointers is appreciated.
Thanks
This is beyond a .NET application and belongs into the Area of Setup programms. Visual Studio used to have a Installer Project, but as there are so many 3rd party ways this was removed.
Having "google test" parsed as a google search for "test" is tricky, but possible. However it depends entirely on how the commandline is parsed in a Windows. Wich is pretty much the way it was parsed since DOS 1.0.
If you just enter a word like "google", the commandline will look if there is a Excetuable file (.exe, .com, .bat) of that name in the current folder (working directory). It will then look into all the folders defined in the path variables. However using path variables is so dated, I could find only a single entry in my Windows Path variable now. And it is for MS backward compatibility: "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;"
Most starting of programms is done via file associations with a specific type. And protocoll associations with a protocoll (like http, https, steam). Indeed my advise would be to make a .NET Programm named "google" whose sole purpose is to to format the proper URL, then send it to console parsing via ProcessStart. And let Windows solve wich programm to use from there.
Edit: And as I only realized after wirting this, apparently Windwos finally supports commandline aliases. https://learn.microsoft.com/en-us/windows/console/console-aliases
Apparently they even added a way to make them via the WinAPI in Windows 2000. However that requires moving to unmanaged code. And I have no experience, but it appears those aliases might only be valid for one console instance. So you might have to put it into a place like hte autoexec.bat to be called everytime a console is created.

How do I set windows service "command line" parameters through the Parameters subkey?

The windows service c# walkthrough on msdn says that the service can receive command line style args via the "Paremeters subkey" in HKLM\System\CurrentControlSet\Services\. Now, I'm assuming that means a sub-foldery looking thing as a child of the folder in the tree, and not just a value of some type named "Parameters" in the foldery thing (I tried that with single and multiple string values, didn't work). Also other services I can see in the registry have a Parameters sub-foldery thing. This is my first time writing real windows code :P.
However, none of the values I put into the Parameters subkey (foldery thing) are getting passed as args to my service. Is it because I'm starting the service manually from the service manager? Do the parameters only get passed on auto-start? Or am I just doing it wrong? I've just been using string values (REG_SZ).
The service is going to be running on an embedded windows device, and we'd like to configure each unit once when we put it together via automatic remote registry editing.
Derp. Missed the step in the walkthrough where you edit the installer code to change the ImagePath line automatically. I'll leave this here in case others suffer the same mistake.

Detect when exe launched via Google Chrome Native Message Passing

I'm trying to retrofit an existing NPAPI plugin to use Google's native message passing technology. Since it's an existing exe, we already have some console behavior programmed in so that users can call our program from the terminal. Is there any way for us to detect, in a C# application, that the exe has been launched by Google Chrome for message passing? If we could do that, we could launch the message passing loop if we're called from Chrome but resume normal behavior if called from Powershell/cmd.
I've tried inspecting the command line arguments passed to the program when launched by Chrome, but there are none. Having a configurable option there would solve this for us, but as far as I can tell it's not possible. I haven't yet had a chance to inspect the current working directory in case it could also be used as an identifier.
Actually, yes, I believe it is possible.
When, for example, a C# console application is started as a native message client host, it is passed two arguments:
--parent-window=<number>
and
chrome-extension://<extension identifier>/
I think the second argument is probably the ideal one for determining that not only was it Chrome that launched the process, but that the specific extension you authored and intended to call it launched it!
Please make note in the above, the "<" and ">" are not literally part of the argument, and just used to denote the beginning and end of that part of the message, much like double quotes.
Alternatively, just have your extension invoke a script (.bat, .sh, etc.) that passes special arguments to your native host. This way you could pass specific arguments of your own.
The API doesn't support passing command line arguments, but your host process should be able to inspect its own parent process to determine if it was launched by Chrome or something else.
An alternative to checking the arguments as suggested by #aikeru would be to check for the existence of certain environment variables that were passed from Chrome to the native messaging host. My host has the following variables that seem to be specific to Chrome (found with Sysinternals Process Monitor):
CHROME_ALLOCATOR=TCMALLOC
CHROME_BREAKPAD_PIPE_NAME=\\.\pipe\GoogleCrashServices\S-1-5-18
CHROME_MAIN_TIME=13037817851797830
CHROME_METRO_DLL=0
CHROME_PRE_READ_EXPERIMENT=100-pct-default
CHROME_RESTART=Google Chrome|Whoa! Google Chrome has crashed. Relaunch now?|LEFT_TO_RIGHT
CHROME_VERSION=33.0.1750.117

How to invoke an executable desktop application from within an ASP.NET MVC application?

I have finished developing an executable desktop application to generate a fractal image based on the passed-in arguments. The output type is JPEG.
Now I am developing a site under ASP.NET MVC 3. I want to use the executable from within my site.
Is it possible to use it as is without converting it to a class library and recompiling?
If you mean "to run on the user's machine", then not "as is" - you might want to look at Silverlight for that.
If you mean "to use at the server", then it will of course depend on how it operates (whether it prompts for input etc), and how it works (does it use GDI+, for example? that isn't recommended for use on web servers).
But sure; you can shell an exe with Process.Start, or if it is a .NET exe you can either add a reference directly to the exe and use it as a library (if it has appropriate code)
There is also a way to run it in-process via AppDomain.ExecuteAssembly - not sure this latter is a good idea on a web-server, though... especially if the exe talks to stdout.
For getting the image back to the client, you would want this processing to happen (perhaps with caching) in a route that uses return File(...); from MVC to simulate the image stream.
Use the system.Diagonstics.Process class to call the exe with a StartInfo object to pass the appropriate arguments.

Categories