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 332105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:50:11+00:00 2026-05-12T09:50:11+00:00

Yet another one of my P/Invoke questions! I have this C function: int _ei_x_new(ei_x_buff*

  • 0

Yet another one of my P/Invoke questions! I have this C function:

int _ei_x_new(ei_x_buff* x);

Essentially, it initializes a new buffer struct. In C#, I have this:

[DllImport(EIDLL, EntryPoint = "_ei_x_new")]
public static extern int ei_x_new(out ei_x_buff x);

ei_x_buff is pretty simple:

typedef struct ei_x_buff_TAG {
    char* buff;
    int buffsz;
    int index;
} ei_x_buff;

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ei_x_buff {
    [MarshalAsAttribute(UnmanagedType.LPStr)]
    public string buff;
    public int buffsz;
    public int index;
}

But when I do this:

ei_x_buff buffer;
Ei.ei_x_new(out buffer);

I get an AccessViolationException:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Do I need to alloc some memory or something? It’s such a simple piece of code that I can’t see any glaring problems with it.

EDIT: Native code for _ei_x_new:

// In my wrapper library
DLL_EXPORT int _ei_x_new(ei_x_buff* x) {
    return ei_x_new(x);
}

// In external library being wrapped
int ei_x_extra = 100;

int ei_x_new(ei_x_buff* x)
{
    x->buff = malloc(ei_x_extra);
    x->buffsz = ei_x_extra;
    x->index = 0;
    return x->buff != NULL ? 0 : -1;
}
  • 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-12T09:50:11+00:00Added an answer on May 12, 2026 at 9:50 am

    Couple of things

    • Use out not ref in the PInvoke signature as you want data marshalled in both directions (into and out of the native function)
    • Can you post the native signature as their could be an issue with the definition. In particular the buff member

    EDIT

    Just to eliminate one possibility, switch the buff member to be typed IntPtr with no attributes. If that doesn’t cause a crash then it’s likely a marshalling issue with the string type.

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

Sidebar

Related Questions

Yet another one of my idiotic P/Invoke questions... Sometimes I think this stuff is
Yet another Scrabble project question... This is a simple one. It seems I am
Back again with yet another DirectSound question, this one regarding the ways DirectSound Buffers
Yet another PHP question. I've got two arrays: one string-based and the other numeric.
Yet another data structure doubt. I will get straight to it. This is what
I have yet another issue which the answer is eluding me. I wish to
ok, yet another jquery selector nightmare for me ! I've read about 20 questions
After getting a helpful answer here , I have run into yet another problem:
I have (yet another) powershell query. I have an array in powershell which i
Y.A.N.Q. (yet another n00b question). I have managed to design my db, defining tables

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.