//private Direct3DTexture9 GetSharedSurface(Direct3DDevice9 device)
            //{

            //    return device.CreateTexture(
            //        m_width,
            //        m_height,
            //        1,
            //        1,  //D3DUSAGE_RENDERTARGET
            //        Format,
            //        0  //D3DPOOL_DEFAULT
            //        );
            //}



            private static Interop.Direct3DDevice9Ex CreateDevice(IntPtr handle)
            {
                const int D3D_SDK_VERSION = 32;

                using (var d3d9 = Interop.Direct3D9Ex.Create(D3D_SDK_VERSION))
                {
                    var present = new Interop.NativeStructs.D3DPRESENT_PARAMETERS();

                    try
                    {
                        var wih = new System.Windows.Interop.WindowInteropHelper(App.Current.MainWindow);

                        if (wih.Handle != IntPtr.Zero)
                        {
                            handle = wih.Handle;
                        }
                    }
                    catch (Exception)
                    {
                    }

                    present.Windowed             = 1;                           // TRUE
                    present.SwapEffect           = 1;                           // D3DSWAPEFFECT_DISCARD
                    present.hDeviceWindow        = handle;
                    present.PresentationInterval = unchecked ((int)0x80000000); // D3DPRESENT_INTERVAL_IMMEDIATE;

                    return(d3d9.CreateDeviceEx(
                               0,  // D3DADAPTER_DEFAULT
                               1,  // D3DDEVTYPE_HAL
                               handle,
                               70, // D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE
                               present,
                               null));
                }
            }
        public Direct3DDevice9 CreateDevice(uint Adapter, int DeviceType, IntPtr hFocusWindow, int BehaviorFlags,
                                            NativeStructs.D3DPRESENT_PARAMETERS pPresentationParameters, NativeStructs.D3DDISPLAYMODEEX pFullscreenDisplayMode)
        {
            ComInterface.IDirect3DDevice9 obj = null;
            int result = this.createDevice(this.comObject, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, out obj);

            Marshal.ThrowExceptionForHR(result);

            return(new Direct3DDevice9(obj));
        }