none
Marshalling an array RRS feed

  • Question

  • I have to call an existing C++ dll (un-managed) - I have to pass a varialble array in C# the current definition of the structure is: struct WMSItemAlias { [MarshalAs(UnmanagedType.I2)] public short Action; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)] public char[] WAREHOUSE_ID; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)] public char[] VENDOR; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)] public char[] ITEM_ALIAS; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)] public char[] PRIOR_ALIAS; } the dll calls looks like: class InsertItem { [DllImport("wmsesi.dll")] public static extern int InsertItemMaster(System.Int32 WsHandle,System.Int32 countAlias, ref WMSItemAlias[] ItemAlias); } and the call of the method looks like: WsReturn = InsertItem.InsertItemMaster(WsHandle, countAlias, ref ItemAlias); - when I do this, the called program does not get the array - if I change the call to specify the "[0]" and the dll call to remove the "[]" from type- then the called program gets the first occurence in the array- How do I get the calling program to marshall and pass the whole array?
    Wednesday, September 2, 2009 3:54 PM

Answers

  • found out how to do-
    set up 2 structures- one describes an "entry" (row) and the other describes the Array using the former as the TYPE

     

    struct WMSItemAlias

    {

    [

    MarshalAs(UnmanagedType.I2)]

     

    public short Action;

    [

    MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]

     

    public char[] WAREHOUSE_ID;

    [

    MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]

     

    public char[] VENDOR;

    [

    MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]

     

    public char[] ITEM_ALIAS;

    [

    MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]

     

    public char[] PRIOR_ALIAS;

    }

     

    struct WMSItemAliasActual

    {

    [

    MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 100)]

     

    public WMSItemAlias[] WMSItemAliasEntry;

    }

    then fill in the rows in the second and pass it (not as an Array

    • Marked as answer by Tony Logility Wednesday, September 2, 2009 7:31 PM
    Wednesday, September 2, 2009 7:31 PM