使用BeginInvoke调用非托管委托

时间:2011-11-27 16:45:56

标签: c# dll unmanaged

我是一个DLL上的非托管函数的委托(我使用GetProcAddress加载)。我可以毫不费力地打电话给这个代表。但是,当我使用BeginInvoke调用委托时,我得到以下异常:

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in '...'.
Additional Information: The runtime has encountered a fatal error. The address of the 
error was at 0x63fd8687, on thread 0xb4c. The error code is 0xc0000005. This error may
be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common 
sources of this bug include user marshaling errors for COM-interop or PInvoke, which
may corrupt the stack.

这是代码:

        private void OnConnectFinished(IAsyncResult a_oResult)
        {
            ConnectDelegate l_oDelegate = (ConnectDelegate)a_oResult.AsyncState;

            if (ConnectFinished != null)
            {
                ConnectFinished(l_oDelegate.EndInvoke(a_oResult));
            }
        }

        public bool Connect()
        {
            AsyncCallback l_oCallback = new AsyncCallback(OnConnectFinished);
            IAsyncResult l_oResult = DLLConnect.BeginInvoke(l_oCallback, DLLConnect);

                    //This Works!:
            //bool l_bResult = DLLConnect(m_oConnectFinishedDelegate);
            //return l_bResult;

            return true;
        }

关于为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

将代理作为userStateBeginInvoke中的第二个参数)传递似乎是可疑的。如果你通过null,它会起作用吗? (是的,我知道你的回调不起作用,但是这个问题可以用另一种方式处理。让我们检查报告的错误是否消失。)