-1

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

SkyStar
  • 29
  • 7

1 Answers1

0

If you have a compiled WPF application executable, you could use the Process.Start API to run it in a separate process:

System.Diagnostics.Process.Start(@"WpfApp1.exe");
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks a lot, the program does start. I didn't describe it clearly, if using process.start(), It will in different process or appdomain. I edited the description, is there a way to solve it in this case. – SkyStar Mar 09 '22 at 15:11
  • Why do you even use a console application if you want to start the WPF application in the same process? Then implement your code in the WPF app directly? – mm8 Mar 09 '22 at 15:23
  • Because the wpf program is already compiled by someone else, I want to make some changes to it – SkyStar Mar 09 '22 at 15:43
  • So why do you want to run it at all then? The changes need to be implemented before you start the process. – mm8 Mar 09 '22 at 15:45
  • It has static integrity check and is very obfuscated... – SkyStar Mar 09 '22 at 15:48
  • Well, you don't "start" anything in the current process unless this is part of the process. If you want a modified version of an app, you need to implement one. – mm8 Mar 10 '22 at 12:34
  • ok i got it, thanks – SkyStar Mar 10 '22 at 15:43
  • I haven't solved my problem yet. This answer may helps me, but it didn't work either. – SkyStar Mar 11 '22 at 14:35
  • You can't solve the issue of recompiling an app by trying to start in an existing process. Your approach doesn't make much sense. – mm8 Mar 11 '22 at 14:38
  • If I use easyhook to modify the compileMethod of JIT, will it work to modify the opcode in it? – SkyStar Mar 11 '22 at 14:48
  • Yes, you are right. I can't start an exe in one process. I will try another way. – SkyStar Mar 12 '22 at 04:12