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

Posted on 2005-04-28
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?
ShellApi,ShlObj, ComObj, ActiveX;
Procedure DoSomething;
  PIDL: PItemIDList;
  Path: array[0..MAX_PATH] of Char;
  psl  : IShellLink;
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_RECENT, PIDL);
  SHGetPathFromIDList(PIDL, Path);
  if psl.QueryInterface(IPersistFile, ppf) = 0 then
    //Program gets here if main thread calls DoSomething.  
    //Program does not get here if a seperate thread calls DoSomething.


Question by:wolsen
    LVL 5

    Accepted Solution

    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.
    LVL 11

    Expert Comment

    Concur with Greybird
    LVL 2

    Author Comment

    Thanks Greybird.  That worked.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now