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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:11:51+00:00 2026-05-26T08:11:51+00:00

I have a function made in C++ that calls a COM interface’s function Its

  • 0

I have a function made in C++ that calls a COM interface’s function
Its signature:

BOOL func(LPWSTR strIn, __out LPWSTR strOut)
{
  //initcom
  //do something
  // release pointers
}

In C#:

[DllImport("funcdll.dll")]
static extern bool func(String strIn, ref String strOut);

// use it

for(int i=0;i<10;i++)
{
   if(func(strin, strout))
   {
      //do something with strout
   }
}

I have tested my dll in a C++ console application, it works, but in C# it crashes with an unknown error.

  • 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-26T08:11:52+00:00Added an answer on May 26, 2026 at 8:11 am

    You’ve got three problems that I can see.

    1. The calling conventions don’t match. Your C++ code is cdecl and your C# code is stdcall.
    2. The C++ code uses wide strings, but the C# code marshals ANSI strings.
    3. The second parameter doesn’t match. Your C# code assumes that the C++ code returns a new pointer to a C string which the C# code then deallocates with the COM allocator. Your C++ code doesn’t do this.

    Now, dealing with these in more detail.

    Calling conventions

    This is pretty easy to fix. Simple change the C++ code to stdcall, or the C# code to cdecl. But don’t do both. I’d change the C# code:

    [DllImport("funcdll.dll"), CallingConvention=CallingConvention.Cdecl]
    

    Unicode/ANSI strings

    I presume you are wanting to use Unicode strings since you have explicitly selected them in the C++ code. But P/invoke defaults to marshalling ANSI strings. You can change this again in the DllImport like so:

    [DllImport("funcdll.dll"), CallingConvention=CallingConvention.Cdecl, 
        CharSet=CharSet.Unicode]
    

    Returning a string from C++ to C#

    Your current C++ function declaration is so:

    BOOL func(LPWSTR strIn, __out LPWSTR strOut)
    

    The __out decorator has no real effect, other than documenting that you want to modify the buffer pointed to by strOut and have those modifications returned to the caller.

    Your C# declaration is:

    static extern bool func(String strIn, ref String strOut);
    

    Now, ref String strOut simply does not match. A ref string parameter matches this in C++:

    BOOL func(LPWSTR strIn, LPWSTR *strOut)
    

    In other words the C# code is expecting you to return a new pointer. In fact it will then proceed to deallocate the buffer you returned in strOut by calling CoTaskMemFree. I’m confident that’s not what you want.

    Your original C++ code can only return a string to the C# code by modifying the buffer that was passed to it. That code would look like this:

    BOOL func(LPWSTR strIn, __out LPWSTR strOut)
    {
        ...
        wcscpy(strOut, L"the returned string");
        ...
    }
    

    If this is what you want then you should allocate a sufficient buffer in C# in a StringBuilder object.

    [DllImport("funcdll.dll"), CallingConvention=CallingConvention.Cdecl, 
        CharSet=CharSet.Unicode]
    static extern bool func(string strIn, StringBuilder strOut);
    ...
    StringBuilder strOutBuffer = new StringBuilder(128);
    bool res = func("input string", strOutBuffer);
    string strOut = StringBuilder.ToString();
    

    If you simply cannot decide in the C# code how big a buffer you need then your best bet is to use a BSTR to marshal strOut. See this answer for details.

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

Sidebar

Related Questions

Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that
I have a array.each function that calls a JSONP request for each item and
I have a page for carpools. I made some changes to the function that
I have a problem with a javascript set of functions that I made. This
I have function getCartItems in cart.js and I want to call that function in
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have a C# class that I've made ComVisible so that it can be
I have an ASP.NET page that needs to make calls to multiple web services
I have a site that calls AJAX. It works at my office, where I

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.