I am new to the world of interacting with unmanaged libraries. I have an unmanaged C function that modifies a string by reference within the function. I'm having trouble passing a string from C# and getting it modified by the C function.
Here's the C function:
__declspec(dllexport) void __stdcall Test(char* name)
{
*name = "Bar";
}
This is the C# DLL import code:
[DllImport(@"C:/blah/mylibrary.dll")]
public extern static string Test(string name);
This is the code I'm using to call the function:
string s = "foo";
Test(s);
//I want s to be "Bar" after the above line
I have tried using "ref" and "out" on the string parameter, and tried Marshalling as an LPStr. Depending on what I try, I either get an error like
"The pointer passed in as a String must not be in the bottom 64K of the process's address space."
or
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I'm sure I'm just doing something stupid with my pointers. Can someone help me determine the appropriate C# code to get "s" to equal "bar"?
Thank you