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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:31:50+00:00 2026-05-14T19:31:50+00:00

How to marshal the type of Cstring in .NET Compact Framework(C#)? DLLname:Test_Cstring.dll(OS is WinCE

  • 0

How to marshal the type of “Cstring” in .NET Compact Framework(C#)?

DLLname:Test_Cstring.dll(OS is WinCE 5.0),source code:

extern "C" __declspec(dllexport) int GetStringLen(CString str)
{ 
   return str.GetLength();
}

I marshal that in .NET Compact Framework(C#),for example:

[DllImport("Test_Cstring.dll", EntryPoint = "GetStringLen", SetLastError = true)]
public extern static int GetStringLen(string s);

private void Test_Cstring()
{
   int len=-1;
   len=GetStringLen("abcd");
   MessageBox.Show("Length:"+len.ToString()); //result is -1,so PInvoke is unsuccessful!
}

The Method of “GetStringLen” in .NET CF is unsuccessful!
How to marshal this type of “Cstring”?
Any information about it would be very appreciated!

  • 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-14T19:31:51+00:00Added an answer on May 14, 2026 at 7:31 pm

    You can’t marshal CString as it’s not a native type – it’s a C++ class that wraps up a char array.

    You can marshal string to char[] as char[] is a native type. You need to have the parameters to the function you want to P/Invoke into as basic types like int, bool, char or struct, but not classes. Read more here:

    http://msdn.microsoft.com/en-us/library/aa446536.aspx

    In order to call functions that take CString as an argument you can do something like this:

    //Compile with /UNICODE
    extern "C" MFCINTEROP_API int GetStringLen(const TCHAR* str) {
      CString s(str);
      return s.GetLength();
      //Or call some other function taking CString as an argument
      //return CallOtherFunction(s);
    }
    
    [DllImport("YourDLL.dll", CharSet=CharSet.Unicode)]
    public extern static int GetStringLen(string param);        
    

    In the above P/Invoke function we pass in a System.String which can marshal to char*/wchar_t*. The unmanaged function then creates a instance of CString and works with that.

    By default System.String is marshalled to char*, so be careful with what kind of string the unmanaged version takes. This version uses TCHAR, which becomes wchar_t when compiled with /UNICODE. That’s why you need to specify CharSet=CharSet.Unicode in the DllImport attribute.

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

Sidebar

Related Questions

suppose a dll contains the following functions extern C __declspec(dllexport) void f(bool x) {
Is it possible to Marshal a generic return type as non-generic for COM interop?
When using Marshal.GetExceptionCode() how do you get the Exception Type and/or instance of the
I'm trying to marshal data between SDL and my C# .NET program. The first
First of all the Boolean type is said to have a default marshal type
I want to marshal/unmarshal 2 custom types. And the type 1 has a List.
I'm using (Excel.Application)Marshal.GetActiveObject(Excel.Application) It returns me a _ComObject type but I was wondering how
i have this code: lvItem.pszText = (IntPtr)(lpRemoteBuffer + Marshal.SizeOf(typeof(LV_ITEM))); it works fine on 4.0.
Type classType = typeof(SomeClass); bool equal = Marshal.GenerateGuidForType(classType) == classType.GUID; I haven't found a
I'm trying to marshal a sub-class as super-class with xsi:type information as the XML

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.