簡體   English   中英

調用PInvoke函數xxx已使堆棧不平衡

[英]A call to PInvoke function xxx has unbalanced the stack

我在控制台應用程序中以C#代碼(.net 4.0)使用C DLL,並且遇到問題。 當我調用C方法時,它引發以下異常。

"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."

這是C代碼

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

這是我的C#代碼

    [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)
    }

下面是調用該方法的C#代碼

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

當代碼到達此處時,我可以看到該方法正在處理,因為它在控制台中打印,但是在處理之后,它引發了上述異常。

我猜這是由於C DLL試圖在輸出/引用參數中寫入值。 我沒有弄清楚問題出在哪里或代碼出了什么問題。

請注意,我已經在工具->選項->調試->常規中取消選中“抑制模塊加載時的JIT優化”選項

請盡快提供幫助。

感謝您花費寶貴的時間閱讀這篇文章。

謝謝基蘭

首先要檢查的是調用約定。 在大多數情況下,在DllImport屬性中指定的調用約定與本機DLL中的實際調用約定不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM