重用ActiveX对象会引发“写入受保护的内存”错误

时间:2013-08-27 22:57:51

标签: c# activexobject

我有一个ActiveX对象,我试图关闭然后重新打开,但似乎有些东西挥之不去,导致错误Attempted to read or write protected memory. This is often an indication that other memory is corrupt.奇怪的是我有对象的示例代码,我调用所有相同的命令。我的代码不完全相同,因为示例代码只显示了一个测试用户界面,但我在两台并排计算机上跟踪了我的代码和运行时的示例代码。我很茫然。

我像这样关闭对象:

        result = result && wrapper.Logout();            

        //  Throw the wrapper in the trash
        IsSelection = false;
        Controls.Remove(wrapper);
        wrapper.Dispose();
        wrapper = null;

当我重新创建它时,它声称它已经初始化,但这也发生在测试代码中。当我尝试再次登录时发生错误:

                result = wrapper.Login(Username, Password, Settings.AuthenticationSource, "", "");
                if (!result)
                    GetLastErrorCode();

包装就是这样;它包装了实际的通信代码。登录看起来像这样:

return (bool)ocx.GetType().InvokeMember("Login", BindingFlags.InvokeMethod, null, ocx, new object[] { userName, password, authSource, token, mnemonic });

和Logout看起来像这样:

return (bool)ocx.GetType().InvokeMember("Logout", BindingFlags.InvokeMethod, null, ocx, null);

在我的代码中,在重新创建ActiveX对象之后,我可以看到在销毁之前添加的值。我知道我没有做任何事来完成破坏,但看不出它可能是什么。有什么明显的迹象表明我可能会失踪吗?如果其他代码有用,请告诉我什么,我会尝试发布。

请注意,我无权访问ActiveX对象的代码。

提前感谢你的帮助。

编辑:

错误文本如下:

------ Exception ------
Error:  Exception has been thrown by the target of an invocation.
Error:  Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
There was an error logging in to iSite.
Exception has been thrown by the target of an invocation.

   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
   at ...Login(String userName, String password, String authSource, String token, String mnemonic) in ...:line 375
   at ...Login() in ...:line 500
---- End Exception ----

编辑2:

以下是创建对象的代码:

    System.ComponentModel.ComponentResourceManager resources = 
        new System.ComponentModel.ComponentResourceManager(typeof(xForm));
    try
    {
        wrapper = new xWrapper(Settings.ProgramID, Settings.ActiveXClassID);
    }
    catch (Exception e)
    { ... }

    //  Initialize the wrapper with default settings.
    ((System.ComponentModel.ISupportInitialize)(this.wrapper)).BeginInit();
    ...
    ((System.ComponentModel.ISupportInitialize)(this.wrapper)).EndInit();

    if (wrapper.SupportsPluginMessagingInterface())
    {
        wrapper.PluginMessage -= new xWrapper.
            _MessagingEvents_EventPluginMessageEventHandler(OnMessageReceived);
        wrapper.PluginMessage += new xWrapper.
            _MessagingEvents_EventPluginMessageEventHandler(OnMessageReceived);
    }

    GetLastErrorCode();

包装器构造函数只是一个默认构造函数,但后来我调用了initialize,它使用以下代码:

return (bool)ocx.GetType().InvokeMember("Initialize", BindingFlags.InvokeMethod, null, ocx, null);

我添加了代码以取消订阅事件处理程序,但这没有任何区别。

0 个答案:

没有答案
相关问题