I am trying to interact with a Dll that implements several functions, one of which takes a string with a terminating zero and int and returns a string with a zero termination. I tried interacting with this way:
[DllImport(dll_loc)] [return : MarshalAs(UnmanagedType.LPStr)] public static extern StringBuilder GetErrorMessage([MarshalAs(UnmanagedType.LPStr)] StringBuilder message, int error_code);
Then I try to call the method as follows:
StringBuilder message = new StringBuilder(1000); StringBuilder out2 = new StringBuilder(1000); out2 = GetErrorMessage(message, res0);
However, when I try to do this, an AccessViolationException message appears informing me that I am trying to access protected memory.
I managed to declare another method as such:
[DllImport(dll_loc)] public static extern int GetVersion([MarshalAs(UnmanagedType.LPStr)] StringBuilder version);
and calls it in the same way, but this method will not work for the current function call.
I also tried to return IntPtr, as the documentation technically states that the method returns a pointer to the first character of the string buffer, but to no avail.
Can anyone understand what could be wrong here? What may be different between the two methods that cause the DLL to try to access memory, it should not. Or, how would you recommend debugging this problem?
source share