DelphiFAQ Home Search:

OLE Error: CoInitialize has not been called

 

comments56 comments. Current rating: 5 stars (40 votes). Leave comments and/ or rate it.

In a project that needed to display HTML documents, I decided to use the TWebBrowser control. I had used this handy ActiveX control successfully in other projects before.

This application was an MDI application, written in Delphi 5. As a 'specialty' I had installed a beta version of Internet Explorer on my system. I am not sure which of this is responsible for it, but when I would call the function in my application to display the HTML document, the TWebBrowser element could not be instantiated.
Instead I would receive an error message:

'CoInitialize has not been called'

The surprising thing is that the webbrowser control shows fine in design mode! I checked and TWebBrowser was properly installed. The underlieing DLL was also registered properly. A call of

regsvr32 shdocvw.dll

did not help. Finally I manually called the CoInitialize() function. I had to add OLE2 to the list of used units. A good place to do this is the initialization part as the sample snippet below shows.
Thanks to Martin Vreeken for pointing out the necessary CoUninitialize() call.

Note:
In a multithreaded application, you have to put a call to CoInitialize() at the beginning of your thread's Execute method and a matching CoUnInitialize() at its end.

uses
  ActiveX, // <-- make sure to include this unit
           // older Delphi versions use: OLE2 instead           
  Windows; // and others


initialization
  CoInitialize(nil); // <-- manually call CoInitialize()

finalization
  CoUnInitialize; // <-- free memory

end.

Comments:

You are on page 1 of 4, other pages: [1] 2 3 4
2005-12-19, 09:05:14
anonymous from Canada  
rating
Good point!
2006-02-17, 14:42:50
anonymous from Turkey  
rating
Perfect
2006-02-28, 08:33:24
anonymous from Germany  
Super
2006-03-12, 16:25:17
anonymous from Belgium  
rating
Thanks for advertizing this problem and its solution. I was about to quit an undebugged program (a DLL actualy, involving a TwebBrowser), and planning to forget about it when I thought to have a look on the web ... and found what the problem was.
2006-03-13, 01:43:22
anonymous from Denmark  
rating
Super duper.. It fixed the current problem leading me onto the next DLL-related /%#/!'%#/!' error.
2006-04-11, 17:00:52
shinichi from Vietnam  
rating
Thanks you so much ! That resolved my bug ! Thanks again ! You re so Pro :D
2006-05-10, 16:17:23
anonymous from United States  
rating
Thank you! This also resolved a problem of a TADOConnection throwing random exceptions when created programmatically in a DLL (Delphi 7).
2006-05-18, 10:43:06
anonymous from France  
i had the same problem with MS Word automation....
I'll try your hint.
thanks a lot
2006-06-21, 13:22:01
anonymous from United States  
rating
Life saver, thanks a million.
2006-07-03, 16:23:15
Eliandro May from Brazil  
Valeu
2006-07-12, 13:16:17
anonymous from Romania  
Unfrotunately it didn't solve the problem of TSimpleRSS and TIdHTTP being called from a separate thread. I don't know WTF is happending. :( I added CoInitialize all over the place, and the error still appears.
2006-08-03, 10:17:18
[hidden] from Germany  
rating
I used this in my new procedure:
procedure CreateShortcut(const ShortCutName: string; OnDesktop: boolean);

It worked perfectly in the pasts without initializing with CoInitialize but suddenly is dropped that error message so I used your solution:

procedure CreateShortcut(const ShortCutName: string; OnDesktop: boolean); { OnDesktop: 1=Desktop 0=StartMenu }
var
MyObject : IUnknown;
MySLink : IShellLink;
MyPFile : IPersistFile;
Directory : String;
WFileName : WideString;
MyReg : TRegIniFile;
begin
CoInitialize(nil); <-----

//OLD CODE ...
MyObject := CreateComObject(CLSID_ShellLink);
MySLink := MyObject as IShellLink;
MyPFile := MyObject as IPersistFile;
//OLD CODE ...

CoUninitialize; <-----
end;


Gabriel
newfedra at yahoo.com

2006-08-10, 03:54:50
anonymous from Poland  
rating
I also would like to thank for this tip -> it allowed me to save a lot of time which I would otherwise spend on fighting with thread-createad ADOConnection :)
2006-08-26, 06:32:07
anonymous from United States  
Worked great for me when trying to auotmate Excel.
2006-09-12, 18:16:48
anonymous from Switzerland  
rating
Great help!
That fixed the winshell.pas unit I found elsewhere, enabling me to read the content of a desktop shortcut.
Thank you so much! Manfred
You are on page 1 of 4, other pages: [1] 2 3 4

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: