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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:34:51+00:00 2026-05-23T19:34:51+00:00

I am trying to interface with a Dll that implements several functions, one of

  • 0

I am trying to interface with a Dll that implements several functions, one of which takes a null terminated string and an int, and returns a null terminated string. I have attempted to interface with the method like this:

[DllImport(dll_loc)]
[return : MarshalAs(UnmanagedType.LPStr)]
public static extern StringBuilder GetErrorMessage([MarshalAs(UnmanagedType.LPStr)]
                                                       StringBuilder message, 
                                                       int error_code);

I then attempt to call the method like this:

StringBuilder message = new StringBuilder(1000);
StringBuilder out2 = new StringBuilder(1000);
out2 = GetErrorMessage(message, res0);

However, when I attempt this, an AccessViolationException is thrown telling me I am attempting to access protected memory.

I was successful in declaring a different method as such:

[DllImport(dll_loc)]
public static extern int GetVersion([MarshalAs(UnmanagedType.LPStr)]
                                        StringBuilder version);

and calling it in the same manner, but this method won’t work for the current function call.

I also tried returning an IntPtr, since the documentation technically says that the method returns a pointer to the first character of the string buffer, but to no avail.

Does anyone have any insight into what could be going wrong here? What could be different between these two methods that is causing the dll to attempt to access memory it should not. Or, how would you recommend going about debugging this problem?

  • 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-23T19:34:53+00:00Added an answer on May 23, 2026 at 7:34 pm

    C functions that return a string are a memory management problem. A C string requires an array and the memory for that array needs to be released after the string is consumed. That makes such functions very hard to use in a C program, next to impossible to use with pinvoke. It is also a classic C bug, returning a pointer to a string on the stack.

    The pinvoke marshaller is going to try to release the returned string, as required to avoid a memory leak. It will use CoTaskMemFree(). That does not often come to a good end, it is rare that the C code actually used CoTaskMemAlloc to allocate the memory for the array. On XP, that will cause a silent memory leak. Vista and Win7 have much stricter heap managers, they’ll invoke a debug break if an unmanaged debugger is attached. And bomb the program with an AccessViolation next.

    You can avoid the automatic marshaling behavior by declaring the return type as IntPtr and marshal the string yourself. Typically with Marshal.PtrToStringAnsi(). But then you’re still faced with the task of releasing the memory. You can’t, you don’t have the handle of the CRT heap and you can’t call free().

    C functions that return strings should be declared with an argument that passes a string buffer. And an argument that says how large the buffer is. Like this:

     int GetErrorMessage(int errorCode, char* buffer, size_t bufferSize);
    

    Now it is simple, the caller can allocate the memory for the buffer and free it. And the C function simply copies the string into the buffer. The return value can be used to tell how many characters were copied, or indicated that a larger buffer is needed. Don’t skimp on the bufferSize argument, a buffer overflow is deadly and invokes the dreaded FatalExecutionEngineError exception, an exception that is as undebuggable as AV since the GC heap corruption doesn’t get detected until much later. You use a StringBuilder on the C# side, suitably initialized with a non-zero capacity. The value of bufferSize.

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

Sidebar

Related Questions

I have a C++ dll which implements several COM interfaces, that I'm trying to
I am trying to define a WCF contract that returns an interface, something like
I am trying to use several functions from kernal32.dll. However, when my application tries
I'm trying to interface with a cdc card reader which outputs to a virtual
I have the following interface which I'm trying to make COM-visible. When I try
I'm currently trying to build a C++ library (DLL file) that interfaces with a
I have a static S.lib that is used by my D.dll. I'm trying to
I'm trying to debug some code that uses COM, which I am a beginner
I am trying to write an interface between RSPEC (ruby flavoured BDD) and a
I am trying to create a user interface using XAML. However, the file is

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.