Why can't I return a char* string from C++ to C# in a Release build?

后端 未结 5 887
梦如初夏
梦如初夏 2021-01-31 10:07

I\'m attempting to call the following trivial C function from C#:

SIMPLEDLL_API const char* ReturnString()
{
    return \"Returning a static string!\";
}
         


        
5条回答
  •  囚心锁ツ
    2021-01-31 10:57

    In my P/Invoke experience, you usually have 2 parameters: 1. a pre-allocated buffer in, and 2, the length of the buffer. The Return value is the length of data returned (not to exceed the original length).

    Usually the DEBUG releases won't move memory around as much.

    BTW, You can pass in a pre-allocated StringBuilder, then set sb.Lenght = the return value of the C function, then you won't have a bunch of \0 nulls at the end of your string.

提交回复
热议问题