Hello,
I am facing some issues in passing arguments from managed code to unmanaged code. I am having a C++ dll and i want to call few functions of it from a C# project (VS2010). C++ dll is not a native assembly or COM component. So i am using DllImport - P\Invoke.
C++ function details
typedef unsigned long UINT32;
typedef UNIT32 ABC_RESULT;
typedef UINT32 ABC_HOBJECT;
typedef ABC_HOBJECT ABC_HCONTEXT;
ABC_RESULT Abc_Context_Create(
ABC_HCONTEXT* phContext
);
phContext - Receives the handle to the created context object.
C# signature -
public class Abc1api
{
[DllImport("Abc1.dll")]
public static extern UInt32 Abc_Context_Create(UIntPtr phContext);
}
And i am calling it as below -
UInt32 result = 0;
UIntPtr hContext = new UIntPtr(sizeof(UInt32));
result = Abc1api.Abc_Context_Create(hContext);
I am able to build the project but while running i am getting below error -
A call to PInvoke function 'ABCTool!ABCTool.Abc1api::Abc_Context_Create' 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 tried IntPtr hContext instead of UIntPtr but result is same.
So I modified C# signature as mentioned below -
public class Abc1api
{
[DllImport("Abc1.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern UInt32 Abc_Context_Create([MarshalAs(UnmanagedType.U4), Out()] IntPtr phContext);
}
And I am calling it as below -
UInt32 result = 0;
IntPtr hContext = new IntPtr(sizeof(UInt32));
result = Abc1api.Abc_Context_Create(hContext);
When i run it, the error is - Cannot marshal 'parameter #1': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt).
I tried UIntPtr but same result. Also tried allocating IntPtr hContext using Marshal i.e.
IntPtr hContext = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt32))); but getting same error as above.
I could not find out the reason of this error. Can any one help me in defining C# signature and how can I pass the arguments so that I can get the required hContext from unmanaged code and use it?
Thank you,
Satish.