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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:21:57+00:00 2026-05-13T07:21:57+00:00

I am calling a DLL using PInvoke. The DLL’s function returns a C string

  • 0

I am calling a DLL using PInvoke. The DLL’s function returns a C string in codepage 437.

Is there a way to have the .Net marshaling convert the string to unicode, or could someone suggest which parameters I should give to DllImport() and MarshalAs() and a conversion function to use in order to get an output in unicode?

For reference, this is the DllImport that I currently use:

[DllImport("name.dll", CharSet=CharSet.Unicode) ]
internal static extern int GetSweepParam(
    int param_num,
    [Out,MarshalAs(UnmanagedType.LPStr)]StringBuilder param_name,
    [Out,MarshalAs(UnmanagedType.LPStr)]StringBuilder param_units,
    double[] values,
    [MarshalAs(UnmanagedType.LPStr)]StringBuilder error_string
);
  • 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-13T07:21:57+00:00Added an answer on May 13, 2026 at 7:21 am

    ANSI string marshalling always uses the system default encoding. If you want to use some other encoding, then you can marshal those strings yourself.

    [DllImport("name.dll")]
    internal static extern int GetSweepParam(
        int param_num,
        [Out]byte[] param_name,
        [Out]byte[] param_units,
        double[] values,
        byte[] error_string
    );
    
    static void Test()
    {
        Encoding enc = Encoding.GetEncoding(437);
        byte[] param_name = new byte[1000], param_units = new byte[1000];
        GetSweepParam(123, param_name, param_units, new double[0], enc.GetBytes("input only"));
        string name = enc.GetString(param_name, 0, Array.IndexOf(param_name, (byte)0));
        string units = enc.GetString(param_units, 0, Array.IndexOf(param_units, (byte)0));
    }
    

    If the string is allocated by the unmanaged function, then you can marshal it from an IntPtr.

    static unsafe string PtrToStringAnsiWithEncoding(IntPtr p)
    {
        int l = 0;
        byte* bytes = (byte*)p.ToPointer();
        while(bytes[l] != 0) l++;
        char* chars = stackalloc char[l];
        int bytesUsed, charsUsed;
        bool completed;
        Encoding.GetEncoding(437).GetDecoder().Convert(bytes, l, chars, l, true, out bytesUsed, out charsUsed, out completed);
        return new string(chars, 0, charsUsed);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 303k
  • 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 I think you were on the right track when trying… May 13, 2026 at 8:31 pm
  • Editorial Team
    Editorial Team added an answer You need to use forward slash / and not backslash… May 13, 2026 at 8:31 pm
  • Editorial Team
    Editorial Team added an answer If you're inputting a word at a time then simply… May 13, 2026 at 8:31 pm

Related Questions

** MAJOR UPDATE ** I made a minor mistake but I'm still curious about
I am (successfully) calling the Windows FilterSendMessage function in c# using the following pinvoke
Introduction I am trying to use P/Invoke to register a struct of callbacks with
I am considering porting a small portion of the code in a C# project
I am working on a system that requires interaction with a native C API

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.