Using P/Invoke to call a pointer to a pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harter.jim@gmail.com

    Using P/Invoke to call a pointer to a pointer

    I am currently calling a 3rd party external library using P/Invoke.
    The library gives me a handful of functions that I need to call. I
    have been successful at calling many of them, but I am some trouble
    calling a certain function that requires a pointer to a pointer. Here
    is the relevant header information:

    typedef struct _ITEM_INFO
    {
    int itemID;
    int itemValue;
    } ITEM_INFO;

    typedef struct _ITEMS
    {
    int count; /* number of items */
    ITEM_INFO * itemList; /* list of iteminfo */
    } ITEMS;

    RESULT_CODE WINAPI GetItemList(ITE MS ** itemList);

    I need a way to call the GetItemList. In the end, I need the list of
    itemIDs and their associated values. I found an article on a technique
    for calling a pointer to a pointer to a struct at
    http://www.vsj.co.uk/articles/display.asp?id=501. That technique used
    the Marshal.AllocHG lobal and Marshal.Structu reToPtr methods, but the
    Marshal.AllocHG lobal requires a size parameter for the ITEMS structure.
    However, this structure does not have a constant size due to the list
    inside of it. Does anyone know how I might be able to call this
    function from C#?

    Thanks for your help.

    Jim

  • Mattias Sjögren

    #2
    Re: Using P/Invoke to call a pointer to a pointer

    >I need a way to call the GetItemList. In the end, I need the list of
    >itemIDs and their associated values. I found an article on a technique
    >for calling a pointer to a pointer to a struct at
    >http://www.vsj.co.uk/articles/display.asp?id=501. That technique used
    >the Marshal.AllocHG lobal and Marshal.Structu reToPtr methods, but the
    >Marshal.AllocH Global requires a size parameter for the ITEMS structure.
    Are you sure you have to allocate the memory? From the signature it
    looks like the caller allocates and returns a pointer through the only
    parameter. In that case, just type it as out IntPtr on the C# side.

    However, this structure does not have a constant size due to the list
    >inside of it.
    The struct itself still has a constant size (sizeof(int) +
    sizeof(ITEM_INF O*)).


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    Working...