Recent

Author Topic: Internet explorer automation question  (Read 6829 times)

facido

  • New Member
  • *
  • Posts: 15
Internet explorer automation question
« on: August 26, 2018, 06:03:06 am »
Hi,

I am automating internet explorer. I found some sample codes for delphi, and am having trouble in Lazarus.

The codes are as follows:
 
Code: Pascal  [Select][+][-]
  1. uses
  2.   mshtml_4_0_tlb, shdocvw_1_1_tlb, Comobj,
  3.  
  4. .....
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. var
  8.   Doc3 :  IHTMLDocument3;
  9.   url,onull:Olevariant;
  10.   FWebBrowser: IWebBrowser2;
  11. begin
  12.      FWebBrowser := CreateComObject(CLASS_InternetExplorer) as IWebBrowser2;
  13.      url:=Utf8decode('http://www.google.com');
  14.      FWebBrowser.Navigate(Url, onull, onull, onull, onull);
  15.      Sleep(250);
  16.      Doc3 := FWebBrowser.Document as IHTMLDocument3;     // error occurs here
  17.  
  18.      .....
  19.  
  20. end;
  21.  

Error occurs at line 16, and the error message is as follows:
"Cannot use RPC server"

I am attaching the compilable demo project so that you can reproduce the error.

https://github.com/facido/My-Demo/raw/master/MyDemo.zip


I checked that my rpc service is up and running, and tried it by disabling my firewall.
And the result is the same. What am I doing wrong?
« Last Edit: August 26, 2018, 06:13:13 am by facido »

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Internet explorer automation question
« Reply #1 on: August 26, 2018, 08:17:02 am »
What am I doing wrong?
You are not waiting for the page to load. It works with your sources:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Doc3 : IHTMLDocument3;
  4.   onull: OleVariant;
  5.   FWebBrowser: IWebBrowser2;
  6. begin
  7.   FWebBrowser := CreateComObject(CLASS_InternetExplorer) as IWebBrowser2;
  8.   try
  9.     FWebBrowser.Navigate(WideString('http://www.google.com'), onull, onull, onull, onull);
  10.     while FWebBrowser.ReadyState <> READYSTATE_INTERACTIVE do
  11.       Sleep(100);
  12.     Doc3 := FWebBrowser.Document as IHTMLDocument3;     // error occurs here
  13.     Caption := UTF8Encode(Doc3.uniqueID);
  14.   finally
  15.     FWebBrowser.Quit;
  16.   end;
  17. end;

facido

  • New Member
  • *
  • Posts: 15
Re: Internet explorer automation question
« Reply #2 on: August 26, 2018, 10:54:34 am »
I am getting the same error message with your codes.
Is it just my computer producing the error message?

Can anybody compile my attached demo codes, and see if the error occurs?

https://github.com/facido/My-Demo/raw/master/MyDemo.zip
« Last Edit: August 26, 2018, 10:56:20 am by facido »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Internet explorer automation question
« Reply #3 on: August 26, 2018, 11:31:05 am »
It doesn't hurt to be a little more cautious. Try  this for starters:
Code: Pascal  [Select][+][-]
  1. {$ASSERTIONS ON} // on top of the unit
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4.   Doc3 :  IHTMLDocument3 = nil; // initialize
  5.   url:widestring;  // use proper type
  6.   onull:Olevariant = VarNull; // initialize;
  7.   FWebBrowser: IWebBrowser2 = nil;; // initialize
  8. begin
  9.      FWebBrowser := CreateComObject(CLASS_InternetExplorer) as IWebBrowser2;
  10.      try
  11.        Assert(FWebBrowser <> nil,'webrowser not initialized');
  12.        url:='http://www.google.com';
  13.        FWebBrowser.Navigate(Url, onull, onull, onull, onull);
  14.        Sleep(250);
  15.        Doc3 := FWebBrowser.Document as IHTMLDocument3;     // error occurs here
  16.        Assert(Doc3 ><> nil,'Doc3 not initialized');
  17.      except
  18.        On E:EAssertionFailed do  // which of the two?
  19.             ShowMessage(e.message)
  20.        else
  21.           raise; // it is something else;
  22.      end;  
  23. end;

Not tested.

