I just want to know how to pass arguments to windows services . The problem is , i am developed a windows application and then called it from Onstart() method . Now , i want to call the particular function from another project .
protected override void OnStart(string[] args)
{
Process.Start("C:\\Program Files\\macro.exe");
}
I want to call a function inside the macro.exe project with arguments . Any suggestions
If you want to use arguments passed to service, you can try:
Process.Start(#"C:\Program Files\macro.exe", String.Join(" ", args))
Note that using #Tudor answer you can have better control on started process, redirect input/output/error, show/hide window and many other nice things.
I wonder if at all you can call a function inside another EXE.
May be you can make a common DLL and refer it in macro.exe and your own application. If thats not a choice then you can either use sockets or file system to communicate with the EXE.
For eg. you pass your EXE 2 command line arguments "MyFunction", "Hello" as shown in previous comments above and the Exe grabs those args deduces that it needs to call MyFunction with argument Hello and to get back the result the EXE writes it to a predefined path and you read it from the file.
If the Exe is running and you can not pass command line to it, then may be the Exe keeps scanning an input file to get commands from external sources like your app. Or better still it listens on a port on which you fire commands to it.
I am not sure if there can be another more starightforward solution.
Related
I want to create a command executor like Start > Run. The application has a TextBox and when a user enters a command eg : notepad "C:\test.txt" it should open notepad with this file or %PROGRAMFILES% it should open 'Programs Files' directory.
For %PROGRAMFILES% and other Windows variables I can use Environment.ExpandEnvironmentVariable and get their path and pass to Process.Start
and for notepad, I can split the with space and first part goes in FileName and rest of string goes in Arguments of ProcessStartInfo.
But what I want to know is, how does Start > Run work ? is there something like I can execute the whole command without splitting command-line or expanding the variables ? Maybe with pinvoke ?
To the very best of my knowledge, the run dialog is implemented with a call to ShellExecuteEx. You can achieve the same with Process.Start and UseShellExecute. You do need to expand the environment variables, and split the command into filename and arguments. You already describe how to do that in your question, and, again to the best of my knowledge, there is no programmatic interface to the functionality of the Run dialog.
However, what you can do programmatically is show the Run dialog. Create an instance of the Shell object requesting the IShellDispatch interface, and then call the FileRun method.
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
This question already has answers here:
How do I set a program to launch at startup
(13 answers)
Closed 9 years ago.
For some CAD software i have been making a cleanup program (WPF) that can clean certain types of temp and journal files that i wont need anymore, but that my cad software does not clean out of itself.
As i made it into a WPF application, i just made a button to clean up any filetypes that i wont need.
Now i have been looking for a function to make it execute after i have logged into windows. So i do not try to execute the full program after the startup, only one of the commands within the program. I know that there is a folder called startup in windows, but i preffer not to use that one.
Is it possible to do it in a different way? So i can just make a checkbox in my application where i can set yes or no to make it run at startup?
i have been looking around, but havent realy found anything about this.
In accordance to your edit;
Check out your startup options in this post; How do I set a program to launch at startup
Then with you addition of 'So i do not try to execute the full program after the startup, only one of the commands within the program'; make use of start-up parameters.
Supply your application with a command-line parameter and check it in your application's Program.cs.
When the right parameter is set, just clean your files and don't show the interface.
When the parameter isn't set (which it won't be when the user just double clicks the icon) run your program like normal.
To use command-line arguments just add a string[] parameter to your Main method like so;
public static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
foreach(string s in args)
{
Console.WriteLine(s);
}
}
Your full (pseudo-)code would look like this:
public static void Main(string[] args)
{
if (args[0].Equals("/JUSTCLEANPLEASE"))
JustClean();
else
Application.Run(new Form1());
}
Why do you not want to use the auto start directory?
There are some alternatives to this. Please refer to this knowledgebase article:
http://support.microsoft.com/kb/137367/en-us
When you create an entry in one of those nodes specified in the link you can control whether you want to run something on every start up or only once (see RunOnce).
The simple approach would be creating a batch file in a temp directory or somewhere where it won't be deleted and add it to one of the auto run nodes, that is appropriate for you.
Samples on how to access registry with C# are plenty.
Edit: Or see the link, provided by Gerald Versluis
However keep in mind that there is a difference in Run and RunOnce of these different nodes
Not sure how your program is set up, but I would split the user interface (GUI) from the logic.
You could create a service for the logic and the WPF user interface would seperately call this service.
You could then write a little code to call the service without having to use the GUI.
Have a look in MSConfig.exe for better control over the startup and services etc.
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.
I have been using Process.Start to launch executables (.exe) files. Now I need to execute a .pl file with some arguments. can I still use Process.Start or I need a different approach?
EDIT :- I am having to mark this question unanswered as I am getting the following error when I try to call the perl file from the CSharp code:- (When I call the same from the commandline with the same path and parameters, It works fine)
System.ApplicationException: StartProcess Failed
System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform)
Please note that when I try to call an .exe file from my C# code, I dont see the above error.
EDIT:-
Checking the following link now:- How do I call Perl script in C# application?
It seems that the ProcessStartInfo constructor has two parameters - fileName and the arguments. You should set Perl.exe as the fileName and the "argument" would be your perl file (.pl) with other arguments It accepts. Checking now....
You certainly can :) you can also pass it arguments by adding them after the file name in
Process.Start(file.pl args1 args 2);
It will load the file with your default application for .pl files, the other option is to specify the software then pass your file as a parameter providing you have the right software to handle the file it should be fine
Process.Start() can be pointed at any file and it will be opened using the default software or that which you specify, it need not be an executable.
Yes you can, Process.Start() takes a string parameter, what you pass for this parameter does exactly the same thing it would do if you entered the same string in the windows start -> run dialog.