error calling function ,[A call to PInvoke function unbalanced the stack]

前端 未结 3 1412
傲寒
傲寒 2021-01-18 10:48

i have following code , once i run my application i get this error

anyone know how i fix this error?

ERROR:

A call to PInvoke function \'testcamera!

相关标签:
3条回答
  • 2021-01-18 11:18

    When doing a platform invoke (P/Invoke), you have to tell the CLR what the parameters are (which determines how they get marshalled) as well as what the calling convention of the target native method is so that the runtime knows how to generate code to properly push arguments and cleanup the stack after the call. If the signatures do not match, you end up with runtime errors similar to what you're seeing.

    The error message explains the issue well:

    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

    Compare the P/Invoke signature for EDSDK.EdsCreateEvfImageRef against the actual native method signature that implements this.

    You can change the calling convention of the P/Invoke by specifying the CallingConvention property on the DllImport attribute. More than likely, the calling convention for EDSDK.EdsCreateEvfImageRef should match the calling convention of your other P/Invokes.

    0 讨论(0)
  • 2021-01-18 11:24

    Please use Cdecl calling convention for that function. Don't ask me why, it just works.

    [DllImport("EDSDK.dll", CallingConvention=CallingConvention.Cdecl)]
    public extern static uint EdsCreateEvfImageRef(IntPtr inStreamRef, out IntPtr outEvfImageRef);
    
    [DllImport("EDSDK.dll",CallingConvention=CallingConvention.Cdecl)]
    public extern static uint EdsDownloadEvfImage(IntPtr inCameraRef, IntPtr outEvfImageRef);   
    
    0 讨论(0)
  • 2021-01-18 11:28

    I had the same issue as the poster, turned out I needed to change my project using the EDSDK library (v2.10) to use .NET 3.5 instead of .NET 4.0.

    0 讨论(0)
提交回复
热议问题