Pass C# string to C++ and pass C++ result (string, char*.. whatever) to C#

后端 未结 5 1621
轻奢々
轻奢々 2020-11-28 13:15

I tried different things but i\'m getting mad with Interop.

(here the word string is not referred to a variabile type but \"a collection of char\"): I have an unmana

5条回答
  •  野性不改
    2020-11-28 13:30

    The "string" return value is the problem. The P/Invoke marshaller is going to call CoTaskMemFree() on the pointer you return. That's not going to work well unless you used CoTaskMemAlloc() in your C/C++ code to allocate the string buffer. Which is a fairly unusual thing to do.

    The best solution is to allow the caller of your code to pass a pointer to a buffer and the buffer length to you as arguments. That way all memory allocation happens on one side. Scott showed you how to do this.

提交回复
热议问题