I am trying to call an existing C dll from a C# application, using System.Runtime.InteropServices, and am having difficulty matching the signatures between the PInvoke function and the Target function.
The target function is
__declspec(dllexport) DWORD GetSomeString(char* strOut);
And my PInvoke function is
[DllImport("Existing.dll")]
public static extern uint GetSomeString([MarshalAs(UnmanagedType.LPWStr)]
string strDisplay);
I make the function call
string tempStr = "My Output String";
uint retVal = GetSomeString(tempStr);
But I get the message
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem... ...A call to PInvoke function 'GetSomeString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
I have also tried implementing the PInvoke function as
[DllImport("Existing.dll")]
public static extern uint GetSomeString([MarshalAs(UnmanagedType.LPWStr)]
StringBuilder strDisplay);
But to no avail.
Does anyone out there have an idea what I could be doing wrong?
Please let me know if further info is required or my question is unclear.
Thanks in advance.