简体   繁体   中英

Calling an unmanaged delegate with BeginInvoke

I've a delegate to an unmanaged function on a DLL (which I loaded using GetProcAddress). I can call this delegate with no trouble. When I call the delegates with BeginInvoke, however, I get the following exception:

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.

This is the code:

        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;
        }

Any ideas on why is this happening?

Passing the delegate as userState (the second argument in BeginInvoke ) seems suspicious. Will it work if you pass null there? (Yes, I know that your callback won't work, but this issue can be handled another way. Let's check if the reported error disappears.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM