这就是故事:
我想从DiretX 10 C ++代码制作一个vb.NET代码,函数Public Declare Function D3DX10CreateDeviceAndSwapChain Lib "d3dx10_43.dll" _
(ByVal pAdapter As IDXGIAdapter,
ByVal drivertype As D3D10_DRIVER_TYPE,
ByVal software As IntPtr,
ByVal flags As UInteger,
ByRef pswapchaindesc As DXGI_SWAP_CHAIN_DESC,
ByRef ppSwapChain As IntPtr,
ByRef ppDevice As IntPtr) As Integer`
可以和我一起使用:
Dim swapChainDesc As New DXGI_SWAP_CHAIN_DESC()
swapChainDesc.BufferCount = 1
swapChainDesc.BufferDesc.Width = Me.Width
swapChainDesc.BufferDesc.Height = Me.Height
swapChainDesc.BufferDesc.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM
swapChainDesc.BufferUsage = 1L << (1 + 4)
swapChainDesc.OutputWindow = Me.Handle
swapChainDesc.SampleDesc.Count = 1
swapChainDesc.SampleDesc.Quality = 0
swapChainDesc.Windowed = True
Dim sc As IntPtr
Dim d As IntPtr
D3DX10CreateDeviceAndSwapChain(Nothing,D3D10_DRIVER_TYPE.D3D10_DRIVER_TYPE_HARDWARE,IntPtr.Zero,0,swapChainDesc,sc,d)
其余代码是:
Dim s As IDXGISwapChain = CType(Marshal.GetTypedObjectForIUnknown(sc, GetType(IDXGISwapChain)), IDXGISwapChain)
Dim device As ID3D10Device = CType(Marshal.GetTypedObjectForIUnknown(d, GetType(ID3D10Device)), ID3D10Device)
Dim surface As ID3D10Texture2D
s.GetBuffer(0, Marshal.GenerateGuidForType(GetType(ID3D10Texture2D)), surface)
并将intptr转换为我使用的有效接口对象(它工作正常):
Dim rtv As ID3D10RenderTargetView
Dim hresult As Integer = device.CreateRenderTargetView(surface, Nothing, rtv)
surface.Release() ' this works fine
MsgBox(rtv IsNot Nothing)
直到这里代码返回成功。
{{1}}
问题是:
当我更改位置(它们之间的成员排名到接口ID3D10Device)时,我得到AccessViolationException或 该行的ArgumentException 昏昏沉沉的...... ...
为什么这种未定义的行为?
备注:界面ID3D10Device包含80多个成员,我也更改了一些成员位置,它们工作正常。