Returning a std::string from a C++ DLL to a c# program -> Invalid Address specified to RtlFreeHeap -
In a function in my C ++ DLL, I am returning the std :: string in my C # application. It looks great:
std :: string g_DllName = "MyDLL"; Extern "C" THUNDER_API const char * __stdcall GetDLL name () {return g_DllName.c_str (); } But when my C # code calls this function, I get this message in my output window:
RtlFreeHeap (00150000, 0012D8D8) The invalid address in the function declaration c # looks like this:
[DllImport ("MyDll", EntryPoint = "GetDLLName") ] [Return: MarshalAs (UnmanagedType .LPStr)] public static extern string GetDLLName (); The message that I am able to search online, sometimes this message appears when there is an inconsistency, between which the new (debug or release, etc.) version is deleted with use is being done. But I am not sure what is happening in my case. So I'm not sure what exactly is the reason for it. Maybe MashallAs what could be something with it?
Any thoughts?
Thank you!
I managed to find this issue in the way it was defined in C # Marshall AS ( UnmanagedType.LPStr) with the string return type to understand that whenever it is, it will try to free the string. But because the string comes from C + + DLL, and there is a possibility of a separate memory manager completely, it fails. And even if he is not a failure, I do not want to free him in any way.
[DllImport ("MyDll", EntryPoint = "GetDLLName",
)) Public Static Extern IntPtr GetDLLName ();
So that's because it only gives an indicator on string data. And then to convert it to a string, pass it on Marshall. PtrToStringAnsi ()
Return Marshall. PtrToStringAnsi (GetDLLName ()); And she is wrapped in another celebration for sanitation.
I found a solution for this page:
Comments
Post a Comment