Solved

Coinitialize Error

Posted on 2006-10-20
4
1,112 Views
Last Modified: 2012-08-14
I have an application that I built using Delphi 2006 VCL and dtSearch COM api.  It has been working as expected until lately when I get the error "CoInitialize has not been called" when ever I use the SelectDirectory function then try to call the dtSearch COM procedures.  The SelectDirectory procedure had not precipitated the error before but now it does ever time.  I understand that the SelectDirectory function uses COM support in it internal call to the ShBrowseForFolder.  What I need is another way to have a dialog box (that does not use COM) that shows the existing directories available or a way to call CoInitiatize that works.  I called the Coinitialize in the startup of the application but I still got the error in the same situtation.
0
Comment
Question by:mcmahling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 17773334
can you paste the line of code where you call SelectDirectory? Because over here I can use it without the need for CoInitialize().

btw, my help says this on CoInitialize():

CoInitialize
Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.

New applications should call CoInitializeEx instead of CoInitialize.

Did you try using CoInitializeEx() ?
0
 

Author Comment

by:mcmahling
ID: 17773717
TForm1.Button1Click(Sender: TObject);
var
strChosenDirectory: String;
begin
 if SelectDirectory('Select a Directory', 'C:\' , strChosenDirectory) then
 begin
   blContinueLoop := True;
 end;
end;

I tried CoInitializeEX(nil,0) it also did not work.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 17779005
if you make a new empty project with only these lines, do you still need to call CoInitialize()?
0
 
LVL 9

Accepted Solution

by:
alkisg earned 500 total points
ID: 17788220
function SelectDirectory(const Caption: string; const Root: WideString;
  var Directory: string; Options: TSelectDirExtOpts = [sdNewUI]; Parent: TWinControl = nil):

If sdNewUI is set in the options (the default), SelectDirectory calls
CoInitializeEx(nil, COINIT_APARTMENTTHREADED)
and after the dialog is shown
CoUninitialize

The Delphi code seems OK, there must be something wrong with the dtSearch COM api.

A workaround would be to pass empty options as a parameter, so that sdNewUI is not set:
if SelectDirectory('Select a Directory', 'C:\' , strChosenDirectory, []) then
  ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question