代码之家  ›  专栏  ›  技术社区  ›  Nathan W

已从其他应用程序运行.NET程序集时调用方法

  •  0
  • Nathan W  · 技术社区  · 16 年前

    我不知道您是否能做到这一点,但基本上我需要能够从不共享同一进程的不同程序集调用已在运行的.NET进程中的方法。

    alt text http://img527.imageshack.us/img527/6960/probt.jpg

    很抱歉质量太低了。

    3 回复  |  直到 16 年前
        1
  •  1
  •   Noon Silk    16 年前

    你能控制这些应用程序吗?如果是这样的话,就让他们互相“打开一个渠道”。也许您可以使用一个套接字,从另一个套接字写入一个,或者一个命名管道,或者类似的东西。如果我同时写这两本书,我会这么做的。

        2
  •  0
  •   Claudiu Mihaila    16 年前

    最简单的方法是使用ServiceHost公开WCF服务。不能从另一个进程定向访问一个对象。唯一的选择是向另一个进程发送消息解释消息并执行操作。

    这里有一个简单的例子: http://blogs.microsoft.co.il/blogs/davids/archive/2009/05/31/a-hands-on-lab-for-wcf-moc-6461.aspx

        3
  •  0
  •   taquion    9 年前

    question 已标记为此副本。那些决定在这个问题上说这个问题有答案的人。好吧,这个问题还没有任何公认的答案。也许我的答案可能会有所帮助。这个解决方案适用于WPF,但它的本质确实独立于WPF

    我对这个话题做了一些研究,因为它看起来很有趣。我想你可以用Win32。我做了一个非常简单的样品。两个WPF应用程序,第一个命名为WpfSender,第二个命名为WpfListener。WpfSender将向WpfListener进程发送消息。

    enter image description here

    下面是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;
        }
    }
    

    您可以使用Win32 api跨windows应用程序发送消息

    这是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;
        }
    }
    

    我不在这里写XAML,因为它非常简单。同样,这是一个非常简单的示例,它向您展示了如何实现跨应用程序消息发送。极限是你的想象力。您可以声明许多int常量,每个常量代表一个操作,在switch语句中,可以调用所选操作。

    我不得不说,我在研究中发现了两篇文章:

    For knowing how to handle WndProc in Wpf

    For knowing how to send messages using win32 api