Solved

call a method from my programm/exe from commandline

Posted on 2010-11-22
12
461 Views
Last Modified: 2013-12-17
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
Comment
Question by:Arikael
12 Comments
 
LVL 5

Assisted Solution

by:buraksarica
buraksarica earned 60 total points
Comment Utility
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
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 60 total points
Comment Utility
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
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 
LVL 3

Author Comment

by:Arikael
Comment Utility
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
 
LVL 3

Author Comment

by:Arikael
Comment Utility
oh, and perhaps storing the an instance of the application-class in a static variable of the program-class?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 
LVL 3

Author Comment

by:Arikael
Comment Utility
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
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
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
 
LVL 3

Author Comment

by:Arikael
Comment Utility
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 380 total points
Comment Utility
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
 
LVL 3

Author Comment

by:Arikael
Comment Utility
Thanks for your help, I really appreciate it :-)

I'll have a look at your code
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now