You can use System.Runtime.InteropServices.Marshal.PtrToStringAnsi if your char * is null terminated (i.e. it has a 0 as the last character).
Have a look at this sample code :
On a different note, a few points on styleCode:byte [] myByte = new byte[] { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)'\0' }; string result = string.Empty; unsafe { fixed (byte* xx = &(myByte[0])) { result = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(new IntPtr(xx)); } } Console.WriteLine(result);
(1) I hope you're not 'new'ing the memory for the char * and then returning it. C# has no way of releasing this memory. Returning a pointer to a variable which is released inside your dll is fine.
i.e.
(2) Don't put the interface code in the UI. You should have a separate class to do this. It should have a method which calls your dll method and returns a .NET string.Code:// in dll // bad ! Don't do anything like this ! char * getNameBad { char *xx = new char[3]; xx[0] = 'h'; xx[1] = 'e'; xx[2] = '\0'; return xx; } // better std::string myName = "hello"; const char *getNameGood() { return myName.c_str(); }
Darwen.




Reply With Quote