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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:01:17+00:00 2026-05-17T17:01:17+00:00

I am using API written in C++ in my code (writting in C#). API

  • 0

I am using API written in C++ in my code (writting in C#).
API requires a parameter as Pointer to Structure.
The Structure consists of “Int”s and Char Arrays:
for example

 unsafe public struct ToBePassed 
    { 
        Int32 Num1;
        Int32 Num2; 
        Char[] Data; // or fixed Char Data[255];
    }

I can not directly pass the structure pointer to API because in that case, I am getting error as “Pointers cannot reference Marshaled structures”. Code get compiled successfully but this Error comes when I execute (Debug) the code.

Now I have two options:
1st:- Passing Structure by Ref: I want to ask does an API requiring A Structure Pointer can receive the address when I pass the structure by ref. Note that API will return Data in “Char[] Data”.

2nd:- Using Marshal.StructureToPtr: This will convert Structure Pointer to IntPtr. Again the Doubt is same, Will that API receive it correctly?

Thanks for Your Time!

Regards,
Swanand

  • 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-17T17:01:17+00:00Added an answer on May 17, 2026 at 5:01 pm

    If it only requires pointer, you can allocate some unmanaged memory, marshal the structure to the memory, and pass that pointer to your function. Then afterwards you could marshal back to the structure (if you wish) and free the memory. Before you marshal anything, you need to properly define the structure. Something like this:

    [StructLayout(
        LayoutKind.Sequential,      //must specify a layout
        CharSet = CharSet.Ansi)]    //if you intend to use char
    public struct ToBePassed
    {
        public Int32 Num1;
        public Int32 Num2;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
        public Char[] Data;    //specify the size using MarshalAs
    }
    
    [DllImport("...")]
    public static extern void APICall(IntPtr argPtr);
    
    
    public static void CallFunction(ToBePassed managedObj)
    {
        IntPtr unmanagedAddr = Marshal.AllocHGlobal(Marshal.SizeOf(managedObj));
    
        Marshal.StructureToPtr(managedObj, unmanagedAddr, true);
    
        APICall(unmanagedAddr);
    
        Marshal.PtrToStructure(unmanagedAddr, managedObj);
    
        Marshal.FreeHGlobal(unmanagedAddr);
        unmanagedAddr = IntPtr.Zero;
    }
    

    [edit]
    To simulate variable length arrays, allocate unmanaged memory within the structure and initialize as usual.

    [StructLayout(LayoutKind.Sequential)]
    public struct SomeStruct
    {
        public Int32 X;
        public Int32 Y;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct VLA
    {
        public Int32 intArrayLength;
        public Int32 SomeStructArrayLength;
        public IntPtr intArray;
        public IntPtr SomeStructArray;
    }
    
    public static VLA CreateVLA(int[] intArray, SomeStruct[] SomeStructArray)
    {
        var vla = new VLA()
        {
            intArrayLength = intArray.Length,
            SomeStructArrayLength = SomeStructArray.Length,
            intArray = Marshal.AllocHGlobal(intArray.Length * Marshal.SizeOf(typeof(int))),
            SomeStructArray = Marshal.AllocHGlobal(SomeStructArray.Length * Marshal.SizeOf(typeof(SomeStruct))),
        };
        Marshal.Copy(intArray, 0, vla.intArray, intArray.Length);
        //there's no overload to copy arbitrary arrays, do it manually
        for (int i = 0; i < SomeStructArray.Length; i++)
        {
            Marshal.StructureToPtr(
                SomeStructArray[i],
                vla.SomeStructArray + i * Marshal.SizeOf(typeof(SomeStruct)),
                true);
        }
        return vla;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've converted an api written in Java into a .net dll using IKVM, and
I have a written up HTML file using the Google Maps API v3. All
Since $(table).delegate(td, hover, function(){ $(this).toggleClass(hover); }); Is equivalent to the following code written using
I have written some code which checks a site for broken links using the
I am having to re-write an existing REST API using .NET (originally written with
I'm creating a small service using api-libraries, such as Twitter. Is it possible to
I'm trying to send post using API feed. I set fields: message , link
I want to update Paypal Order status and add tracking number using API. but
When using PHP API posts ' likes comes in this detailed format [likes] =>
I'm using Youtube API, I'd like to have a search auto-complete feature, just like

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.