简体   繁体   中英

A call to PInvoke function xxx has unbalanced the stack

I am using C DLL in C# code (.net 4.0) in a console application and facing issue. When I call the C method it raises the below exception.

"A call to PInvoke function 'ProcessFilesC' 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."

Here is the C code

    int ProcessFilesC(
        char **p_FilesOrDirs,
        ImageInfoC **p_vecImageInfo,
        int ***p_vecStackIndexes,
        RectC ***p_vecFaces
    );    

Here is my C# code

    [DllImport(@"..\ref\myCApp.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe private static extern UInt16 ProcessFilesC(
               String[] p_FilesOrDirs,
               ref ImageInfoC[] p_vecImageInfo,
               out Int32[] p_vecStackIndexes,
               out RectC[] p_vecFaces
            );


    public unsafe struct ImageInfoC
    {            
        public String resizedFilePath;      //char*   (in C)
        public Byte isUsable;               //unsigned char (in C)
        public UInt32 imageId;              //long int in (in C)
        public UInt16 stackIndex;           //int (in C)
        public Boolean isBestPhoto;         //unsigned char  (in C)
    }

Below is C# code to call the method

    unsafe
        {
             iResult = ProcessFilesC(
                              p_FilesOrDirs,          // Value getting passed to DLL
                              ref p_vecImageInfo,
                              out p_vecStackIndexes,
                              out p_vecFaces
                        );
             Console.WriteLine("ProcessFilesC Complete");
        }

When code reaches here, I can see that method is processing as it prints in console but after processing, it raises the mentioned exception.

I guess this is due to C DLL is trying to write values in output/ref parameters. I am not getting where is the issue or what's the wrong in my code.

Please note that I have already uncheck option "Suppress JIT optimization on module load" at Tools -> Options -> Debugging -> General

Please help as soon as poosible.

Thanks for spending to valuable time to read this post.

Thanks, Kiran

The first thing to check is the calling convention. In most cases the calling convention specified in your DllImport attribute differs from the actual calling convention in the native DLL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM