Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Marshalling Variable-Size Array

Marshalling Variable-Size Array

Scheduled Pinned Locked Moved Visual Basic
c++databasedata-structuresjsonperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VFaul
    wrote on last edited by
    #1

    I'm calling an unmanaged native WLAN API method, WlanEnumInterfaces, that returns a pointer to a structure (WLAN_INTERFACE_INFO_LIST). There are three components to the structure. The third component is an array (WLAN_INTERFACE_INFO) of variable size. The first component in the structure is the number of items in the array. I'm having trouble marshalling the data. I am using Marshal.PtrToStructure. I get the following exception: "Cannot marshal field 'InterfaceList' of type "WLAN_INTERFACE_INFO_LIST": Invalid managed/unmanaged type combination (Arrays fields must be paired with ByValArray or SafeArray)." Since the array size is variable, I marshal using UnmanagedType.LPArray and SizeParamIndex. Below is my code. The call to WlanEnumInterfaces() is successful and I can view the contents of the structure in memory. The exception occurs at Marshal.PtrToStructure. _ Public Structure WLAN_INTERFACE_INFO Public InterfaceGuid As GUID Public strInterfaceDescription() As String Public isState As WLAN_INTERFACE_STATE End Structure _ Public Structure WLAN_INTERFACE_INFO_LIST Public NumberOfItems As UInteger Public Index As UInteger Public InterfaceList() As WLAN_INTERFACE_INFO End Structure Dim pTemp As IntPtr Dim WlanInterfaceList As WLAN_INTERFACE_INFO_LIST ui32Status = WlanEnumInterfaces(WlanHandle,IntPtr.Zero, pTemp) WlanInterfaceList = Marshal.PtrToStructure(pTemp, GetType(WLAN_INTERFACE_INFO_LIST)) Any help is appreciated. VF

    D 1 Reply Last reply
    0
    • V VFaul

      I'm calling an unmanaged native WLAN API method, WlanEnumInterfaces, that returns a pointer to a structure (WLAN_INTERFACE_INFO_LIST). There are three components to the structure. The third component is an array (WLAN_INTERFACE_INFO) of variable size. The first component in the structure is the number of items in the array. I'm having trouble marshalling the data. I am using Marshal.PtrToStructure. I get the following exception: "Cannot marshal field 'InterfaceList' of type "WLAN_INTERFACE_INFO_LIST": Invalid managed/unmanaged type combination (Arrays fields must be paired with ByValArray or SafeArray)." Since the array size is variable, I marshal using UnmanagedType.LPArray and SizeParamIndex. Below is my code. The call to WlanEnumInterfaces() is successful and I can view the contents of the structure in memory. The exception occurs at Marshal.PtrToStructure. _ Public Structure WLAN_INTERFACE_INFO Public InterfaceGuid As GUID Public strInterfaceDescription() As String Public isState As WLAN_INTERFACE_STATE End Structure _ Public Structure WLAN_INTERFACE_INFO_LIST Public NumberOfItems As UInteger Public Index As UInteger Public InterfaceList() As WLAN_INTERFACE_INFO End Structure Dim pTemp As IntPtr Dim WlanInterfaceList As WLAN_INTERFACE_INFO_LIST ui32Status = WlanEnumInterfaces(WlanHandle,IntPtr.Zero, pTemp) WlanInterfaceList = Marshal.PtrToStructure(pTemp, GetType(WLAN_INTERFACE_INFO_LIST)) Any help is appreciated. VF

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      VFaul wrote:

      Public InterfaceList() As WLAN_INTERFACE_INFO

      Loose the SizeParamIndex field, it's only used in COM. Replace it with SizeCont:=10. Before you call the function that is returning data in this structure, create 10 entries in your InterfaceList array so the memory is reserved for the funtion to pass data back in. You cannot pass variable length arrays because the Marshaler has no way of determining how many elements are comming back to managed code or how to read the format of the memory block. The Marshaler can copy back a single structure with PtrToStructure. It cannot handle variable length structures! You can find some more tidbits from this[^]. It IS possible to do though! You have to supply the code to walk the memory block and pick out the values yourself. I do NOT recommend doing this because you'll be lucky to get any help making it work. It's a technique that is very very rarely ever used. There's just about no support base for it. But, for the curious -> Here it is[^].

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups