代码之家  ›  专栏  ›  技术社区  ›  mmr

PinvokeStack-如何修复或关闭它?

  •  67
  • mmr  · 技术社区  · 14 年前

    这个例外在2008年不会被解雇。我完全可以访问C++ DLL和调用应用程序。pinvoke似乎没有任何问题,但这个问题使得调试其他问题变得不可能;IDE总是停下来告诉我这些事情。

    例如,以下是C#签名:

        [DllImport("ImageOperations.dll")]
        static extern void FasterFunction(
            [MarshalAs(UnmanagedType.LPArray)]ushort[] inImage, //IntPtr inImage, 
            [MarshalAs(UnmanagedType.LPArray)]byte[] outImage, //IntPtr outImage, 
            int inTotalSize, int inWindow, int inLevel);
    

    下面是C++方面的内容:

    #ifdef OPERATIONS_EXPORTS
    #define OPERATIONS_API __declspec(dllexport)
    #else
    #define OPERATIONS_API __declspec(dllimport)
    #endif
    extern "C" {
    
    
    OPERATIONS_API void __cdecl FasterFunction(unsigned short* inArray, 
                                           unsigned char* outRemappedImage,
                                           int inTotalSize, 
                                           int inWindow, int inLevel);
    
    }
    

    vs2010和vs2008之间有什么不同会导致抛出这些异常?我应该向DllImport指令添加一组不同的参数吗?

    5 回复  |  直到 11 年前
        1
  •  149
  •   sk8forether    7 年前

    在您的情况下,呼叫约定不正确。 DllImport CallingConvention.WinApi ,与 CallingConvention.StdCall 用于x86桌面代码。应该是的 CallingConvention.Cdecl

    这可以通过编辑行来完成 [DllImport("ImageOperations.dll")]

    [DllImport("ImageOperations.dll", CallingConvention = CallingConvention.Cdecl)]
    

    有关详细信息,请参阅 this MSDN reference

        2
  •  45
  •   Code Lღver Ionut Rusen    12 年前

    要关闭它:

    1. CTRL+ALT+E组合键
    2. 在“Managed Debugging Assistants”下,取消选中Pinvokes。
        3
  •  8
  •   Vijay Kumbhoje    10 年前

    更好的解决这个问题其实并不难,这里我提的一些方法,可能和我上面提到的一些朋友一样。我正在与PCSC的智能卡应用程序,我花了大约一个星期的时间,感到愤怒做了很多改变,终于得到了解决方案。

    http://www.red-gate.com/products/dotnet-development/pinvoke/

    enter image description here

    新窗口如下所示 enter image description here

    输入dll的名称,然后单击“搜索”您可以在“搜索结果”窗口中看到该dll的所有函数,单击该函数您将获得该特定函数的签名。

    使用该签名,您需要根据该签名(主要是数据类型)修改程序。

    这解决了我的问题,你可能有不同的问题,如callingConvention或其他属性需要指定导入dll。

    祝你身体健康!

        4
  •  3
  •   Code Lღver Ionut Rusen    12 年前

    我在使用VS2010时也遇到了这个问题。 它是什么: 当调用外部DLL时,变量指针(如字符串)现在变成64位,因为所有可靠和受信任的DLL都使用32位指针。

    别以为你的DLL有什么问题,其实没有。

    更改VS设置以生成如下X86代码(C的快速版本)

    1. 转到“工具”->选项。
    2. 在“选项”对话框的左下角,选中“显示所有设置”框。
    3. 在右侧的选项中,选中“显示高级生成配置”框
    4. 单击“确定”。
    5. 转到生成->配置管理器。。。
    6. 在“新平台”设置中,选择“x86”。
    7. 单击“关闭”。

        5
  •  0
  •   Hao    8 年前

    我试着用 CallingConvention ThisCall

    [DllImport("sqlncli11.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.ThisCall)]
                private static extern SafeFileHandle OpenSqlFilestream(
                            string FilestreamPath,
                            UInt32 DesiredAccess,
                            UInt32 OpenOptions,
                            byte[] FilestreamTransactionContext,
                            UInt32 FilestreamTransactionContextLength,
                            Int64 AllocationSize);
    

    更多信息请访问: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention(v=vs.110).aspx