Solved

Coinitialize Error

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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