Re: COM-Interface usage in MultiThreaded-Environment fails

Tech-Archive recommends: Fix windows errors by optimizing your registry



Do you realy need the threads to be apartment (STA) threaded? I asume
IShellFolder supports both STA and MTA apartments, so I assume MSFT made
them thread safe.

Willy.

"taj" <taj@xxxxxxxxxxx> wrote in message news:di6kvp$t9b$1@xxxxxxxxxxxx
> Hi,
>
> i got really frustated by this; in the following code, i try to use
> SHGetDesktopFolder-function to retrieve the IShellFolder-interface for -
> well - the desktop (root) folder.
> works well for the first (main) thread doing so; however, it does not for
> the second also doing this if the interface isn't freed by the first
> thread before the second trys to use it.
> if using IntPtr as out-parameters for SHGetDesktopFolder-function, i
> recognized that both threads (although both STA-Apartmented) got the same
> interface-pointer by the function call.
> i asume the marshaller recognizes this and doesn't allow to work the
> second thread on the interface if the first thread not quitted doing so by
> releasing the interface.
> however, calling the function in the second thread works somehow, only
> using it (casting it to another interface in this case) fails for the
> second thread.
> any ideas?
>
> regards,
> taj
>
> //////////////////////////////////////////////////////////////////
>
> using System;
> using System.Threading;
> using System.Runtime.InteropServices;
>
> namespace ConsoleApplication1 {
> class Class1 {
> [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
> Guid("000214E5-0000-0000-C000-000000000046")]
> public interface IShellIcon {
> }
>
> [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
> Guid("000214E6-0000-0000-C000-000000000046")]
> public interface IShellFolder {
> }
>
> [DllImport("shell32.dll")]
> public static extern Int32 SHGetDesktopFolder(out IShellFolder ppshf);
>
> [STAThread]
> static void Main(string[] args) {
> IShellFolder shellFolder = null;
> SHGetDesktopFolder(out shellFolder);
>
> Thread mythread = new Thread(new ThreadStart(MyThreadWorker));
> mythread.ApartmentState = ApartmentState.STA;
> mythread.Start();
>
> //Marshal.ReleaseComObject(shellFolder); //<-- Uncomment to make it work
> mythread.Join();
> }
>
> public static void MyThreadWorker() {
> try {
> IShellIcon shellIcon = null;
> IShellFolder shellFolder = null;
>
> SHGetDesktopFolder(out shellFolder);
> shellIcon = shellFolder as IShellIcon;
>
> if(shellIcon != null)
> System.Windows.Forms.MessageBox.Show("Got it!");
> } catch(Exception ex) {
> string test = ex.Message;
> }
> }
> }
> }


.



Relevant Pages

  • one questiong about IShellFolder
    ... What I wanna know is how to get system icon index by using IShellFolder ... value in SHFILEINFO structure which is retured from SHGetFileInfo function.. ... but even though I got the IExtractIcon pointer from IShellFolder interface, ...
    (microsoft.public.vc.mfc)
  • Re: show windows explorer context-menu
    ... It's done using the IContextMenu COM interface. ... If your app is based on IShellFolder and so on, ... you should switch to IShellFolder. ...
    (microsoft.public.vb.winapi)