我用C#仿照别人的写一个控件,其中用到了WebBrowser, 编译后放到HTML中,结果确出现以下错误
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。************** 异常文本 **************
System.ObjectDisposedException: 无法访问已释放的对象。
对象名:“WebBrowser”。
在 System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
在 System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
在 System.Windows.Forms.WebBrowser.PerformNavigateHelper(String urlString, Boolean newWindow, String targetFrameName, Byte[] postData, String headers)
在 System.Windows.Forms.WebBrowser.Navigate(String urlString)
在 CSharpActiveX.Hello.btnGo_Click(Object sender, EventArgs e) 位置 D:\VC++\CSharpActiveX\CSharpActiveX\Hello.cs:行号 110
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。例如: <configuration>
<system.windows.forms jitDebugging="true" />
</configuration>启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。HTML文件代码是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ActiveX测试</title>
</head>
<object id="csharpActiveX" classid="clsid:4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB"></object>
<input type="button" onclick="alert(csharpActiveX.SayHello());" value="显示当前时间" />
<body>
</body>
</html>C#的原代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security; namespace CSharpActiveX
{
[Guid("4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB")]
public partial class Hello : UserControl, IObjectSafety
{
public Hello()
{
InitializeComponent();
} #region IObjectSafety 成员
private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
private const int E_FAIL = unchecked((int)0x80004005);
private const int E_NOINTERFACE = unchecked((int)0x80004002);
private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;
public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
Rslt = S_OK;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
Rslt = S_OK;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
#endregion private void button1_Click(object sender, EventArgs e)
{
SetTime(DateTime.Now.ToLongTimeString());
} public void SetTime(string timeStr)
{
this.label2.Text = timeStr;
} public void SayHello()
{
SetTime("出错了");
} private void button2_Click(object sender, EventArgs e)
{
this.label2.BackColor = Color.Red;
} private void btnGo_Click(object sender, EventArgs e)
{
this.Browser.Navigate(txtAddr.Text);
} private void btnPreview_Click(object sender, EventArgs e)
{
IOleCommandTarget pCmdTarg = Browser.ActiveXInstance as IOleCommandTarget;
Guid CGID_MSHTML = GlobalConst.CGID_MSHTML; string vTemplatePath = txtTemplateAddr.Text;
Object[] o = new object[1] { vTemplatePath }; pCmdTarg.Exec(ref CGID_MSHTML,
GlobalConst.IDM_PRINTPREVIEW,
(uint)OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
o,
null);
} private void btnPrint_Click(object sender, EventArgs e)
{
IOleCommandTarget pCmdTarg = Browser.ActiveXInstance as IOleCommandTarget;
Guid CGID_MSHTML = GlobalConst.CGID_MSHTML; string vTemplatePath = txtTemplateAddr.Text;
Object[] o = new object[1] { vTemplatePath }; pCmdTarg.Exec(ref CGID_MSHTML,
GlobalConst.IDM_PRINT,
(uint)OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
o,
null);
}
}
}接口IObjectSafety.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; namespace CSharpActiveX
{
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
}接口IOleCommandTarget.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;namespace CSharpActiveX
{
[ComImport(), Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IOleCommandTarget
{
[PreserveSig()]
int QueryStatus([In, MarshalAs(UnmanagedType.Struct)] ref Guid
pguidCmdGroup, [MarshalAs(UnmanagedType.U4)] int cCmds,
[In, Out] IntPtr prgCmds, [In, Out] IntPtr pCmdText);
[PreserveSig()]
int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdExecOpt,
[In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn,
[In, Out, MarshalAs(UnmanagedType.LPArray)] object[] pvaOut); } public enum OLECMDEXECOPT
{
OLECMDEXECOPT_DODEFAULT,
OLECMDEXECOPT_PROMPTUSER,
OLECMDEXECOPT_DONTPROMPTUSER,
OLECMDEXECOPT_SHOWHELP
}
public static class GlobalConst
{
public const int MSOCMDEXECOPT_DONTPROMPTUSER = 2;
public const int IDM_PRINT = 0x1b;
public const int IDM_PRINTPREVIEW = 0x7d3;
public static readonly Guid CGID_MSHTML = new Guid("DE4BA900-59CA-11CF-9592-444553540000");
public static readonly Guid IID_OleCommandTarget = new Guid("B722BCCB-4E68-101B-A2BC-00AA00404770");
}}生成后
再部署安装Setup后打开HTML页,click me和button2均无问题,
在Document address中输入:http://www.sohu.com 然后点击Go就出现上面的错误.
也就是说程序在
private void btnGo_Click(object sender, EventArgs e)
{
this.Browser.Navigate(txtAddr.Text);
}
出错,不知怎样解决.
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。************** 异常文本 **************
System.ObjectDisposedException: 无法访问已释放的对象。
对象名:“WebBrowser”。
在 System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
在 System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
在 System.Windows.Forms.WebBrowser.PerformNavigateHelper(String urlString, Boolean newWindow, String targetFrameName, Byte[] postData, String headers)
在 System.Windows.Forms.WebBrowser.Navigate(String urlString)
在 CSharpActiveX.Hello.btnGo_Click(Object sender, EventArgs e) 位置 D:\VC++\CSharpActiveX\CSharpActiveX\Hello.cs:行号 110
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。例如: <configuration>
<system.windows.forms jitDebugging="true" />
</configuration>启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。HTML文件代码是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ActiveX测试</title>
</head>
<object id="csharpActiveX" classid="clsid:4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB"></object>
<input type="button" onclick="alert(csharpActiveX.SayHello());" value="显示当前时间" />
<body>
</body>
</html>C#的原代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security; namespace CSharpActiveX
{
[Guid("4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB")]
public partial class Hello : UserControl, IObjectSafety
{
public Hello()
{
InitializeComponent();
} #region IObjectSafety 成员
private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
private const int E_FAIL = unchecked((int)0x80004005);
private const int E_NOINTERFACE = unchecked((int)0x80004002);
private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;
public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
Rslt = S_OK;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
Rslt = S_OK;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
#endregion private void button1_Click(object sender, EventArgs e)
{
SetTime(DateTime.Now.ToLongTimeString());
} public void SetTime(string timeStr)
{
this.label2.Text = timeStr;
} public void SayHello()
{
SetTime("出错了");
} private void button2_Click(object sender, EventArgs e)
{
this.label2.BackColor = Color.Red;
} private void btnGo_Click(object sender, EventArgs e)
{
this.Browser.Navigate(txtAddr.Text);
} private void btnPreview_Click(object sender, EventArgs e)
{
IOleCommandTarget pCmdTarg = Browser.ActiveXInstance as IOleCommandTarget;
Guid CGID_MSHTML = GlobalConst.CGID_MSHTML; string vTemplatePath = txtTemplateAddr.Text;
Object[] o = new object[1] { vTemplatePath }; pCmdTarg.Exec(ref CGID_MSHTML,
GlobalConst.IDM_PRINTPREVIEW,
(uint)OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
o,
null);
} private void btnPrint_Click(object sender, EventArgs e)
{
IOleCommandTarget pCmdTarg = Browser.ActiveXInstance as IOleCommandTarget;
Guid CGID_MSHTML = GlobalConst.CGID_MSHTML; string vTemplatePath = txtTemplateAddr.Text;
Object[] o = new object[1] { vTemplatePath }; pCmdTarg.Exec(ref CGID_MSHTML,
GlobalConst.IDM_PRINT,
(uint)OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
o,
null);
}
}
}接口IObjectSafety.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; namespace CSharpActiveX
{
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
}接口IOleCommandTarget.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;namespace CSharpActiveX
{
[ComImport(), Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IOleCommandTarget
{
[PreserveSig()]
int QueryStatus([In, MarshalAs(UnmanagedType.Struct)] ref Guid
pguidCmdGroup, [MarshalAs(UnmanagedType.U4)] int cCmds,
[In, Out] IntPtr prgCmds, [In, Out] IntPtr pCmdText);
[PreserveSig()]
int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdExecOpt,
[In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn,
[In, Out, MarshalAs(UnmanagedType.LPArray)] object[] pvaOut); } public enum OLECMDEXECOPT
{
OLECMDEXECOPT_DODEFAULT,
OLECMDEXECOPT_PROMPTUSER,
OLECMDEXECOPT_DONTPROMPTUSER,
OLECMDEXECOPT_SHOWHELP
}
public static class GlobalConst
{
public const int MSOCMDEXECOPT_DONTPROMPTUSER = 2;
public const int IDM_PRINT = 0x1b;
public const int IDM_PRINTPREVIEW = 0x7d3;
public static readonly Guid CGID_MSHTML = new Guid("DE4BA900-59CA-11CF-9592-444553540000");
public static readonly Guid IID_OleCommandTarget = new Guid("B722BCCB-4E68-101B-A2BC-00AA00404770");
}}生成后
再部署安装Setup后打开HTML页,click me和button2均无问题,
在Document address中输入:http://www.sohu.com 然后点击Go就出现上面的错误.
也就是说程序在
private void btnGo_Click(object sender, EventArgs e)
{
this.Browser.Navigate(txtAddr.Text);
}
出错,不知怎样解决.
解决方案 »
- winform 两时间控件之间值的查询
- object类的 GetHashCode()作用是什么呀?(看书一直读不懂)
- 求《c#程序开发范例宝典》光盘源代码
- 双击dataGridView后的数据传递
- DG的一个怪问题
- 急急急!求C#入门经典 习题答案和源码
- CSC.EXE不见了
- 帮忙优化代码,查询数据是否在40个表中被使用
- C/S webBrowser 从自定义webBorwser中打开新页面
- 我写的程序, 几乎每个函数都有try ..catch ,被leader说,这样影响效率,尽量不要写。try..catch是不是很费时间呢?
- c#如何把字符串转化为窗体类型
- attribute的作用和具体使用方法
你在调用的代码外面加上using试试