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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:07:08+00:00 2026-05-14T03:07:08+00:00

I have a C function with the following signature: int my_function(int n, struct player

  • 0

I have a C function with the following signature:

int my_function(int n, struct player **players)

players is a pointer to an array of pointers to struct player objects. n is the number of pointers in the array. The function does not modify the array nor the contents of the structures, and it does not retain any pointers after returning.

I tried the following:

[DllImport("mylibary.dll")]
static extern int my_function(int n, 
    [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] 
     player_in []players);

However, that marshals the data as a pointer to an array of structures, not a pointer to an array of pointers to structures.

  • 1 1 Answer
  • 1 View
  • 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-14T03:07:08+00:00Added an answer on May 14, 2026 at 3:07 am

    I believe you’ll have to do some of the marshaling manually. The function declaration should look like this:

    [DllImport("mylibary.dll")]
    private static extern int my_function(int n, IntPtr players);
    

    We’ll need to allocate some native memory and marshal the structures to it before passing it in to the native function:

    private static void CallFunction(Player[] players)
    {
        var allocatedMemory = new List<IntPtr>();
    
        int intPtrSize = Marshal.SizeOf(typeof(IntPtr));
        IntPtr nativeArray = Marshal.AllocHGlobal(intPtrSize * players.Length);
        for (int i = 0; i < players.Length; i++)
        {
            IntPtr nativePlayer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Player)));
            allocatedMemory.Add(nativePlayer);
            Marshal.StructureToPtr(players[i], nativePlayer, false);
    
            Marshal.WriteIntPtr(nativeArray, i * intPtrSize, nativePlayer);
        }
    
        my_function(players.Length, nativeArray);
    
        Marshal.FreeHGlobal(nativeArray);
    
        foreach (IntPtr ptr in allocatedMemory)
        {
            Marshal.FreeHGlobal(ptr);
        }
    }
    

    If your native function is going to hold on to and re-use these memory locations, this won’t work. If this is the case, either hold off on freeing the memory until you think it’s not being used anymore or in the native method copy the data passed in and let the managed side clean up its memory immediately after the call.

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

Sidebar

Related Questions

I have been trying to call a C function that has the following signature
Let's say I have the following signature: static extern void External(int foo, IntPtr bar);
I have a static library. This library have the following function defined int WriteData(LPTSTR
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I have a function that is exported by a C library with the following
I have a function in an external library that I cannot change with the
I have a C#.NET 2.0 CF application that imports a function from a native
Suppose I have a function which takes a nullary functor as an argument: void
I have some C structures related to a 'list' data structure. They look like
I'm currently self-studying C for mastering an university project. In that project we have

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.