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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:55:56+00:00 2026-05-26T16:55:56+00:00

I have an unmanaged DLL that gets called from .NET with pre-allocated buffers to

  • 0

I have an unmanaged DLL that gets called from .NET with pre-allocated buffers to get filled within the unmanaged DLL (according to Pass C# string to C++ and pass C++ result (string, char*.. whatever) to C#).

My unmanaged function has the following prototype:

myFunc(char* a_inBuf,  int a_InLen, 
       char* a_outBuf, int* a_pOutLen, 
       char* a_errBuf, int* a_pErrLen);

So, I declare the method in the managed code like this:

public static extern int myFunc(
  [In, MarshalAs(UnmanagedType.LPStr)] string inputXml, int inputLen,
  [MarshalAs(UnmanagedType.LPStr)] StringBuilder outputXml, ref int outputLen,
  [MarshalAs(UnmanagedType.LPStr)] StringBuilder errorXml, ref int errorLen);

Before calling myFunc, I create the two StringBuilders:

StringBuilder outputXml = new StringBuilder(100);
StringBuilder errorXml  = new StringBuilder(100);

After calling myFunc, I take the two StringBuilders and write them into an XML file (one for each StringBuilder) using

using (StreamWriter writer = new StreamWriter("OutputXmlFile.xml", false, Encoding.UTF8))
{
  writer.Write(outputXml.ToString());
  writer.Close();
}

The output shall be written in UTF8 since the Input is also UTF8. But unfortunately, the StringBuilder uses UTF16 encoding. The content of outputXml and errorXml gets filled in the unmanaged DLL also in UTF8 encoding. This behaviour should not be changed. When writing the files, special chars contained in the StringBuilders aren’t written correctly.

How do I tell the StringBuilder that the content is actually NOT UTF16 but UTF8?


Edit: the answer provided by Polynomial indicates to use xmlWriter to write the file. But actually, the writing is just used for debugging output. In normal application run, the content of outputXml and errorXml is directly used within the program. Therefore, any hints regarding the use of special XML handling classes is not useful.

The actual issue is to get the correct strings out of the StringBuilder (or convert them to be correct).

  • 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-26T16:55:56+00:00Added an answer on May 26, 2026 at 4:55 pm

    You cannot convince the pinvoke marshaller to convert from utf8. It will either assume utf-16 or the system default code page and always convert to utf-16.

    Not a problem, just do it yourself. Declare the arguments of type byte[] instead. Create the arrays before the call with the proper length, after the call use Encoding.UTF8.GetString() to convert.

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

Sidebar

Related Questions

I have a very simple DLL written in unmanaged C++ that I access from
So I have a function in unmanaged c++ that gets called when some text
I have a requirement to call a dll (unmanaged c) from a .NET web
I have a DLL that was written in C++ and called from a C#
I have an unmanaged class that I'm trying to dllexport from a managed DLL
I have an unmanaged dll that contains a function to read a data from
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
I have a simple application that loads an unmanaged dll and passes a few
I have a unmanaged DLL that exposes a function that takes a pointer to
I have an unmanaged DLL with a function that can run for a long

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.