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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:53:31+00:00 2026-05-30T08:53:31+00:00

I have a C++ project in which I have to return some variables from

  • 0

I have a C++ project in which I have to return some variables from C++ to C#.

These char variables are in the main program:

char test1[MAX_Q_LEN], test2[MAX_Q_LEN], test3[MAX_Q_LEN];

After I finish doing something with these variables in my C program, I have to return the values of these variables in a C# program.

ReturnChar.h

extern "C" RETURNCHAR_API TCHAR* __cdecl testString();

ReturnChar.cpp

extern "C" RETURNCHAR_API TCHAR* testString()
{
return ;
}

TestImport C#

static class TestImport
{
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr testString(); 
}


public partial class MainWindow : Window
{
public MainWindow()
{
try
{
InitializeComponent();
textBox1.Text = ReturnSomething()
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static string ReturnSomething()
{
IntPtr t = TestImport.testString();
String result = Marshal.PtrToStringAuto(t);
}

I tried with the above approach but I am not able to find out how to return the above char values. Also, this should not be an independent function because the values shoud be fetched only after executing the main which will give the right values in these variables.

Any suggestions?

  • 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-30T08:53:33+00:00Added an answer on May 30, 2026 at 8:53 am

    I will suggest a solution which would require you to change function signature to this:

    extern "C" int __cdecl testString(char *output, int outputSize);
    

    That is, pass an allocated buffer as the first argument to the function which will hold the output, and pass the size of the buffer as the second argument.

    Note that I mention the return type of the function as int. It is because you could return the output actual size from the function, and the caller can interpret this value to confirm that outputSize value was large enough to hold the output string. For example, you could implement testString() as:

     int testString(char *output, int outputSize)
     {
        std::string const & s = getString();
        if ( s.size() <= outputSize )
        {
            std::strncpy(output, s.c_str(), s.size());
            return s.size(); //return the actual size of output
        }
        else //means s.size() > outputSize, i.e outputSize is smaller than required!
        {
            std::strncpy(output, s.c_str(), outputSize);
            return s.size(); //return what is required (the actual size of output)!
        }
     }
    

    Then in C# code, do this:

    [DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int testString(out StringBuilder output, int outputSize); 
    

    And call it as:

    private static string ReturnSomething()
    {
       int bufferSize = 100;
       StringBuilder buffer= new StringBuilder(bufferSize);
       int outputSize = TestImport.testString(buffer, bufferSize);
       if ( outputSize < bufferSize )  //output bufferSize was sufficient
       {
            return buffer.ToString();
       }
       else //output bufferSize was insufficient 
       {
            //retry!
            bufferSize = outputSize;
            buffer = new StringBuilder(bufferSize); //reallocate!
            outputSize = TestImport.testString(buffer, bufferSize); 
            if ( outputSize <= bufferSize )
                 return buffer.ToString();
            else
            {
                throw new Exception("PANIC");
            }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project which builds correctly from the command line. On one of
I have some project where I have a single producer thread which writes events
I have a project which has just assimilated several apps from other projects of
I have been working on a project which is about data copy from one
I have a project which is source controlled using Subversion and VisualSVN. Since the
I have a project which contains different components that everyone works on. We have
I have a project which it's domain contain following classes: Courier Customer Food Order
I have a project which exposes object model to use by different types of
I have a project which has a set of binary dependencies (assembly dlls for
We have a project which had to be radically descoped in order to ship

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.