I tried many codes in internet for this but it says Access violation. Can somebody provide me the code snippets in C++ and C# for this?

Use the StringBuilder class to convert a zero terminated char array to a C# string.

Hello DDanbe,
Iam not clear.Can you explain me with an example?

I have the c++ code in a dll file. Function in C++ is given below.

extern "C" __declspec(dllexport) string getString()
 {
	 string test;
	 test = "Hello";
	 return(test);
}

In C#

public class NativeMethods
{
 [DllImport("C++dll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern string getString();

public static void Main()
    {
        
        Console.WriteLine("o/p{0}", getString());
        Console.ReadLine();

    }
}

Iam getting the error System.AccessViolationException:Attempted to read or write protected memory

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.