none
i got error when assining a pointer to a member RRS feed

  • Question

  • i have converted  the native C++ transparency project (from DirectX10 SDk 2010) to vb.NET, and the vb sample works fine but there is a point concerning assining a value (an IntPtr) to a member of a structure: a piece of code is:

    ' g_QuadVertices is an array of structure:

    Dim g_QuadVertices(0 To 5) As QuadVertex
            g_QuadVertices(0).v3Pos = New D3DXVECTOR3(-1.0F, -1.0F, 0.0F)
            g_QuadVertices(0).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(0).v2TexCoord = New D3DXVECTOR2(0.0F, 1.0F)
            g_QuadVertices(1).v3Pos = New D3DXVECTOR3(-1.0F, 1.0F, 0.0F)
            g_QuadVertices(1).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(1).v2TexCoord = New D3DXVECTOR2(0.0F, 0.0F)
            g_QuadVertices(2).v3Pos = New D3DXVECTOR3(1.0F, -1.0F, 0.0F)
            g_QuadVertices(2).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(2).v2TexCoord = New D3DXVECTOR2(1.0F, 1.0F)
            g_QuadVertices(3).v3Pos = New D3DXVECTOR3(-1.0F, 1.0F, 0.0F)
            g_QuadVertices(3).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(3).v2TexCoord = New D3DXVECTOR2(0.0F, 0.0F)
            g_QuadVertices(4).v3Pos = New D3DXVECTOR3(1.0F, 1.0F, 0.0F)
            g_QuadVertices(4).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(4).v2TexCoord = New D3DXVECTOR2(1.0F, 0.0F)
            g_QuadVertices(5).v3Pos = New D3DXVECTOR3(1.0F, -1.0F, 0.0F)
            g_QuadVertices(5).v3Normal = New D3DXVECTOR3(0.0F, 0.0F, -1.0F)
            g_QuadVertices(5).v2TexCoord = New D3DXVECTOR2(1.0F, 1.0F)

    Dim _all As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(QuadVertex)) * g_QuadVertices.Length) 
            Dim g_QuadVertices_p(0 To g_QuadVertices.Length) As IntPtr
            For i = 0 To g_QuadVertices.Length - 1
                g_QuadVertices_p(i) = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(QuadVertex)))
                Marshal.StructureToPtr(g_QuadVertices(i), g_QuadVertices_p(i), True)
            Next i

            Marshal.Copy(g_QuadVertices_p, 0, _all, g_QuadVertices_p.Length)
            InitData.pSysMem = _all

    so the sample does not show the quad (nothing is shown) but if the code above is replaced with the following code , it works

    BOOL ver2(QuadVertex sv[],  D3D10_SUBRESOURCE_DATA * InitData)
    {

    InitData->pSysMem = (void*)sv;

    return TRUE;
    }

    remark: the ver2 function is the bridge between the vb.NET project and the native C++ code:

    so finally , what is the equivalent vb.NET code for the line : InitData->pSysMem = (void*)sv;

    Wednesday, April 3, 2019 2:18 PM

All replies

  • may be the error is related to the member pSysMem of the object InitData, idon't know
    Wednesday, April 3, 2019 2:23 PM
  • If all of the values are structures, then try this:

       Dim h = GCHandle.Alloc(g_QuadVertices, GCHandleType.Pinned)

       Dim _all = h.AddrOfPinnedObject()

     

       InitData.pSysMem = _all

     

       ' . . .

     

       h.Free()

    Wednesday, April 3, 2019 5:41 PM
  • yes , you are correct
    Wednesday, April 3, 2019 8:02 PM
  • Hi,

    Is your problem solved? If so, please mark the useful replies as answers.  

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 4, 2019 2:42 AM
    Moderator