本人在用c#开发IE的BHO插件,实现从数据库中读入URL,依次自动实现访问,但遇到个问题,当url代表的是一个“下载文件的url”时,IE会自动跳出下载对话框,请问在这种情况下我怎样实现持续自动化IE?对此我采用的两个方法均不奏效
1:通过定义SHDocVw.WebBrowser接口的FileDownload事件响应,当有文件下载时,自动实现文件保存到默认文件夹。但编程实现时发现FileDownload时间根本就没有触发/或者根本没有调用此事件的响应消息(消息框没有弹出,如下)。而DocumentComplete等事件均可正确触发和响应。
代码如下: //定义IObjectWithSite接口
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
] public interface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
} //接口功能实现
[ComVisible(true),
Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
ClassInterface(ClassInterfaceType.None)
]
public class BHO:IObjectWithSite
{
WebBrowser webBrowser;
public void OnWebBrowser_FileDownload(ref bool Cancel)
{
System.Windows.Forms.MessageBox.Show("这是一个下载文件"); //此处无任何响应
webBrowser.Quit();
}
#region BHO Internal Functions
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
[ComRegisterFunction]
public static void RegisterBHO(Type type){//注册此组件//}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type){//解除此组件//}
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.FileDownload+=new DWebBrowserEvents2_FileDownloadEventHandler(this.OnWebBrowser_FileDownload);//挂载事件处理
}
else
{
webBrowser.FileDownload -= new DWebBrowserEvents2_FileDownloadEventHandler(this.OnWebBrowser_FileDownload);
webBrowser = null;
}
return 0;
}
public int GetSite(ref Guid guid, out IntPtr ppvSite){//getsite实现}2:上面方法不通后,又想通过定制IE下载管理实现持续自动化,但在c#下使用IDownloadManager接口总是不成功,IDownloadManager是不是可以直接使用,还是需要从其他接口继承或者查询获得?问题代码如下:
//定义IDownloadManager接口
[ComVisible(true), ComImport]
[Guid("988934A4-064B-11D3-BB80-00104B35E7F9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IDownloadManager
{ [return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
void Download(
System.Runtime.InteropServices.ComTypes.IMoniker pmk,
System.Runtime.InteropServices.ComTypes.IBindCtx pbc,
[In, MarshalAs(UnmanagedType.U4)] UInt32 dwBindVerb,
[In] Int32 grfBINDF,
ref IntPtr pBindInfo,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszHeaders,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszRedir,
[In, MarshalAs(UnmanagedType.U4)] UInt32 uiCP);
} //实现IDownloadManager接口功能类
[
ComVisible(true),
Guid("9be66cc0-1dd1-11b2-8617-e3a3ed26e3b0"),
ClassInterface(ClassInterfaceType.None)
] public class DownloadManager : IDownloadManager
{
public void Download(IMoniker pmk,
IBindCtx pbc,
UInt32 dwBindVerb,
int grfBINDF,
ref IntPtr pBindInfo,
String pszHeaders,
String pszRedir,
UInt32 uiCP)
{
System.Windows.Forms.MessageBox.Show("Extremely great sign..."); //此处也无反应
} #region BHO Internal Functions
public static string BHOKEYNAME = "Software\\Microsoft\\Internet Explorer";
//修改HKEY_CurrentUser\Software\Microsoft\Internet Explorer下的DownloadUI的值
//
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(BHOKEYNAME, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
registryKey.SetValue("DownloadUI", "{9be66cc0-1dd1-11b2-8617-e3a3ed26e3b0}");
registryKey.Close();
} [ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
registryKey.SetValue("DownloadUI", "{802F530B-A8F6-4631-AE49-6BACAAC6373E}");
}
#endregion
}
1:通过定义SHDocVw.WebBrowser接口的FileDownload事件响应,当有文件下载时,自动实现文件保存到默认文件夹。但编程实现时发现FileDownload时间根本就没有触发/或者根本没有调用此事件的响应消息(消息框没有弹出,如下)。而DocumentComplete等事件均可正确触发和响应。
代码如下: //定义IObjectWithSite接口
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
] public interface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
} //接口功能实现
[ComVisible(true),
Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
ClassInterface(ClassInterfaceType.None)
]
public class BHO:IObjectWithSite
{
WebBrowser webBrowser;
public void OnWebBrowser_FileDownload(ref bool Cancel)
{
System.Windows.Forms.MessageBox.Show("这是一个下载文件"); //此处无任何响应
webBrowser.Quit();
}
#region BHO Internal Functions
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
[ComRegisterFunction]
public static void RegisterBHO(Type type){//注册此组件//}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type){//解除此组件//}
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.FileDownload+=new DWebBrowserEvents2_FileDownloadEventHandler(this.OnWebBrowser_FileDownload);//挂载事件处理
}
else
{
webBrowser.FileDownload -= new DWebBrowserEvents2_FileDownloadEventHandler(this.OnWebBrowser_FileDownload);
webBrowser = null;
}
return 0;
}
public int GetSite(ref Guid guid, out IntPtr ppvSite){//getsite实现}2:上面方法不通后,又想通过定制IE下载管理实现持续自动化,但在c#下使用IDownloadManager接口总是不成功,IDownloadManager是不是可以直接使用,还是需要从其他接口继承或者查询获得?问题代码如下:
//定义IDownloadManager接口
[ComVisible(true), ComImport]
[Guid("988934A4-064B-11D3-BB80-00104B35E7F9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IDownloadManager
{ [return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
void Download(
System.Runtime.InteropServices.ComTypes.IMoniker pmk,
System.Runtime.InteropServices.ComTypes.IBindCtx pbc,
[In, MarshalAs(UnmanagedType.U4)] UInt32 dwBindVerb,
[In] Int32 grfBINDF,
ref IntPtr pBindInfo,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszHeaders,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszRedir,
[In, MarshalAs(UnmanagedType.U4)] UInt32 uiCP);
} //实现IDownloadManager接口功能类
[
ComVisible(true),
Guid("9be66cc0-1dd1-11b2-8617-e3a3ed26e3b0"),
ClassInterface(ClassInterfaceType.None)
] public class DownloadManager : IDownloadManager
{
public void Download(IMoniker pmk,
IBindCtx pbc,
UInt32 dwBindVerb,
int grfBINDF,
ref IntPtr pBindInfo,
String pszHeaders,
String pszRedir,
UInt32 uiCP)
{
System.Windows.Forms.MessageBox.Show("Extremely great sign..."); //此处也无反应
} #region BHO Internal Functions
public static string BHOKEYNAME = "Software\\Microsoft\\Internet Explorer";
//修改HKEY_CurrentUser\Software\Microsoft\Internet Explorer下的DownloadUI的值
//
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(BHOKEYNAME, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
registryKey.SetValue("DownloadUI", "{9be66cc0-1dd1-11b2-8617-e3a3ed26e3b0}");
registryKey.Close();
} [ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
registryKey.SetValue("DownloadUI", "{802F530B-A8F6-4631-AE49-6BACAAC6373E}");
}
#endregion
}
这种情况下没有办法。你可以要求用户修改迅雷的选项,不监视下载。
我用c#开发的BHO,如果对应的url是一个下载url,我想停止此导航,然后继续下一个url的访问,因为在.net环境下Filedownload事件响应机制好像是无效的,那能提供一个解决办法吗?
IE6.1有个BUG,如果用户在提示确认时选择了保存,那么Filedownload事件不会被触发。