Also note that the RPC server  service needs to be running! It can be turned off.
On my computer I need to start it manually, because I configured the service like that.
Many sysadmins do the same.

Also note maybe that there needs to be a call to coInitialize(nil) and coUnInitialize somewhere in your program and in the context of the main thread.
But usually one of the COM related units do that in initialization/finalization sections.

« Last Edit: August 26, 2018, 11:52:12 am by Thaddy »
Specialize a type, not a var.

facido

  • New Member
  • *
  • Posts: 15
Re: Internet explorer automation question
« Reply #4 on: August 26, 2018, 11:56:18 am »
Your codes produce the same error message:
"cannot use rpc server"

I just want somebody to compile my attached codes, and see if the same error occurs.
It could be my computer producing the error message.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Internet explorer automation question
« Reply #5 on: August 26, 2018, 12:36:52 pm »
Listen, start your rpc services. That is windows system configuration! not FPC or Lazarus.
If you used my actual code or the code from ASerge you would know that.
In my code set a break-point on raise...
« Last Edit: August 26, 2018, 12:56:05 pm by Thaddy »
Specialize a type, not a var.

facido

  • New Member
  • *
  • Posts: 15
Re: Internet explorer automation question
« Reply #6 on: August 26, 2018, 01:29:27 pm »
As I described in my previous posting, my rpc service is up and running.
Your code produces same error message:
"cannot use rpc server"

No other messages were received, and the program exited.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Internet explorer automation question
« Reply #7 on: August 26, 2018, 02:07:31 pm »
can not reproduce....
Specialize a type, not a var.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Internet explorer automation question
« Reply #8 on: August 26, 2018, 03:05:04 pm »
As I described in my previous posting, my rpc service is up and running.
Your code produces same error message:
"cannot use rpc server"

No other messages were received, and the program exited.

What versions of Windows, IE, Lazarus and FPC are you using here?
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

facido

  • New Member
  • *
  • Posts: 15
Re: Internet explorer automation question
« Reply #9 on: August 27, 2018, 06:24:55 am »
What versions of Windows, IE, Lazarus and FPC are you using here?

I am running the codes on Winodws Vista(SP1), IE version 7, and Lazarus 1.8.4.
The versions of typed library are as follows:
mshtml_4_0_tlb, shdocvw_1_1_tlb
« Last Edit: August 27, 2018, 06:34:47 am by facido »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Internet explorer automation question
« Reply #10 on: August 27, 2018, 07:15:04 am »
hello,
try lazbrowserdemo on your computer.
OK for me on windows 10 I.E 11 lazarus 1.8.2

EDIT :  You can also try to reinstall Internet Explorer on your computer.

Friendly, J.P
« Last Edit: August 27, 2018, 08:32:04 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Internet explorer automation question
« Reply #11 on: August 27, 2018, 01:40:45 pm »
It doesn't hurt to be a little more cautious. Try  this for starters:
Code: Pascal  [Select][+][-]
  1. {$ASSERTIONS ON} // on top of the unit
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4.   Doc3 :  IHTMLDocument3 = nil; // initialize
  5.   url:widestring;  // use proper type
  6.   onull:Olevariant = VarNull; // initialize;
  7.   FWebBrowser: IWebBrowser2 = nil;; // initialize
  8. begin
  9.      FWebBrowser := CreateComObject(CLASS_InternetExplorer) as IWebBrowser2;
  10.      try
  11.        Assert(FWebBrowser <> nil,'webrowser not initialized');
  12.        url:='http://www.google.com';
  13.        FWebBrowser.Navigate(Url, onull, onull, onull, onull);
  14.        Sleep(250);
  15.        Doc3 := FWebBrowser.Document as IHTMLDocument3;     // error occurs here
  16.        Assert(Doc3 ><> nil,'Doc3 not initialized');
  17.      except
  18.        On E:EAssertionFailed do  // which of the two?
  19.             ShowMessage(e.message)
  20.        else
  21.           raise; // it is something else;
  22.      end;  
  23. end;

The Assert() for Doc3 is not necessary (aside from the operator being bogus :P ), cause as already ensures that the left hand side is not Nil (it wouldn't be able to query the interface otherwise).

 

TinyPortal © 2005-2018