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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:30:02+00:00 2026-05-23T03:30:02+00:00

I have a C# app that calls a C++ DLL. In C#, I have

  • 0

I have a C# app that calls a C++ DLL.

In C#, I have code as follows:

[DllImport(@"111.dll", CharSet = CharSet.Unicode)]
public extern static String Func1(String arg);
......
String arg = "test text";
String retstring = Func1(arg);

In CPP, I have the function defined as follows:

extern "C"
{
__declspec(dllexport) LPWSTR Func1(LPWSTR arg)
{
          ....
          LPWSTR ret1 = L"1?2?3?4?5";
          LPWSTR ret2 = SomeActualFunction(arg);
          retturn ret1; // return ret2;
     }
}

If I return ret1 in C++’s Func1(), all works fine. And in VS2008’s memory window, I can see correct Unicode binaries. In C++ the binaries of ret1 is

“31 00 3f 00 32 00 3f 00 33 00 3f 00 34 00 3f 00 35 00”

, and in C# the binaries of retstring is

“28 67 a3 f7 fe 07 00 00 0a 00 00 00 09 00 00 00 31 00 3f 00 32 00 3f 00 33 00 3f 00 34 00 3f 00 35 00”

. I think the C# binaries

“28 67 a3 f7 fe 07 00 00 0a 00 00 00 09 00 00 00”

are the header of System.String type.

What’s more, if I add the following line just before return in CPP code, I can get correct retstring in C# code too:

ret2 = L"1?2?3?4?5";

But when I return ret2 in the C++ DLL, the returned string ret in C# seems to be corrupted. The binaries in the C++ DLL are correct Unicode on my inspection. But the binaries of retstring in the C# code are

“28 67 a3 f7 fe 07 00 00 0a 00 00 00 09 00 00 00 dd dd dd dd dd dd dd dd dd dd dd dd ….”.

I can only notice ret2 is longer than ret1 – ret2 has some hundreds of WCHARs.

Any ideas? Thx in advance.

  • 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-23T03:30:03+00:00Added an answer on May 23, 2026 at 3:30 am

    I would always use a BSTR for this because it makes the responsibility of memory allocation/deallocation transparent.

    C++

    #include <comutil.h>
    BSTR GetSomeText()
    {
        return ::SysAllocString(L"Greetings from the native world!");
    }
    

    C#

    [DllImport(@"test.dll", CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.BStr)]
    private static extern string GetSomeText();
    

    You don’t say how your strings are being allocated, but as soon as you use a dynamically allocated string you need to tackle that issue. The great thing about BSTR is that it uses the shared COM allocator which enables the C# marshaller to deallocate the string with the same allocator as the C++ code that allocated it.

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

Sidebar

Related Questions

I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code
We have a VB6 app that calls out to a .NET DLL. Occasionally, after
I have an app that uses some code scripts to generate dll's at run-time
I have a c# windows app that calls a managed c++ dll, which, in
I have an iPhone app out there that calls home to my server every
I have a C++ app that uses Python to load some scripts. It calls
I have a Windows Forms App that loads a dll @ runtime and executes
I have disassembled a VB6 application that calls a function in a DLL library,
I have a .NET 3.5 winforms app that calls a method in a VB6
I have some C code that compiles to a DLL. From C#, I need

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.