用BeginInvoke调用非托管委托



我有一个委托到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;
        }

你知道为什么会这样吗?

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

最新更新