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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:13:00+00:00 2026-05-15T11:13:00+00:00

The following .net to native C code does not work, any ideas extern C

  • 0

The following .net to native C code does not work, any ideas

extern "C" {
   TRADITIONALDLL_API int TestStrRef( __inout char* c)    {
   int rc = strlen(c);
   std::cout << "the input to TestStrRef is: >>" << c << "<<" ;
   c = "This is from the C code ";
   return rc;
   }
 }

 [DllImport("MyDll.dll", SetLastError = true)]
 static extern int TestStrRef([MarshalAs(UnmanagedType.LPStr)] ref string s);
 String abc = "InOut string";
 TestStrRef(ref abc);

At this point Console.WriteLine(abc) should print “This is from the C code ” but doesn’t, Any ideas on what’s wrong ?

FYI – i have another test function not using ref type string, it works just fine

  • 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-15T11:13:01+00:00Added an answer on May 15, 2026 at 11:13 am

    Your code wrong at C side also. __inout annotation just tell compiler you can change buffer to which “c” argument pointed. But pointer itself located in stack and does not return to caller if you modified “c” argument.
    Your declaration may look like:

    extern "C" {
       TRADITIONALDLL_API int TestStrRef( __inout char** c)    {
       int rc = strlen(*c);
       std::cout << "the input to TestStrRef is: >>" << *c << "<<" ;
       *c = "This is from the C code ";
       return rc;
       }
     }
    

    And C# side:

    [DllImport("MyDll.dll", SetLastError = true)]
    static extern int TestStrRef(ref IntPtr c);
    
    {
        String abc = "InOut string";
        IntPtr ptrOrig = Marshal.StringToHGlobalAnsi(abc)        
        IntPtr ptr = ptrOrig; // Because IntPtr is structure, ptr contains copy of ptrOrig
        int len = TestStrRef(ref ptr);
        Marshal.FreeHGlobal(ptrOrig); // You need to free memory located to abc' native copy
        string newAbc = Marshal.PtrToStringAnsi(ptr); 
        // You cannot free memory pointed by ptr, because it pointed to literal string located in dll code.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the version you compiled with is the same as… May 15, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer I'd guess your MEDIA_URL settings (in settings.py) is to blame… May 15, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer Try stop-process -ProcessName alcore.* -Force From get-help stop-process: On Windows… May 15, 2026 at 1:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.