I don\'t know if you can do this, but basicly I need to be able to call a method in already running .NET process from a different assembly that don\'t share the same process.
Do you have control over the apps? If so, just have them 'open a channel' to each other. Perhaps you can just use a socket, to write to one from the other, or a named pipe, or something similar. It'd what I'd do, if I was writing both of them.
The easiest way is to use ServiceHost to expose a WCF service. You can not directed access an object from a process from another process. The only option is to send a message to the other process interpret the message and execute a action.
Here you can find a quick example: http://blogs.microsoft.co.il/blogs/davids/archive/2009/05/31/a-hands-on-lab-for-wcf-moc-6461.aspx
Hope this helps.
This question has been marked as a duplicate of this. And people that decided that say in this question there is the answer for that question. Well, this question does not have any accepted answer yet. An may be my answer may help. This solution works with WPF but its essence is indepent of WPF indeed
I have researched a little about the topic since it seems interesting. I think you could do this by using Win32. I have made a very very simple sample. Two WPF applications, first named WpfSender and second named WpfListener. WpfSender will send a message to WpfListener process.
WpfSender only has one button that send the message once it is clicked. WpfListener is only an empty window that will show a message box when receiving the message from WpfSender.
Here is the code behind for WpfSender
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfSender
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var process = Process.GetProcessesByName("WpfListener").FirstOrDefault();
if (process == null)
{
MessageBox.Show("Listener not running");
}
else
{
SendMessage(process.MainWindowHandle, RF_TESTMESSAGE, IntPtr.Zero, IntPtr.Zero);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
private const int RF_TESTMESSAGE = 0xA123;
}
}
You use Win32 api for sending messages across windows applications
Here is the code for WpfListener
using System;
using System.Windows;
using System.Windows.Interop;
namespace WpfListener
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(WndProc);
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == RF_TESTMESSAGE)
{
MessageBox.Show("I receive a msg here a I can call the method");
handled = true;
}
return IntPtr.Zero;
}
private const int RF_TESTMESSAGE = 0xA123;
}
}
I don not write here the XAML since it is very simple. Again this is a very simple sample that shows you how to achieve cross application message sending. The limit is your imagination. You can declare many int constants each one representing an action, an then in a switch statement you can call selected action.
I have to say that I follow two articles that I found in my research:
For knowing how to handle WndProc in Wpf
For knowing how to send messages using win32 api
Hope this helps!