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

  • Home
  • SEARCH
  • 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 4564888
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:33:56+00:00 2026-05-21T18:33:56+00:00

Hey! I’ve just begun fiddling with pinvoke and have encountered a problem. I’m getting

  • 0

Hey!
I’ve just begun fiddling with pinvoke and have encountered a problem. I’m getting the AccessViolationException. First of all, is there some way to debug or trace out which field is causing this error? The only thing being written to is the result struct.

The c++ call looks like:

MyFunc(int var1, _tuchar *var2, _tuchar *var3, _tuchar *var4, MyStruct *Result,
       _tuchar *var5, _tuchar *var6);

The c++ struct:

typedef struct MyStruct 
{
   _tuchar *id;
   _tuchar *ErrorMessages;
   int int1; 
   _tuchar language[3]; 
   _tuchar *result;
   int type;
   int number;
   int *type2; 
   _tuchar **blocks;
}

The C# struct:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
    [MarshalAs(UnmanagedType.LPStr)]
    public string Id;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=500)]
    public char[] ErrorMessages;

    public int int1;

    [MarshalAs(UnmanagedType.LPStr)]
    public string language;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
    public char[] result;

    public int type;

    public int number;

    public int type2;

    [MarshalAs(UnmanagedType.ByValArray)]
    public string[] blocks;

The C# method declaration:

[DllImport(MyPath, EntryPoint = "MyEntryPoint", SetLastError = true,
           CharSet = CharSet.Unicode)]
internal static extern int MyFunc(int var1, string var2, string var3,
      string var4, ref MyStruct Result, string var5, string var6);

The C# Call:

var result = new MyStruct();
MyFunc(0, "var2", "var3", "var4", ref result, "var5", "var6");

Hope I haven’t left anything out.
Thanks for any help!

  • 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-21T18:33:57+00:00Added an answer on May 21, 2026 at 6:33 pm

    Ooooh, man! You’ve picked quite a complex case for your first fiddling experience. I recommend doing something simpler first, and then moving on to the real stuff.

    Firstly, CharSet=CharSet.Ansi looks suspicious. All your strings and chars are _tuchar, and I gather the u in there means “Unicode”, doesn’t it? If that’s the case, you need CharSet=CharSet.Unicode.

    Secondly, (and this is the most likely culprit) why is the ErrorMessages field marshaled as ByValArray? You know that ByVal here means “by value”, don’t you? As in, not by reference. And you know that little asterisk thing in C++ means “reference”, don’t you? So why does your reference field ErrorMessages marshaled as a by-value array? In case you don’t know, an array is generally said to be passed “by value” when all of it’s content is being passed, instead of just passing a reference (pointer) to a memory location where all that content is stored. In C++ struct definition, you specify _tuchar*, which means “a reference (pointer) to some memory containing one or more of _tuchars”, whereas in C# you specify [MarshalAs(UnmanagedType.ByValArray, SizeConst=500)], which means “500 _tuchars are supposed to be here, no more and no less”. Seeing how a reference (pointer) usually takes 4 bytes (or 8 bytes on 64bit machines), and 500 unicode characters take 1000 bytes, you have an obvious mismatch right here.

    Thirdly and fourthly, same point goes for result and blocks fields.

    Fifthly, the language field is exactly reverse situation: the C++ code says “there are 3 _tuchars here”, while C# code says “there is a reference (pointer) to a string here” (in case you don’t know, LPStr means “Long Pointer to STRing”)

    And finally, after you have fixed all those problems, I recommend you execute your program and print out the result of call to Marshal.SizeOf( typeof( MyStruct ) ). That will give you exactly how big your struct is, in .NET’s opinion. The go on the C++ side and print out sizeof( MyStruct ). That will give you what C++ thinks about the size.

    If they turn out different, see what’s wrong. Try to remove fields one by one, until they become same. This will give you the culprit field(s). Work with them.

    Overall, I suggest you need a better understanding of how things work first. This case is waaaay too complex for a beginner.

    Good luck!

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hey all i have a JSFiddle that i have been working on with trying
Hey all i am wondering why my code below is only writing the first
Hey all, just wondering what the best way to add this capability was. I
Hey all- I have looked this up on here and Google but none of
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey I have been looking all over the internet and this site for hours
Hey I have been following gcm tutorial to create gcm-demo-server and a demo-client. All
Hey all i am wondering why i am getting this error with the following
Hey, I have a problem sharing a link on twitter: data-url=http://ipofmywebsite/mypage/?some_id=276&some_name=b+a As I put

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.