Call method in already running .NET assembly from different app

孤街浪徒 提交于 2019-12-20 07:26:14

问题


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.

Basically what I have is a application and when it calls a .net method it loads the assembly that contains that method into into a appdomian and then calls the method.

I need to get from the loaded assembly into another process and call a supplied method.

I know this might not help but this is picture of what happens: alt text http://img527.imageshack.us/img527/6960/probt.jpg

Sorry for the low quality.


回答1:


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.




回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/1376496/call-method-in-already-running-net-assembly-from-different-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!