Passing strings from c# application to c++ dll

Posted on 2007-07-31
Last Modified: 2012-08-14
Im trying to create a dll in c++ that i can reference from a c# application.  My problem is that i am new to c++ and am just starting the grasp the concepts behind all the different types of strings.  I have built a managed class in c++ and linked it fine in c#.  it worked for passing ints and doubles as parameters but i cant figure out how to pass a string as a parameter or likewise return a string.  
Any help is appreciated, thank you.
Question by:drezdrez
    LVL 96

    Expert Comment

    by:Bob Learned
    It all depends on how the strings are defined in C++, as to what type of string marshalling that you are going to need.

    LVL 86

    Accepted Solution

    by: pretty much targets this issue. You could do that like

    C++ DLL ("returnstring.dll"):  

    #include <windows.h>
    #include <tchar.h>

    int GetString(LPSTR buf, int nMaxCount)
          TCHAR* pszText = _T( "Hi from unmanaged code");
          int nLen = _tcslen(pszText);

          if (nLen < nMaxCount)

             return nLen;

          return 0;


    using System.Text; // for StringBuilder
    public class ReturnStringDll{
      public static extern int GetString(StringBuilder buf, int nMaxCount);

    StringBuilder cb = new StringBuilder(256);
    ReturnStringDll.GetString(sb, sb.Capacity);

    That works for just displaying a string on the C++ side also.

    Author Comment

    Thank you jkr,  
    I've got this working and its put me back int he right direction.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    This article will show, step by step, how to integrate R code into a R Sweave document
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now