private static uint IDirect3D9CreateDeviceHandler(IntPtr thisPtr, uint adapter, uint deviceType, IntPtr hFocusWindow, uint behaviorFlags, IntPtr pPresentationParameters, [Out] IntPtr ppReturnedDeviceInterface)
        {
            uint ret;
            if (!_isXnaCreateDeviceCall)
            {
                try
                {
                    var nativePresentationParameters =
                        (NativePresentationParameters)Marshal.PtrToStructure(pPresentationParameters, typeof(NativePresentationParameters));
                    var presentationParameters = nativePresentationParameters.ToXnaPresentationParameters(hFocusWindow);

                    _preservedBehaviorFlags = behaviorFlags;
                    _isXnaCreateDeviceCall = true;

                    _xnaGraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach,
                                                            presentationParameters);

                    var pComPtrField = _xnaGraphicsDevice.GetType().GetField("pComPtr", BindingFlags.NonPublic | BindingFlags.Instance);
                    if (pComPtrField == null)
                        throw new Exception("Unable to get pComPtr field from XNA Graphics Device");

                    unsafe
                    {
                        var pComPtr = new IntPtr(Pointer.Unbox(pComPtrField.GetValue(_xnaGraphicsDevice)));
                        Marshal.WriteIntPtr(ppReturnedDeviceInterface, pComPtr);

                        _endSceneDetour = pComPtr.VTable(IDirect3DDevice9VTable.EndScene)
                            .DetourWith(EndSceneFunc);

                        _resetDetour = pComPtr.VTable(IDirect3DDevice9VTable.Reset)
                            .DetourWith(ResetFunc);
                    }
                    // TODO
                    OnCreateDevice();
                    ret = 0;
                }
                catch (Exception)
                {
                    // If we get an exception trying to create the XNA device, just call the original method and pass out the return
                    ret = (uint)_createDeviceDetour.CallOriginal(
                        thisPtr, adapter, deviceType, hFocusWindow,
                        behaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
                }
            }
            else
            {
                // Now we're inside the XNA Device's call to CreateDevice - get our cached presentation parameters and add a required flag
                // TODO: check this process / flag
                var pp = (NativePresentationParameters)Marshal.PtrToStructure(pPresentationParameters, typeof(NativePresentationParameters));
                pp.Flags |= 0x1;
                Marshal.StructureToPtr(pp, pPresentationParameters, true);

                ret = (uint) _createDeviceDetour.CallOriginal(
                    thisPtr, adapter, deviceType, hFocusWindow,
                    _preservedBehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
            }
            return ret;
        }