如何加载 wpf exe 并在同一进程和应用程序域中通过反射启动它?
我正在使用 TinyJitHook https://github.com/Elliesaur/TinyJitHook 来修补已编译的可执行文件。
我想加载一个wpf exe文件并在ac#控制台程序中运行它;
wpf程序是Visual Studio默认的空窗口。
主程序是控制台程序;
当我运行主程序时,出现“无法找到资源‘mainwindow.xaml’”错误。
我应该怎么办。
感谢发表“Process.Start("WpfApp1.exe")”评论的人,这会启动程序,但这似乎并没有使它们位于一个进程或AppDomain中。
mainApp
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());
}
}
App.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>
MainWindow.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
I'm using TinyJitHook https://github.com/Elliesaur/TinyJitHook to patch a compiled executable file.
I want to load a wpf exe file and run it in a c# console program;
The wpf program is the default empty window of visual studio.
The main program is a console program;
When I run the main program, I get a "Cannot locate resource 'mainwindow.xaml'" error.
What should I do.
Thanks to the people who made the "Process.Start("WpfApp1.exe")" comments, this starts the program, but that doesn't seem to make them in one process or AppDomain.
mainApp
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());
}
}
App.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>
MainWindow.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>
Error
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有已编译的 WPF 应用程序可执行文件,则可以使用 Process.Start API 在单独的进程中运行它:
If you have a compiled WPF application executable, you could use the
Process.Start
API to run it in a separate process: