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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:59:52+00:00 2026-06-17T15:59:52+00:00

In C code, the function is defined like: INT WINAPI myFunction(LPCTSTR str1, LPCTSTR str2,

  • 0

In C code, the function is defined like:

INT WINAPI myFunction(LPCTSTR str1, LPCTSTR str2, INT iNumber,
    LPSTRUCT *lpStruct);

*lpStruct is an array of pointers of a struct type:

typedef struct myStruct
{
CHAR                    m_s1[64];       
UINT                    m_nS;       
CHAR                    m_s2[8][64];    
UINT                    m_nP;
CHAR                    m_s3[512];

} SomeStruct, *LPSTRUCT;

I need to call this external myFunction in C#, I defined SomeStruct as:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SomeStruct
{

   [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]
   public string m_s1;             
   public uint m_nS;             
   [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
   public string m_s2;             
   public uint m_nP;
   [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 512)]
   public string m_s3;
 }

myFunction as:

[DllImport("some.dll")]
public static extern int myFunction(
             string str1,
             string str2,
             int iNumber,
             IntPtr[] lpStruct);

I initialize IntPtr[] in C#:

IntPtr[] lpptr = new IntPtr[iNumber]; 

I know the struct array of pointer has iNumber elements.

There is no error to call this function (lpStruct[i] has number). But when I tried to Marshal the pointer to the struct using:

SomeStruct st = (SomeStruct )Marshal.PtrToStructure(lpStruct[i], 
    typeof(SomeStruct ));

I got error message: try to write read-only memory. I don’t know what’s wrong here. Is the external function definition in C# wrong or the definition of struct wrong, or both.

  • 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-06-17T15:59:52+00:00Added an answer on June 17, 2026 at 3:59 pm

    The question lacks important details, but I would expect that you need to allocate memory for each IntPtr in the array.

    IntPtr[] lpptr = new IntPtr[iNumber];
    for (int i=0; i<iNumber; i++)
        lpptr[i] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SomeStruct)));
    

    And obviously you should free the memory once you are done with it.

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

Sidebar

Related Questions

I have a structure defined like so: typedef struct { int n; int *n_p;
I often see code a function defined without visibility keywords. e.g: class Foo() {
I have the following object defined function BusinessUnit(id, name, code) { this.id = ko.observable(id);
I'm sending the code of a javascript function that is defined in the parent
this code produces 17 error C2995: function template has already been defined; there was
I have to extract defined variable and function names from a js code passed
I'd like to call a custom function that is defined in a Python module
I have a variable in the argument of a function that is defined like
I'm trying to manipulate the output of Wordpress' the_date() function. My code: <?php define('WP_USE_THEMES',
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom

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.