Solved

Coinitialize Error

Posted on 2006-10-20
4
1,107 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now