0
votes

I am using a DLL built in C in my .NET application. This DLL has a function with an unsigned char* input parameter

short function_dll (unsigned char data)*

I've looking for the best calling conversion for this parameter in .NET: StringBuilder char[] byte[]

The call to the function works but randomly it fails with the error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt no matter the conversion used.

This is the last thing I have tried after a lot of combinations but ... the problem persists, randomly the error arises.

[DllImport(@"myDLL.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern short function_dll([MarshalAs(UnmanagedType.LPArray)] char[] data);

Any idea? Better conversion?

Thank you in advance, Regards.

1

1 Answers

0
votes

For one thing, the C# char is twice as large as the C++ one, so the monstrosity with the char[] is not going to work.

The question you should be asking yourself is this: is the string you're passing an input to the C++ function or an output?

If it's an input (ie you give the function a string and it doesn't modify it), the declaration should look like this:

[DllImport(@"myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern short function_dll([MarshalAs(UnmanagedType.LPStr)] string data);

// called like: function_dll("meep");

If the function you're passing is an output (ie you supply the function a buffer to write into), then the API you're working with is completely borked, because it doesn't take the length of the buffer as a parameter. Using the borked API then would use a StringBuilder with a preallocated buffer size of your choice and some hopes and prayers:

[DllImport(@"myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern short function_dll([MarshalAs(UnmanagedType.LPStr)] StringBuilder data);

// called like: 
//   var sb = new StringBuilder(100);
//   function_dll(sb);