?
Solved

COM - works in main thread, but not in seperate thread?

Posted on 2005-04-28
3
Medium Priority
?
178 Views
Last Modified: 2010-04-05
Please look at the code below.  I would like to know why it behaves differently in when I call it from a seperate thread.  Is there some sort of special COM/OLE initialization that I must do to make it work in a seperate thread?
...
uses
ShellApi,ShlObj, ComObj, ActiveX;
..
Procedure DoSomething;
var
  PIDL: PItemIDList;
  Path: array[0..MAX_PATH] of Char;
  psl  : IShellLink;
Begin
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_RECENT, PIDL);
  SHGetPathFromIDList(PIDL, Path);
  CoCreateInstance(CLSID_ShellLink,
                  nil,
                  CLSCTX_INPROC_SERVER,
                  IShellLink,
                  psl);
  if psl.QueryInterface(IPersistFile, ppf) = 0 then
  begin
    //Program gets here if main thread calls DoSomething.  
    //Program does not get here if a seperate thread calls DoSomething.
.....

Thanks,

Will.
0
Comment
Question by:wolsen
3 Comments
 
LVL 5

Accepted Solution

by:
Greybird earned 400 total points
ID: 13892242
Have you tried calling CoInitialize or CoInitializeEx in the thread that calls the function ?
This function is called automatically for the main thread of an Exe, but you have to call it for other threads you create.

It may be the problem.
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13893493
Concur with Greybird
0
 
LVL 2

Author Comment

by:wolsen
ID: 13896767
Thanks Greybird.  That worked.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

850 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