Okay so i recently dusted off my old bot code and starting rewriting it from scratch and it seems i've run into one of the problems i had in my previous project: WPF and EndScene hooks screw shit up :<
Basically i have a normal WPF window with nothing exciting in, just normal stuff that works just fine in a normal standalone WPF app and a EndScene and a BeginScene hook that so far does nothing but just sit there and gives me access to wow's d3d9 device using the SlimDx libary...
For the hooking/detouring I'm using Apoc's WhiteMagic libary
Ok time for some code:
Main.cs
Code:
public class Main
{
private static BotBase Base = new BotBase();
public static int Start()
{
Statics.Initialize();
Extensions.Rebasher.Rebash(typeof (Offsets.Misc));
Base.DefaultInitialize();
HookEndScene();
while (!Statics.Shutdown)
{
System.Threading.Thread.Sleep(100);
}
Shutdown();
return 0;
}
public static void HookEndScene()
{
EndSceneHandler = EndSceneHook;
var dev = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, new PresentParameters() { BackBufferWidth = 1, BackBufferHeight = 1 });
var endsceneAdd = (IntPtr)Marshal.ReadInt32(((IntPtr)Marshal.ReadInt32(dev.ComPointer)) + 0xA8);
var dele = Statics.Magic.RegisterDelegate<EndSceneDelegate>(endsceneAdd);
Statics.Magic.Detours.CreateAndApply(dele, EndSceneHandler, "EndScene");
}
#region Delegates
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int EndSceneDelegate(IntPtr pointer);
public static EndSceneDelegate EndSceneHandler;
#endregion
private static int EndSceneHook(IntPtr pointer)
{
if (Statics.Device == null)
{
Statics.Device = Device.FromPointer(pointer);
}
else
{
if (Statics.Device.ComPointer == pointer)
{
Base.PulsNow();
}
}
return (int)Statics.Magic.Detours["EndScene"].CallOriginal(pointer);
}
private static void Shutdown()
{
Statics.Magic.Detours["EndScene"].Remove();
}
}
BotBase.cs:
Code:
class BotBase:Puls.Pulsator
{
readonly GuiManager gui = new GuiManager();
protected override void OnFirstFrame()
{
gui.Launch();
base.OnFirstFrame();
}
}
GuiManager.cs (Just a quick mock up based on Kryso's code http://dl.dropbox.com/u/1799304/Wow/GuiThread.cs)
Code:
class GuiManager
{
Thread _thread;
private MainView _view;
private Application _app;
private bool _running;
public GuiManager()
{
_thread = new Thread(ThreadWork);
_thread.SetApartmentState(ApartmentState.STA);
_thread.IsBackground = true;
}
public void Launch()
{
if (!_running)
{
_thread.Start();
_running = true;
}
}
public void Close()
{
}
private void ThreadWork()
{
try
{
_app = new Application();
_view = new MainView();
_app.Run(_view);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void CleanUp()
{
}
}
Basically all i need todo to make wow crash now is to force the WPF window to rerender by resizing it or whatever...
What exactly happens? Am I overlooking something obvious or is there a bug in Apocs lib?