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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:52:18+00:00 2026-06-14T12:52:18+00:00

Say, I’ve got that prototype of a function that is exposed on a DLL:

  • 0

Say, I’ve got that prototype of a function that is exposed on a DLL:

int CALLBACK worker (char* a_inBuf, int a_InLen,
                     char** a_pOutBuf, int* a_pOutLen,
                     char** a_pErrBuf, int* a_pErrLen)

I’m sure that it’s ridiculously easy to call that DLL function from my C# code but it doesn’t work with this code:

[DllImport("mydll.dll")]  
     public static extern int worker(
         [In, MarshalAs(UnmanagedType.LPArray)] byte[] inBuf,  
         int inputLen,  
         [Out, MarshalAs(UnmanagedType.LPArray)] byte[] outBuf,  
         out int outputLen,  
         [Out, MarshalAs(UnmanagedType.LPArray)] byte[] errBuf,  
         out int errorLen);

... 

int outputXmlLength = 0;
int errorXmlLength  = 0;

byte[]  outputXml = null;
byte[]  errorXml  = null;
worker(input, input.Length, output, out outputLength, error, out errorLength);

I get an access violation when I’m going to fetch the memory for output and error inside my unmanaged library (and therefore de-reference the passed pointer):

*a_ppBuffer = (char*) malloc(size*sizeof(char));
  1. How do I write the DLLIMPORT statement in my C# code for this function?

  2. How do I actually call the function so that a_pOutBuf and a_pErrBuf are accessible and not null from within worker (i.e. use a real double pointer)?

  • 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-14T12:52:19+00:00Added an answer on June 14, 2026 at 12:52 pm

    Your current definition will not work. The worker function is allocating memory inside of the function and writing to that memory.

    The P/Invoke layer does not support marshaling C-style arrays that are allocated in this way, as it has no way of knowing just how large the array will be when the call returns (unlike say, a SAFEARRAY).

    That’s also why returning pointers to arrays from API functions is generally a bad idea, and the Windows API is written in such a way that the memory allocation is handled by the caller.

    That said, you want to change the P/Invoke declaration of worker to this:

    [DllImport("mydll.dll")]  
    public static extern int worker(
        [In, MarshalAs(UnmanagedType.LPArray)] byte[] inBuf,  
        int inputLen,  
        ref IntPtr outBuf, 
        ref int outputLen,  
        ref IntPtr errBuf, 
        ref int errorLen
    );
    

    In doing this, you’re indicating that you’re going to marshal the arrays manually (the outBuf and errBuf parameters are going to be set for you); you’re passing the reference to the pointer (double-indirection, that’s your char**) and then have to read from them using other indicators for bounds checking (in this case, the outputLen and errorLen parameters).

    You would marshal the data out of the pointers upon return like so:

    int outputXmlLength = 0;
    int errorXmlLength  = 0;
    
    IntPtr output = IntPtr.Zero;
    IntPtr error = IntPtr.Zero;
    
    worker(
        input, 
        input.Length, 
        ref output, 
        ref outputLength, 
        ref error, 
        ref errorLength
    );
    
    // Get the strings.
    string outputString = Marshal.PtrToStringAnsi(
        output, 
        outputLength
    );
    string errorString = Marshal.PtrToStringAnsi(
        error, 
        errorLength
    );
    

    That said, you have another problem. Because the memory was allocated inside the function, you have to free the memory. Since you’re using malloc to allocate the memory, you need to pass the two IntPtr instances back to unmanaged code in order to have free called on them.

    If you were allocating memory in unmanaged code using LocalAlloc or CoTaskMemAlloc then you could use the FreeHGlobal or FreeCoTaskMem methods respectively on the Marshal class to free the memory on the managed side.

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

Sidebar

Related Questions

Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say I have a Student table, it's got an int ID. I have a
Say I have function with_foo that takes a block, and wrap it around a
Say I have a SSI script that uses exec, or a PHP script that
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say I have 2 div s with the same CSS class that are stacked
Say, if we already have aScreen that points to the main screen, by UIScreen
Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Say I have the following template function: template <class T> void apply(const vector<complex<T> >&

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.