Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

call a method from my programm/exe from commandline

Hi experts

I have a small c# application (.NET 3.5) which gets distributed as clickonce as an exe and is used for tasks, the users have to complete (similiar to outlook tasks)
this application then sends emails to various users. the emails contain tasks which need to be done.
What I need is a link in the emails which, upon clicking, opens the correct task in my application.
So I need to be able to call a method from my application from an external point.

I have no idea how I can do that.
Hope you can help
0
Arikael
Asked:
Arikael
3 Solutions
 
buraksaricaCommented:
You can register your own protocol using Registry. Here is how to do it: http://msdn.microsoft.com/en-us/library/aa767914%28VS.85%29.aspx

with this, you can make applications open your application with given argument.

But if you want your application, not to open new instance and, call a method on the already-opened instance; then you need to make your application single-instance compatible, and send the arguments to the already-opened instance.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
research command line arguments.   Pass those into your program as part of the call.  
In your main, parse out the arguments (switch/select command), calling the appropriate function as needed.
0
 
Todd GerbertIT ConsultantCommented:
You can register a custom URL handler in the registry, following the directions here: http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx

Since this is a ClickOnce application you'd probably want a method in your program to do/check that each time it runs (since ClickOnce applications may have a tendancy to move around) - you could use System.Reflection.Assembly.GetExecutingAssembly().Location to determine the path to register, and register - for example - the URL myTasks.

Then you could put a URL in the email along the lines of myTasks:theTaskId
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ArikaelAuthor Commented:
Thanks for your comments
the URL-Handler thing looks very interesting, thanks for that too?

I was a bit puzzled because the main of a winForm-App doesn't take any parameters/arguments.
I assume Environment.GetCommandLineArgs(); is the way to go?
0
 
ArikaelAuthor Commented:
oh, and perhaps storing the an instance of the application-class in a static variable of the program-class?
0
 
Todd GerbertIT ConsultantCommented:
Environment.GetCommandLineArgs() will work fine (you can also change the Main() method of a Windows Forms application to accept parameters and/or return a value).
0
 
Todd GerbertIT ConsultantCommented:
oh, and perhaps storing the an instance of the application-class in a static variable of the program-class?

What application class?  You mean the Application class?  All you should need to do is reference System.Windows.Forms for that class to be available.
0
 
ArikaelAuthor Commented:
Hi

I explained it a bit weird. I meant the winform-class (the main form) which gets called via application.run(new Form())

there should only be one instance of my application running, so I thought storing my mainform in a static variable would be a way to prevent that.
if the variable is not not null, I can just get the static variable and use it.
0
 
Todd GerbertIT ConsultantCommented:
That won't work quite the way you think it does.  You'll need to implement something like using a Mutext to ensure only one instance of your application is running.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			bool grantedOwnerShip;
			Mutex singleInstanceMutex = new Mutex(true, "MyAppSingleInstanceMutex", out grantedOwnerShip);
			if (!grantedOwnerShip)
			{
				MessageBox.Show("Another instance of this application is already running.");
				Environment.Exit(1);
			}

			Application.Run(new Form1());

			GC.KeepAlive(singleInstanceMutex);
		}
	}
}

Open in new window

0
 
ArikaelAuthor Commented:
Thanks, that's what I found out after testing the  static-approach :)
http://www.sanity-free.com/143/csharp_dotnet_single_instance_application.html
this would be a way to notify my application and show the task to the user (it's the same as your suggestion with the addition of the ability to notify my app).

Basically this means that this message gets broadcasted to every window, right?
is this the best way to do it?
0
 
Todd GerbertIT ConsultantCommented:
Broadcasted to every window: yes, but it's done by registering a custom Windows message so it will be meaningless to applications other than yours.

Best way: 6 of one, half dozen of the other.  I've done something similar by using a NativeWindow whose sole purpose is to listen for messages, and directing a pre-existing Windows message (WM_APP, which is reserved for application-defined use) to that Window.  I did this for a specific reason - namely that my application spends most of it's time as an icon in the system tray, and thus didn't have a form open whose WndProc I could override. Using the NativeWindow class let's me always have an "invisible" form open all the time in order to receive Windows messages.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

static class Program
{
	public const string MessageWindowCaption = "MessageListenerWindowCaption"; // A GUID string would be good here

	[STAThread]
	static void Main()
	{
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);

		bool grantedOwnerShip;
		Mutex singleInstanceMutex = new Mutex(true, "MyAppSingleInstanceMutex", out grantedOwnerShip);
		if (!grantedOwnerShip)
		{
			IntPtr hWnd = WinAPI.FindWindow(null, Program.MessageWindowCaption);
			if (hWnd != IntPtr.Zero)
				WinAPI.PostMessage(hWnd, WinAPI.WM_APP, IntPtr.Zero, IntPtr.Zero);
			return;
		}

		Application.Run(new Form1());

		GC.KeepAlive(singleInstanceMutex);
	}
}

public class Form1 : Form
{
	MessageListener _messageListener;
	public Form1()
	{
		_messageListener = new MessageListener(Program.MessageWindowCaption);
		_messageListener.MessageReceived += new EventHandler(_messageListener_MessageReceived);
	}

	void _messageListener_MessageReceived(object sender, EventArgs e)
	{
		MessageBox.Show("A second instance was attemped.");
	}
}

public class MessageListener : NativeWindow
{
	public event EventHandler MessageReceived;
	public MessageListener(string WindowCaption)
	{
		CreateParams cp = new CreateParams();
		cp.Parent = WinAPI.HWND_MESSAGE;
		cp.Caption = WindowCaption;
		CreateHandle(cp);
	}
	protected override void WndProc(ref Message m)
	{
		if (m.Msg == WinAPI.WM_APP)
			OnMessageReceived(new EventArgs());
		base.WndProc(ref m);
	}
	protected virtual void OnMessageReceived(EventArgs e)
	{
		if (MessageReceived != null)
			MessageReceived(this, e);
	}
}
public static class WinAPI
{
	[DllImport("user32.dll", SetLastError = true)]
	public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
	[DllImport("user32.dll", SetLastError = true)]
	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

	public static readonly IntPtr HWND_MESSAGE = new IntPtr(-3);
	public const int WM_APP = 0x8000;
}

Open in new window

0
 
ArikaelAuthor Commented:
Thanks for your help, I really appreciate it :-)

I'll have a look at your code
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now