none
Access Violation Exception in Marshal.PtrToStructure in vb.net RRS feed

  • Question

  • I have below code snippet, which gets a pointer from a C API which is defined in a dll. Using marshalling I am trying to get the structure array, which is my requirement

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Public Class vb_menu_dotnet
    Public level As Short
    Public menu_id As String
    End Class
    Dim current As IntPtr
    Dim outArray As IntPtr
    Dim manArray(100) As vb_menu_dotnet
    vb_dotnet_get_menu_hierarchy(p_menu_handle, p_apl_id, outArray) //C API call
    current = outArray
    Dim j As Integer
    For j = 1 To 100
    manArray(j) = New vb_menu_dotnet()
    Marshal.PtrToStructure(current, manArray(j)) //Access Violation Exception

    ==========

    The prototype of C API is as below

    vb_dotnet_get_menu_hierarchy(tcodss_handle_t p_menu_handle,char* p_apl_id,vb_menu_dotnet** p_menu_array)
    Structure defination :
    typedef struct
    {
    short level;
    char* menu_id;
    } vb_menu_dotnet;

    The same code snippet works when both dll and above code is built with x86 option. But when ran with x64 option i get AccessViolation Exception at, Marshal.PtrToStructure(current, manArray(j))

    Thursday, April 18, 2013 2:44 PM

Answers

  • You may just want to to build the code you have a sepeerate project and then and this project as a reference to your main project.

    jdweng

    Friday, April 19, 2013 10:53 AM