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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:58:55+00:00 2026-05-22T11:58:55+00:00

I have a problem, I tried to look at almost all the poster solutions,

  • 0

I have a problem, I tried to look at almost all the poster solutions, unsuccessful to find a suitable one.

The question is easy, Want to have a return string from unmanaged C code in my managed C#.
The c function is:

extern "C" __declspec(dllexport) int process_batch (char *&result);

and in C# I imported the DLL:

[DllImport("mydll.dll")]
public static extern IntPtr process_batch(StringBuilder result);

I run, but the return value in my StringBuilder is a 7-8 character string of non-sense! (I think the memory address)

I tried adding ref before the StringBuilder, this time I get the correct return value in StringBuilder but I get AccessViolationException :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

So I need your help to fix this.

Just one more thing, I use malloc in c for allocating the memory to the char* variable.

Thanks,

  • 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-22T11:58:56+00:00Added an answer on May 22, 2026 at 11:58 am

    If you really want to do that, pass the parameter as a ref IntPtr, then call Marshal.PtrToStringAnsi, or similar.

    [DllImport("mydll.dll")]
    public static extern IntPtr process_batch(ref IntPtr result);
    

    Note that, since you’re allocating the string with malloc in C, the .NET program will have to keep track of the returned pointer so that it can pass it back to you to be deallocated. Otherwise you’ll have a memory leak.

    If you were to pass a ref StringBuilder, you wouldn’t be able to deallocate the memory that was allocated by the C program.

    Also, as somebody commented in another post, you need to set the calling convention:

    [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem, I have tried different solutions to add a image
I tried searching and didn't find anything that fixed my problem. I have a
I have problem with return statment >.< I want to store all magazine names
I tried to look after a similar problem but couldn't find any here. So
I have tried troubleshooting this problem and just ended up with nothing, I hope
I have tried this approach/hack: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ The problem is this leaves a faint seam.
I am currently working on Problem 62 I have tried the following code to
I'm using diazo for theming, and I have a problem with all popups. I'm
Please take a look at this fiddle: http://jsfiddle.net/d3uc9/4/ I have a problem with this,
I have a problem with the MSDropDown plugin for jQuery. Please take a look

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.