Exemplo n.º 1
0
 public static bool IsNoContextMTA()
 {
     if (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)
     {
         return(false);
     }
     if (CanCallCoGetObjectContext)
     {
         IntPtr zero = IntPtr.Zero;
         IntPtr ppv  = IntPtr.Zero;
         try
         {
             WmiNetUtilsHelper.APTTYPE apttype;
             if (CoGetObjectContext(ref IID_IComThreadingInfo, out zero) != 0)
             {
                 return(false);
             }
             if (WmiNetUtilsHelper.GetCurrentApartmentType_f(3, zero, out apttype) != 0)
             {
                 return(false);
             }
             if (apttype != WmiNetUtilsHelper.APTTYPE.APTTYPE_MTA)
             {
                 return(false);
             }
             if (Marshal.QueryInterface(zero, ref IID_IObjectContext, out ppv) == 0)
             {
                 return(false);
             }
         }
         finally
         {
             if (zero != IntPtr.Zero)
             {
                 Marshal.Release(zero);
             }
             if (ppv != IntPtr.Zero)
             {
                 Marshal.Release(ppv);
             }
         }
     }
     return(true);
 }
Exemplo n.º 2
0
        public static bool IsNoContextMTA()
        {
            WmiNetUtilsHelper.APTTYPE aPTTYPE = WmiNetUtilsHelper.APTTYPE.APTTYPE_STA;
            bool flag;

            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA)
            {
                if (MTAHelper.CanCallCoGetObjectContext)
                {
                    IntPtr zero   = IntPtr.Zero;
                    IntPtr intPtr = IntPtr.Zero;
                    try
                    {
                        if (MTAHelper.CoGetObjectContext(ref MTAHelper.IID_IComThreadingInfo, out zero) == 0)
                        {
                            if (WmiNetUtilsHelper.GetCurrentApartmentType_f(3, zero, out aPTTYPE) == 0)
                            {
                                if (aPTTYPE == WmiNetUtilsHelper.APTTYPE.APTTYPE_MTA)
                                {
                                    if (Marshal.QueryInterface(zero, ref MTAHelper.IID_IObjectContext, out intPtr) != 0)
                                    {
                                        return(true);
                                    }
                                    else
                                    {
                                        flag = false;
                                    }
                                }
                                else
                                {
                                    flag = false;
                                }
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    finally
                    {
                        if (zero != IntPtr.Zero)
                        {
                            Marshal.Release(zero);
                        }
                        if (intPtr != IntPtr.Zero)
                        {
                            Marshal.Release(intPtr);
                        }
                    }
                    return(flag);
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }