I am trying to use a Golang library that is compiled as a shared C library in my .NET application. It's compiled with the following command: go build --buildmode=c-shared -o main.dll
My Golang code looks like this:
func DoRequest(request *C.char) *C.char {
// ...
// b = []byte
return (*C.char)(unsafe.Pointer(&b[0]))
}
How can I get this string back into a usable form in the .NET world? I've tried this but I get a panic from Go:
[DllImport("C:\\Users\\GolandProjects\\awesomeProject\\main.dll", EntryPoint = "DoRequest", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
extern static string DoRequest(byte[] requestJsonString);
static void Main(string[] args)
{
var result = DoRequest(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Request
{
// ...
})));
Console.ReadLine();
}