Get the Direct3D Device?

A quick search of the two XNA forums and the XNA Connection didn't turn up an answer. Is there a way to get the underlying Direct3D Device I found a nice GUI library for ManagedDX, but I need to pass the Device to it.


Answer this question

Get the Direct3D Device?

  • jdmarsh2g

    Jim-

    Did you get that pointer to work for you

  • oaix

    I'd try disposing for Managed Direct3D device BEFORE disposing of the Xna device.
    I'd try disposing of it AFTER.
    If I was already disposing it, I'd try not disposing it.

    If you could isolate where the NullReferenceException was coming from that would help.

  • CoverPpl

    Disclaimer: You are not supposed to get the underlying pointer. Xna is an abstraction layer, and if you form a dependency on an underlying component then you defeat the purpose. You will certainly not be able to run on XBox 360 and your code will break whenever they update the software.

    With that out of the way if you REALLY think you need access to the IDirect3DDevice9*, then here is how you can get it:


    You have to import the System.Reflection namespace for this code, and you must compile with /unsafe.

    GraphicsDevice device = this.Device;
    FieldInfo field = typeof(GraphicsDevice).GetField("pComPtr", BindingFlags.NonPublic | BindingFlags.Instance);
    Pointer reflectionPointer = (Pointer)field.GetValue(device);
    void* voidPointer = Pointer.Unbox(reflectionPointer);
    //   if you want a managed direct3d device then add the following...
    Microsoft.DirectX.Direct3D.Device device = new Microsoft.DirectX.Direct3D.Device((IntPtr)voidPointer);



  • John_Mac

    Nice UI library - and not one I have come across before.

  • Chaman Zinga

    Thanks, but now the question becomes how do I create a Managed D3D Device from that pointer (if you even can)

  • jdonahue1971

    Any idea how to fix it so I don't have to waste time

  • DiamondDavo

    Hmmm, problems:

    DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX.Direct3D\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.Direct3D.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

    I created a GameComponent that tries to use the UI and creating an instance of that component in the Game causes this. I'm not sure I want to spend much time trying to get this to work.



  • project2n5e0o1

    Yeah, but I'm only doing it to get a GUI system during the beta and since we can't run on the 360 now anyway...  

    For those that are interested, here's the GUI system. The wiki was pretty slow earlier, so don't be surprised if it doesn't come right up.



  • Elad_23

    may have to do with reference counting on the IDirect3DDevice9 pointer.

  • Atul Bahl

    One of the managed Device constructors takes an IntPtr, just cast the void* to an IntPtr.

    Let us all know if you get this to work.

  • Lolchickenzor

    Well, it works, kind of. I'm getting a NullReferenceException when I close down the game. Getting there.



  • Radoslav

    Of course as soon as you take a dependency on Managed DirectX you are restricted to PC only.

  • JBondITW

    Ah that one is easy to work around http://www.thezbuffer.com/articles/304.aspx



  • donkaiser

    Had a Real Life interruption. Still working on it.

  • Get the Direct3D Device?