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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:34:13+00:00 2026-05-26T05:34:13+00:00

I created a cross-platform DLL in C++ that compiles on both Windows and Mac

  • 0

I created a cross-platform DLL in C++ that compiles on both Windows and Mac OSX. On Windows, I have a C# app that calls the DLL using P/Invoke and on Mac OSX, an objective C app calls the DLL. I have simple functions working just fine but I need a new function that returns an array of integers.

The best example I can find is at Marshal C++ int array to C# and I was able to make it work. However, I would like to modify this example to pass the integer array back as a reference argument instead. The size of the array has to be set at runtime.

Here’s what I’ve tried. The pSize is coming back correctly but the list is empty.

In unmanaged c++:

bool GetList(__int32* list, __int32* pSize)
{

    // Some dummy data
    vector<int> ret;
    ret.push_back(5);
    ret.push_back(6);

    list = (__int32*)malloc(ret.size());
    for (unsigned int i = 0; i < ret.size(); i++)
    {
            list[i] = ret.at(i);
    }
    *pSize = ret.size();

    return true;
}

In C#:

[DllImport(@"MyDll.dll",
    CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
    public static extern bool GetList(out IntPtr arrayPtr, out int size);


public static int[] GetList() {
    IntPtr arrayValue = IntPtr.Zero;
    int size = 0;

    bool b = GetFrames(out arrayValue, out size);
    // arrayValue is 0 here

    int[] result = new int[size];

    Marshal.Copy(arrayValue, result, 0, size);

    return result;
}
  • 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-26T05:34:14+00:00Added an answer on May 26, 2026 at 5:34 am

    Your problem is the definition of list, it really needs to be an __int32** in order to pass back the address of the allocated array. To breeze through the interop difficulties of pointers-to-pointers, how about you instead return the address of list or null if it fails:

    __int32* GetList(__int32* pSize)
    {
        // Some dummy data
        vector<int> ret;
        ret.push_back(5);
        ret.push_back(6);
    
        // per @David's catch, you'll need to allocate the right amount
        __int32* list = (__int32*)malloc(ret.size() * sizeof(__int32));
        for (unsigned int i = 0; i < ret.size(); i++)
        {
                list[i] = ret.at(i);
        }
        *pSize = ret.size();
    
        return list;
    }
    
    void RemoveList(__int32* list)
    {
        free(list);
    }
    

    With the appropriate modifications to your C#:

    [DllImport(@"MyDll.dll",
     CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr GetList(out int size);
    
    [DllImport(@"MyDll.dll",
     CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
    private static extern void RemoveList(IntPtr array);
    
    public static int[] GetList()
    {
        int[] result = null;
        int size;
    
        IntPtr arrayValue = IntPtr.Zero;
        try
        {
            arrayValue = GetList(out size);
            if (arrayValue != IntPtr.Zero)
            {
                result = new int[size];
                Marshal.Copy(arrayValue, result, 0, size);
            }
        }
        finally
        {
            // don't forget to free the list
            RemoveList(arrayValue);
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All of my cross-platform libraries are created with solutions that have two projects: one
I want to create a C++ cross-platform (Windows and MacOS X) application that sends
Imagine a cross-platform library that has to create its own windows without relying on
I have simple console application project for live video streaming using cross-platform libs on
I have a requirement to create a cross platform application that launches a web
I have a cross platform c++ program where I'm using the boost libraries to
I'm implementing a cross-platform app for Android, iOS, and BlackBerry. I'm using PhoneGap to
For some time now I've been happily using dlmalloc for a cross-platform project (Windows,
I found cool article on Creating cross platform GUI's with IronRuby where someone re-created
Say I have created a GUI (Qt) that operates with some data(text+image). How I

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.