簡體   English   中英

如何加載一個 wpf exe 並在同一個進程和 appdomain 中通過反射啟動它?

[英]How to load a wpf exe and start it by reflection in the same process and appdomain?

我正在使用 TinyJitHook https://github.com/Elliesaur/TinyJitHook來修補已編譯的可執行文件。

我想加載一個wpf的exe文件,在c#的控制台程序中運行;

wpf程序是visual studio默認為空的window。

主程序是一個控制台程序;

當我運行主程序時,出現“找不到資源‘mainwindow.xaml’”錯誤。

我應該怎么辦。

感謝發表“Process.Start("WpfApp1.exe")”評論的人,這啟動了程序,但似乎並沒有使它們在一個進程或 AppDomain 中。

主程序

public static void Main(string[] args)
{
    string filePath = @"WpfApp1.exe";

    Assembly asm = Assembly.LoadFrom(filePath);
    MainJitHook hook = new MainJitHook(asm, IntPtr.Size == 8);
    hook.OnCompileMethod += ChangeExample;

    hook.Hook();
    try
    {
        MethodInfo method = asm.EntryPoint;
        method.Invoke(null, new object[] { });
    }
    catch (Exception extInfo)
    {
        Console.WriteLine(extInfo.ToString());
    }

    hook.Unhook();
    Console.WriteLine("DONE");
    Console.ReadKey();
}


private static unsafe void ChangeExample(MainJitHook.RawArguments args, Assembly relatedAssembly, uint methodToken, ref byte[] ilBytes1, ref byte[] ehBytes1)
{
    try
    {
        var methodBase = relatedAssembly.ManifestModule.ResolveMethod((int) methodToken);
        Console.WriteLine("###################### cur method is " + methodBase.Name + " asm fiel is " + relatedAssembly.GetName());
    } catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

申請.xaml

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

主窗口.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>

錯誤

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: Cannot locate resource 'mainwindow.xaml'.
   at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)

    ...
    ...

   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at WpfApp1.App.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at TinyJitHook.Program.Main(String[] args) in E:\download\TinyJitHook-master\TinyJitHook\Program.cs:line 42

如果您有一個已編譯的 WPF 應用程序可執行文件,您可以使用Process.Start API 在單獨的進程中運行它:

System.Diagnostics.Process.Start(@"WpfApp1.exe");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM