1

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

我像这样关闭对象:

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

和注销看起来像这样:

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();

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

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

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

4

0 回答 0