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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:23:35+00:00 2026-06-05T10:23:35+00:00

Assume this C function: void do_something(const char* str) It stores the string somewhere for

  • 0

Assume this C function:

void do_something(const char* str)

It stores the string somewhere for later reference.

Furthermore, I have this signature in C# to call this function:

[DllImport("NativeLib")]
static extern void do_something(string str);

Now, what do I need to do when passing a string to this method:

  • Do I need to pin the string (with GCHandle.Alloc()) (or is the marshaller creating a copy)?
  • If I do need to pin it, do I pass the “original” string (i.e. the string I passed to GCHandle.Alloc())? Or do I need to pass the return value of GCHandle.AddrOfPinnedObject()?
  • Is string the correct data type (for do_something) in this case? Or should I use IntPtr instead?
  • 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-05T10:23:36+00:00Added an answer on June 5, 2026 at 10:23 am

    Storing pointers through an interop boundary is a Really Bad Idea, do everything you can to modify the C code to make a copy of the string instead.

    Pinning the string as suggested in the upvoted answer isn’t going to work, the pinvoke marshaller must convert the string to char* and releases that converted string after making the native call, invalidating the pointer. You must marshal the string yourself. Declare the argument as IntPtr and use Marshal.StringToHGlobalAnsi() to create the pointer value.

    Or use Marshal.StringToCoTaskMemAnsi(), it allocates from the COM heap. Which is your real problem, there is no great scenario to release the string. The C code cannot release the string because it doesn’t know how it was allocated. Your C# code cannot release the string because it doesn’t know when the C code is done with the pointer. This is why copying the string is so important. You can live with the memory leak if you make this call only a few times.

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

Sidebar

Related Questions

For example, I have a function: function doSomething( a:String, b:Number, c:Array ):void { trace(
This is my function: string GaugeStr; void someFunction() { float pie = someFloat(); stringstream
assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return
Assume I have a class a: class a { public: void load_data( ); private:
I have a function similar to the following: public void Increment() { if (Count
I have an issue while working with NSTimer. Let's assume I have this architecture
I assume this is really easy, and I'm missing something obvious. I'm connecting to
I assume this should be fine bool prefMatch = false; // Is the frequency
I assume this just returns an int. Is there anything else going on I
Let's assume this class in C#: public class LimitedList<T> : List<T> { private int

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.