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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:45:26+00:00 2026-06-01T14:45:26+00:00

I am trying to call a function on a unmanaged C++ DLL, searching stackoverflow

  • 0

I am trying to call a function on a unmanaged C++ DLL, searching stackoverflow posts I came up close but I cant get it to fully work.

With a declaration in the .h file as follows:

extern int SomeDLLMethod(const char **data, int *count);

the data is a string

I have declared in C# as follows:

[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int SomeDLLMethod(IntPtr data, ref int count);

Then i can call it from C# as follows:

unsafe
{
    fixed (byte* buffer = new byte[MAX_LENGTH])
    {
        IntPtr ptr = new IntPtr(buffer);
        int count = 0;
        var retVal = SomeDLLMethod(ptr, ref count);
        var dataString = Marshal.PtrToStringAuto(ptr);
        Console.WriteLine(dataString);
     }
 }

The call succeeds, there is a count and data in buffer, but how do I read this value back to C# string?

The Marshal methods is giving me garbage

  • 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-01T14:45:27+00:00Added an answer on June 1, 2026 at 2:45 pm

    There’s not enough information in the question to be 100% sure but my guess is that you need this:

    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern int SomeDLLMethod(ref IntPtr data, ref int count);
    .....
    IntPtr data;
    int count;
    int retval = SomeDLLMethod(ref data, ref count);
    string str = Marshal.PtrToStringAnsi(data, count);
    

    Ideally when asking a question like this you should include the full documentation of the native function. I say this because a char** can mean many different things.

    My assumption is that the char** here is a pointer to a null-terminated C string allocated by the DLL. Your code assumes that the caller allocates the buffer but if that were so then I would expect to see char* rather than char**.

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

Sidebar

Related Questions

I am trying to call a DIV .click function, but it doesn't work. The
I trying to write a simple function to call unmanaged code from managed code.
I am trying to call my function named isUrgencyTypeValid from my javascript code but
When trying to call this function in my code i get the error in
I'm trying to call a function inside another function in python, but can't find
In unmanaged C++ I have a function which I'm trying to call from C#.
Hi im trying call this function from an element, but the element id is
I'm trying to call a function Size() from a custom COM DLL from PHP.
I'm trying to use P/Invoke to call functions in an unmanaged C++ DLL from
I am trying to call a function defined in a DLL, documented here: http://xiph.org/vorbis/doc/vorbisfile/ov_fopen.html

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.