Passing String from C++ DLL to C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TedWin
    New Member
    • Nov 2008
    • 1

    Passing String from C++ DLL to C#

    Hi,

    I've got a C++ DLL and a C# GUI. The DLL gets a value and then needs to pass it back to the C# program in the form of a string.

    I know this is probably a glaringly simple thing to achieve but it's escaping me at the moment.

    I want something simple like this, a function that I can call from the C# part of my program that returns a string that I can then print to file/messagebox or whatever:

    extern "C" __declspec(dlle xport)
    string DataToReturn()
    {
    string test="TEST";
    return test;
    }

    I've tried a few combinations of char/string etc but seem to always come accross access violation exceptions.

    Anyone have any insight into this? :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read up on C++ /C# interoperabilit y. There are many topics on this in MSDN.

    The extern "C" only applies to making a C++ function callable from a C program. It does nothing to help with C#.

    A C++ string is not a C# String.

    Comment

    Working...