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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:49:57+00:00 2026-05-13T16:49:57+00:00

I have problems with marshalling output parameter of c++ function returning array of data

  • 0

I have problems with marshalling output parameter of c++ function returning array of data to c#.

Here is C++ declaration:

#define DLL_API __declspec(dllexport)

typedef TPARAMETER_DATA
{
    char        *parameter;
    int     size;
} PARAMETER_DATA;

int DLL_API GetParameters(PARAMETER_DATA *outputData);

The function allocates memory for char array, places the data there and returns the number of allocated bytes in “size” field.
Here is my c# declaration:

[StructLayout(LayoutKind.Sequential)]
public struct PARAMETER_DATA
{        
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 50000)]        
    public byte[] data;   // tried also SizeParamIndex = 1 instead of SizeConst

    [MarshalAs(UnmanagedType.I4)]                      
    public int size;
}

[DllImport("thedll.dll", SetLastError = true, ExactSpelling = true)]
public extern static uint GetParameters(ref PARAMETER_DATA outputData); // tried also  'out' parameter

When calling the function in c# I get empty structure (size=0, empty array). I tried passing outputData parameter with data feld initialized to new byte[50000] but no data is returned anyway.

Every other function in this dll (some with complex input structures) are working fine, but this is the only function that allocates memory to return data.
I tried many other C# marshalling declarations (with LPArray, LPString) with no luck – always empty data structure is returned or memory access exception is thrown.
Am I missing something simple here?

EDIT:

I cannot change the c++ code – it’s external library.

  • 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-13T16:49:58+00:00Added an answer on May 13, 2026 at 4:49 pm

    The problem you’re facing is that a pointer is returned – not really a string or an array. There is no way for the marshaller to convert the pointer to an array or string, because the length is unknown.

    The solution might be to do the pointer handling in c#. You should also figure out if you’re responsible for freeing the pointer, or that the library will do that for you.

    [StructLayout(LayoutKind.Sequential)]
    public struct PARAMETER_DATA
    {        
        public IntPtr data;   // tried also SizeParamIndex = 1 instead of SizeConst
    
        [MarshalAs(UnmanagedType.I4)]                      
        public int size;
    }
    
    [DllImport("thedll.dll", SetLastError = true, ExactSpelling = true)]
    private extern static uint GetParameters(ref PARAMETER_DATA outputData);
    
    public static uint GetParameters(out String result)
    {
        PARAMETER_DATA outputData = new PARAMETER_DATA();
        result= Marshal.PtrToStringAnsi(outputData.data, outputData.size );
        Marshal.FreeHGlobal(outputData.data); // not sure about this
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 286k
  • Answers 286k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like this is documented on the gdata-python-client wiki:… May 13, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer This page on the Mac Dev Center gives you thread… May 13, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer MainConcept has some excellent codec options. They offer support for… May 13, 2026 at 4:50 pm

Related Questions

A certain form in our application displays a graphical view of a model. The
When calling an unmanaged Dll from a c# application I get an AccessViolationException .
I am running into a problem which I am using C Dll into my
I have been trying to understand a bit more about the wider picture of
I have a very painful library which, at the moment, is accepting a C#

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.