Looking for help from experience people who have dealt with C++ and C#
I am now writing a C# project which requires calling an existing C++ function in the internal library, and encountering some difficulties.
C++ Code Function Header (Cannot be changed)
typedef int (*PTR_func) 
(const char*             param1,      // input
 const char*             param2,      // input
 VirtualInternalString&  po_output);  // Third param is actually the output
C++ Code Class for the internal string object (Cannot be changed)
namespace InternalLib
{
    class VirtualInternalString
    {
        public :
            virtual ~VirtualInternalString(){};
            virtual void _cdecl SetString(const char *) =0;
            virtual const char * _cdecl c_str() const =0;
    };
    class SafeInternalString :public VirtualInternalString
    {
        public :
            SafeInternalString():TheString(""){};
            virtual ~SafeInternalString(){};
            virtual void _cdecl SetString(const char *  chararray) { TheString = chararray;};
            virtual const char * _cdecl c_str() const { return TheString.c_str() ;} ;           // This is the std::string
        protected:
            std::string TheString;
    };
    class SafePtrString :public VirtualInternalString
    {
        public :
            SafePtrString():PtrString(0){};
            SafePtrString(std::string &str):PtrString(&str){};
            virtual ~SafePtrString(){}; 
            virtual void _cdecl SetString(const char *  chararray) { (* PtrString) = chararray;};
            virtual const char * _cdecl c_str() const { return PtrString->c_str() ;} ;
        protected :
            std::string * PtrString;
    };
}
Now I have to utilize this "func" in my C# code. I can call and use the C++ function properly, but problem occurs during output return (the third param). It should be problem related return type.
C# (Can be changed)
[DllImport("xxxxlib", CallingConvention = CallingConvention.Cdecl)]
private extern static int func(string param1, 
                               string param2, 
                               [MarshalAs(UnmanagedType.LPStr)] StringBuilder res); // problem here
I know I have to create an appropriate wrapper to decopose the c++ object into C# string but I don't have previous experience about it. I have tried several solutions from the internet but not working.
Does any one know how I can implement this? Would you mind showing me some simple C# skeleton code?
 
     
     
     
    