CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: SafeArrayTypeMismatchException

  1. #1
    Join Date
    May 2001
    Silicon Valley

    Red face SafeArrayTypeMismatchException


    Please help me chase out a SafeArrayTypeMismatchException that I keep getting. I need to pass a struct to unmanaged DLL. One of the struct members is a variable-length array. Unmanaged code will populate it with data, then my C# code will use the data.

    My approach is this:
    1. Get IntPtr for my structure using StructureToPtr(...), do the memory allocation too, of course.
    2. Call unmanaged function, and pass IntPtr as a parameter
    3. Get the populated structure using PtrToStructure(...)

    If for the purposes of an exercise I call StructureToPtr(...) and PtrToStructure(...) back-to-back there are no exceptions.

    PtrToStructure(...) generates SafeArrayTypeMismatchException, if I put a call to unmanaged DLL between StructureToPtr(...) and PtrToStructure(...). The description for SafeArrayTypeMismatchException is "Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata."

    Any suggestion, insight or reference is really appreciated!

    I can post my code if needed.

    - Nick
    Last edited by kender_a; December 27th, 2010 at 06:26 PM.

  2. #2
    Join Date
    Jan 2010

    Re: SafeArrayTypeMismatchException

    What version of the framework you're using?

    Check out this link, the article seems to be related to your problem:
    (And here's a related MSDN page: DispatchWrapper Class)

    P.S. The important part of the VB code in the article translates as:
    DispatchWrapper[] dispArray = new DispatchWrapper[9];
    for (int i = 0; i < 9; i++)
    	// I'll assume that the objs array already exists
    	dispArray[i] = new DispatchWrapper(objs[i]);
    // And then just pass dispArray instead.

  3. #3
    Join Date
    May 2001
    Silicon Valley

    Resolved Re: SafeArrayTypeMismatchException

    Attributes in my struct weren't correct. I've figure that out with some help from the community.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)