如何将 IntPtr 获取到本机 directx11 结构?

How to get IntPtr to a native directx11 struct?

我正在尝试在 C++ 中创建(并填充)一个 D3D11Buffer,通过互操作将指针传递给 C#,以便使用 slimdx 从该缓冲区读取 - 没有通过 [=60 的缓冲区副本=] 应该参与其中。

到目前为止,这是我的代码

函数导出:

extern "C" {__declspec(dllexport) HRESULT getValues(int * debug, ID3D11Buffer* bufOut); }

和函数:

ID3D11Device* d3d11Device;
ID3D11Buffer* triangleVertBuffer;
ID3D11DeviceContext* d3d11DevCon;

struct Vertex    //Overloaded Vertex Structure
{
    Vertex() {}
    Vertex(float x, float y, float z)
        : pos(x, y, z) {}

    XMFLOAT3 pos;
};

HRESULT getValues(int * debug, ID3D11Buffer* bufOut) {


    //Create the vertex buffer
    Vertex v[] =
    {
        Vertex(0.0f, 0.5f, 0.5f),
        Vertex(0.5f, -0.5f, 0.5f),
        Vertex(-0.5f, -0.5f, 0.5f),
    };

    D3D_FEATURE_LEVEL  FeatureLevelsRequested = D3D_FEATURE_LEVEL_11_0;
    UINT               numLevelsRequested = 1;
    D3D_FEATURE_LEVEL  FeatureLevelsSupported;

    //create device
    hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, &FeatureLevelsRequested, numLevelsRequested, D3D11_SDK_VERSION, &d3d11Device, &FeatureLevelsSupported, &d3d11DevCon);
    debug[0] = hr;

    D3D11_BUFFER_DESC vertexBufferDesc;
    ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(Vertex) * 3;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA vertexBufferData;


    ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
    vertexBufferData.pSysMem = v;
    hr2 = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);


    bufOut = triangleVertBuffer;

    debug[1] = hr2;

    return hr;
}

在 C# 中,我像这样导入函数:

[System.Runtime.InteropServices.DllImport("CreateID3D11Buffer.dll", CallingConvention = CallingConvention.StdCall)]
    private static unsafe extern UInt32 getValues([In, Out] int[] debug, [In, Out] IntPtr bufIn);

然后这样称呼它:

int[]debug = new int[2];
IntPtr bufPtr;// = IntPtr.Zero;
UInt32 hr = getValues(debug, bufPtr);
DataStream ds = new DataStream(bufPtr, sizeof(Vector3) * 3, true, false);

到目前为止一切顺利....现在解决我的问题(请耐心等待,仍然是 c++ 初学者和 c# 中级...):

那么,我这样做是不是都错了? :) 如果是这样,请指出正确的方向。

注意:大部分 cpp 代码是从 https://www.braynzarsoft.net/viewtutorial/q16390-4-begin-drawing

中提取的

感谢您 help/hints,这几天一直在努力解决这个问题。

ID3D11Buffer* 是一个指针。如果你想取回一个指针,你必须将一个指针传递给一个指针,所以像这样定义你的 C++ 方法:

HRESULT getValues(int * debug, ID3D11Buffer** bufOut)
{
    ...

    *bufOut = triangleVertBuffer;

    ...
}

C# 是这样的:

[DllImport("CreateID3D11Buffer.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int getValues([In, Out] int[] debug, out IntPtr bufIn);

这部分应该可以。