C # get string / characters value from C ++ function that returns a char pointer

I have a dll written in C ++. The function of this DLL is similar to the following code:

C ++ Code:

     char _H *GetPalette() {

            -------Functions body

            -------Functions body

            return pPaletteString;

      }

Now I want to get a pallet string from this GetPalette () function in C # code.

How can I get a string from this function? I tried this in C # code. But could not get the correct result.

C # code:

    [DllImport("cl.dll", EntryPoint = "GetPalette@0", CallingConvention = CallingConvention.StdCall)]

    private static extern IntPtr libGetPalette();

    public IntPtr GetPalette()
    {
        return libGetPalette();
    }

Finally, I want to get a line like this

            IntPtr result;
            result = imgProcess.GetPallet();

            string pallet;
            pallet = Marshal.PtrToStringAnsi(result);
            MessageBox.Show(pallet);

This code does not work correctly. Can any plz body help me, How can I get a string value from my C ++ DLL function?

thank

Shahriar

+3
source share
2 answers

You can define your C ++ function in C # code with a return type.

[DllImport("cl.dll")]
private static extern string GetPalette();

And than just calling it C # code.

string palette = GetPalette();

DllImport CallingConvention CharSet

+1

#, __stdcall, __stdcall . , char* UTF-8.

+1

All Articles