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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:10:51+00:00 2026-06-14T03:10:51+00:00

I have a dll written in C#, and exposed to COM. I am using

  • 0

I have a dll written in C#, and exposed to COM. I am using the dll in builder… I can instantiate the class, but am having problems with marshalling the return value from the C# method calls.

C#

public string GetValue([MarshalAs(UnmanagedType.LPWStr)] string key)
{
   return "value";
}

The translated function as it’s imported into builder:

virtual HRESULT STDMETHODCALLTYPE GetValue(LPWSTR key/*[in]*/, 
                                           BSTR* pRetVal/*[out,retval]*/) = 0;

I know very little about C++. The ‘key’ parameter gets passed in fine because I’m able to use the ‘MarshalAs’ attribute on the parameter, but I either don’t know how to declare it for the return value, or don’t know how to call the function on the C++ side (I’ve tried several things, just guessing).

UPDATE: Okay, I was just able to solve the issue by taking Anton’s example and trying modifications based on Hans’ comments. Antons answer works precisely as he shows, but because of the concerns expressed about the memory management issue, I ended up not applying the return attribute in C#, and the C++ code calls the function as follows:

BSTR result;
obj->GetValue(key, &result);
SysFreeString(key);
SysFreeString(result);

I wish I could give credit to both answers for helping me with this, they were both necessary to supplying me with the information I needed.

  • 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-06-14T03:10:52+00:00Added an answer on June 14, 2026 at 3:10 am

    You can apply a [return:] attribute but it is a Really Bad Idea. The problem with this function signature is that the callee must allocate the buffer for the string and the caller must release it. That requires both to use the same heap. This will not be the case when you force it to use LPWSTR, the CLR uses its own heap and you cannot get to it from your native code, you can’t get the required heap handle.

    Both pieces of code must use the same heap. And there is one especially for this purpose, the COM heap. The BSTR is a string type that uses that heap, the CLR automatically uses it as you could tell from the signature. To use it, simply access the pRetVal pointer after the call, it is a wchar_t* under the hood. And you must release it afterwards, call SysFreeString().

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

Sidebar

Related Questions

I have written a COM dll, and wish to register it using regsvr32 myComdll.dll
I have an application written in PHP that uses a COM dll written in
I have a DLL written in C#. In this DLL there is a class
We have a COM dll written in delphi in our bin folder that call
I have a COM DLL written in unmanged C++, and compiled with VS 2010.
I have a COM dll written in vb6. When I try to create a
I have written a class dll in Net that I have to expose to
We have COM dll which was written in C++ and has been used by
I have Windows Forms Application written on Delphi 7 and C++ .dll written using
I have a third-party (Win32) DLL, written in C, that exposes the following interface:

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.