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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:15:38+00:00 2026-05-18T06:15:38+00:00

I’ve got an unmanaged c++ DLL that I need to call from a Windows

  • 0

I’ve got an unmanaged c++ DLL that I need to call from a Windows Mobile C# app.

I’ve got the C# wrapper and it works nicely in desktop. I can call the DLL functions from a C# desktop program and pass strings around with no problem.

However, when I compile the lib and the wrapper for the mobile platform, I get an error in the DllImport lines saying that the CharSet.ANSI is not recognized. The only options I’m allowed to write are CharSet.Auto and CharSet.Unicode.

The problem is that, regardless of this setting, the strings that are received in the c++ functions are wide char strings, and not plain char* strings that is what they expect.

We can use wcstombs() to translate all strings at the beginning of each c++ function, but I’d rather not modify the lib to such an extent…

Is there a way to fix the marshalling between C# and C that works with the .NET Compact Framework?

  • 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-18T06:15:38+00:00Added an answer on May 18, 2026 at 6:15 am

    No, there isn’t.

    Microsoft documentation specifies that:

    […] the .NET Compact Framework
    only supports Unicode, and consequently only includes the
    CharSet.Unicode (and CharSet.Auto
    which equals Unicode) value, and does
    not support any of the clauses of the
    Declare statement. This means that the
    ExactSpelling property is also not
    supported.

    As a result, if your DLL function
    expects an ANSI string, you’ll need
    to perform the conversion in the DLL,
    or convert the string to a byte array
    using the overloaded GetBytes method
    of the ASCIIEncoding class
    , before
    calling the function, since the .NET
    Compact Framework will always pass a
    pointer to the Unicode string. […]

    The solution is:

    Functions in the DLL

    int MARSHALMOBILEDLL_API testString(const char* value);
    const char* MARSHALMOBILEDLL_API testReturnString(const char* value);
    

    Wrapper

    [DllImport("marshalMobileDll.dll")]
    public static extern int testString(byte[] value);
    
    [DllImport("marshalMobileDll.dll")]
    public static extern System.IntPtr testReturnString(byte[] value);
    

    Calling Code

    string s1 = "1234567";
    int v = Wrapper.testString( Encoding.ASCII.GetBytes(s1));
    
    string s2 = "abcdef";
    IntPtr ps3 = Wrapper.testReturnString(Encoding.ASCII.GetBytes(s2));
    string s3 = IntPtrToString(ps3);
    
    
    private string IntPtrToString(IntPtr intPtr)
    {
      string retVal = "";
    
      byte b = 0;
      int i = 0;
      while ((b = Marshal.ReadByte(intPtr, i++)) != 0)
      {
        retVal += Convert.ToChar(b);
      }
      return retVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

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.