Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3617276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:36:13+00:00 2026-05-18T22:36:13+00:00

Folks, Please help me chase out a SafeArrayTypeMismatchException that I’m getting. I need to

  • 0

Folks,

Please help me chase out a SafeArrayTypeMismatchException that I’m 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 or insight is really appreciated!

I can post my code if needed.

– Nick

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:36:13+00:00Added an answer on May 18, 2026 at 10:36 pm

    The .NET marshaller can’t handle C/C++ arrays of unknown size. An array in .NET always has a fixed size associated with it but C/C++ arrays are just a pointer to a memory block. The marshaller has no way to know how large the array returning from the C/C++ code is, so it throws an exception.

    What it’s is trying to do in your case is to marshal the array as a SafeArray which is a COM type – an array that contains it’s own size, but you’re array isn’t a SafeArray.

    There isn’t a way to get the marshaller to handle this automatically. Declare the struct member you are using as an IntPtr and manually create .NET array and copy the values in. See this for an example of how to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

yes, this is a homework-type question but could you please help me out? In
Folks, Please can anybody imagine a nice way of defining a struct in C#
Folks, please advice is there are any free available package(s) for OAuth? I need
I am fighting this thing and cannot figure it out... can someone please help.
Hi folks would really appreciate your help in answering this rather involved question. Please
Folks I am surprised to see my code working, that I dont have any
Folks, We are developing an application that is part a downloadable WinForms application that
Folks, is there a collection of gotchas where Numpy differs from python, points that
I'm a bit of a WPF newbie, so go easy on me please folks!
Howdi folks. I'm currently in the process of creating a shared library (aka DLL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.