尝试调用c#回调时的AccessViolationException

时间:2013-06-20 12:01:47

标签: c++ pinvoke

我有这个简单的代码:

typedef void (__stdcall * OrdersCallback)(ordersTest*);

__declspec(dllexport) void InitializeCallbacks(long ordersCallbackAddress_) {
    OrdersCallback ordersCallbackFunction;
    std::cout << "current ordersCallbackFunction = " << ordersCallbackFunction << " address = " << &ordersCallbackFunction << std::endl;
    std::cout << "set ordersCallbackAddress_ = " << ordersCallbackAddress_ << std::endl;
    ordersCallbackFunction = (OrdersCallback) ordersCallbackAddress_;
    std::cout << "new ordersCallbackFunction = " << ordersCallbackFunction << " address = " << &ordersCallbackFunction << std::endl;

    ordersTest test;
    test.replID = 123;
    std::cout << "use ordersCallbackFunction = " << ordersCallbackFunction << " address = " << &ordersCallbackFunction << std::endl;
    ordersCallbackFunction(&test);
}

当回调调用时,会引发AccessViolationException:

current ordersCallbackFunction = 000007F92BC354E0 address = 0000009785D1EC68
set ordersCallbackAddress_ = -2043003524
new ordersCallbackFunction = FFFFFFFF863A3D7C address = 0000009785D1EC68
use ordersCallbackFunction = FFFFFFFF863A3D7C address = 0000009785D1EC68

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

我无法理解什么是错的,在另一个项目非常相似的代码中工作。谢谢!

更新,更多代码:

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void OrdersCallback(ref OrdersTest value);

    [DllImport("CGateNativeAdapter.dll"), SuppressUnmanagedCodeSecurity]
    public static extern void InitializeCallbacks(
        [MarshalAs(UnmanagedType.FunctionPtr)] OrdersCallback ordersSnapshotCallbackPointer);

        OrdersCallback ordersCallback =
            delegate(ref OrdersTest value)
            {
                Console.WriteLine("C# Orders call received = " +
                    " replID = " + value.replID);
            };
        InitializeCallbacks(ordersCallback);

1 个答案:

答案 0 :(得分:3)

你所展示的最明显的缺陷是你试图将64位指针作为32位值传递,这显然无法工作。 ordersCallbackAddress_参数声明为long,宽度为32位。但是你显然有64位进程。这几乎可以肯定地解释了您遇到的错误。

请记住,在Windows上的C ++中,long是32位宽。在C#中它是64位宽。在任何情况下,您都不应该使用整数类型来传递指针。如果你有一个指针,设计你的函数来操作指针。

您需要确保ordersCallbackAddress_被声明为指针大小。我不明白为什么它没有被声明为OrdersCallback

可能还有其他问题,但您没有显示太多代码。您没有在界面的托管端显示结构声明或任何内容。