Delphi和Internet Explorer,创建“全局"目录. IE [英] Delphi and Internet Explorer, create "global" IE

查看:144
本文介绍了Delphi和Internet Explorer,创建“全局"目录. IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些继承的代码可以打开IE,这是简短的版本:

procedure OpenIE(URL: OleVariant; FieldValues: string = '');
var ie : IWebBrowser2;
begin
  ie := CreateOleObject('InternetExplorer.Application') as IWebBrowser2;
  ie.Navigate2(URL, Flags, TargetFrameName, PostData, Headers);
  ShowWindow(ie.HWND, SW_SHOWMAXIMIZED);
  ie.Visible := true;
  ...
end;

由于CreateOleObject需要很长时间才能执行,因此我希望在首次运行时准备一个准备好的" IE.

例如在Main FormCreate中调用CreateOleObject,然后在第一次调用OpenIE时使用已创建的"IE"对象.

对于OpenIE的第2次,第3次调用-只是常规调用 即:= CreateOleObject

当我尝试对其进行编码时,出现一些线程和封送处理错误,我是该领域的新手.什么是做这件事的正确方法(一些小代码示例会很棒)?

谢谢.

解决方案

也许您正在其他线程中创建浏览器实例,然后从该线程中发出后续调用.以下平凡的代码完全按预期工作:

type
  TMainForm = class(TForm)
    ShowBrowser: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ShowBrowserClick(Sender: TObject);
  private
    FBrowser: Variant;
  end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FBrowser := CreateOleObject('InternetExplorer.Application');
end;

procedure TMainForm.ShowBrowserClick(Sender: TObject);
begin
  FBrowser.Navigate('http://stackoverflow.com');
  ShowWindow(FBrowser.HWND, SW_SHOWMAXIMIZED);
  FBrowser.Visible := True;
end;

我没有使用IWebBrowser2,因为我没有方便的导入单元.但这不会改变任何东西–您的问题将与早期/晚期绑定无关.

很显然,FormCreate在GUI线程中运行.而ShowBrowserClick是按钮OnClick事件处理程序.因此,它在GUI主线程中运行.

如果从GUI线程以外的其他线程调用OpenIE函数,则可以解释您的错误.如果您在创建该线程的线程之外的其他线程上访问该浏览器,则会收到带有消息该应用程序称为接口的应用程序,该消息被编组到另一个线程. >

最后,在提问题时要提一些建议.如果收到错误消息,请确保在您的问题中包括该确切的错误消息.这样做使我们更有可能提供良好的答案.

I have some inherited code for opening IE, this is short version :

procedure OpenIE(URL: OleVariant; FieldValues: string = '');
var ie : IWebBrowser2;
begin
  ie := CreateOleObject('InternetExplorer.Application') as IWebBrowser2;
  ie.Navigate2(URL, Flags, TargetFrameName, PostData, Headers);
  ShowWindow(ie.HWND, SW_SHOWMAXIMIZED);
  ie.Visible := true;
  ...
end;

Since CreateOleObject takes a long time to execute I would like to have one "prepared" IE for the first run.

For example in Main FormCreate to call CreateOleObject, then for 1st call of OpenIE to use "IE" object already created.

For 2nd, 3rd ... call of OpenIE - just usual call ie := CreateOleObject

When I try to code it, I get some threads and marshaling errors, I am newbie in this area. What would be proper way to do this (some small code example would be great) ?

Thanks in advance.

解决方案

Perhaps you are creating the browser instance in a different thread from which you then issue subsequent calls. The following trivial code works exactly as expected:

type
  TMainForm = class(TForm)
    ShowBrowser: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ShowBrowserClick(Sender: TObject);
  private
    FBrowser: Variant;
  end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FBrowser := CreateOleObject('InternetExplorer.Application');
end;

procedure TMainForm.ShowBrowserClick(Sender: TObject);
begin
  FBrowser.Navigate('http://stackoverflow.com');
  ShowWindow(FBrowser.HWND, SW_SHOWMAXIMIZED);
  FBrowser.Visible := True;
end;

I'm not using IWebBrowser2 because I don't have the import unit handy. But that won't change anything – your problems will not be related to early/late binding.

Obviously FormCreate runs in the GUI thread. And ShowBrowserClick is a button OnClick event handler. And so it runs in the main GUI thread.

If you are calling your OpenIE function from a thread other than the GUI thread, that would explain your errors. If you access the browser on a thread other than the one on which it was created, you will receive an EOleSysError with message The application called an interface that was marshalled for a different thread.

Finally, a word of advice when asking questions. If you receive an error message, make sure you include that exact error message in your question. Doing so makes it much more likely we can provide good answers.

这篇关于Delphi和Internet Explorer,创建“全局"目录. IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