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

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.
LVL 2
wolsenAsked:
Who is Participating?
 
GreybirdCommented:
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
 
pcsentinelCommented:
Concur with Greybird
0
 
wolsenAuthor Commented:
Thanks Greybird.  That worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.