private static IRemoteWebConfigurationHostServer CreateRemoteObjectOn32BitPlatform(string server, string username, string domain, string password)
 {
     IRemoteWebConfigurationHostServer objectForIUnknown;
     MULTI_QI[] amqi = new MULTI_QI[1];
     IntPtr zero = IntPtr.Zero;
     COAUTHINFO structure = null;
     IntPtr ptr = IntPtr.Zero;
     COSERVERINFO srv = null;
     Guid gUID = typeof(RemoteWebConfigurationHostServer).GUID;
     int errorCode = 0;
     COAUTHIDENTITY coauthidentity = null;
     IntPtr ptr3 = IntPtr.Zero;
     try
     {
         zero = Marshal.AllocCoTaskMem(0x10);
         Marshal.StructureToPtr(typeof(IRemoteWebConfigurationHostServer).GUID, zero, false);
         amqi[0] = new MULTI_QI(zero);
         coauthidentity = new COAUTHIDENTITY(username, domain, password);
         ptr3 = Marshal.AllocCoTaskMem(Marshal.SizeOf(coauthidentity));
         Marshal.StructureToPtr(coauthidentity, ptr3, false);
         structure = new COAUTHINFO(RpcAuthent.WinNT, RpcAuthor.None, null, RpcLevel.Default, RpcImpers.Impersonate, ptr3);
         ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(structure));
         Marshal.StructureToPtr(structure, ptr, false);
         srv = new COSERVERINFO(server, ptr);
         errorCode = System.Web.UnsafeNativeMethods.CoCreateInstanceEx(ref gUID, IntPtr.Zero, 0x10, srv, 1, amqi);
         if (errorCode == -2147221164)
         {
             throw new Exception(System.Web.SR.GetString("Make_sure_remote_server_is_enabled_for_config_access"));
         }
         if (errorCode < 0)
         {
             Marshal.ThrowExceptionForHR(errorCode);
         }
         if (amqi[0].hr < 0)
         {
             Marshal.ThrowExceptionForHR(amqi[0].hr);
         }
         errorCode = System.Web.UnsafeNativeMethods.CoSetProxyBlanket(amqi[0].pItf, RpcAuthent.WinNT, RpcAuthor.None, null, RpcLevel.Default, RpcImpers.Impersonate, ptr3, 0);
         if (errorCode < 0)
         {
             Marshal.ThrowExceptionForHR(errorCode);
         }
         objectForIUnknown = (IRemoteWebConfigurationHostServer) Marshal.GetObjectForIUnknown(amqi[0].pItf);
     }
     finally
     {
         if (amqi[0].pItf != IntPtr.Zero)
         {
             Marshal.Release(amqi[0].pItf);
             amqi[0].pItf = IntPtr.Zero;
         }
         amqi[0].piid = IntPtr.Zero;
         if (ptr != IntPtr.Zero)
         {
             Marshal.DestroyStructure(ptr, typeof(COAUTHINFO));
             Marshal.FreeCoTaskMem(ptr);
         }
         if (ptr3 != IntPtr.Zero)
         {
             Marshal.DestroyStructure(ptr3, typeof(COAUTHIDENTITY));
             Marshal.FreeCoTaskMem(ptr3);
         }
         if (zero != IntPtr.Zero)
         {
             Marshal.FreeCoTaskMem(zero);
         }
     }
     return objectForIUnknown;
 }
        private static IRemoteWebConfigurationHostServer CreateRemoteObjectOn32BitPlatform(string server, string username, string domain, string password)
        {
            MULTI_QI []     amqi            = new MULTI_QI[1];
            IntPtr          guidbuf         = IntPtr.Zero;
            COAUTHINFO      ca              = null;
            IntPtr          captr           = IntPtr.Zero;
            COSERVERINFO    cs              = null;
            Guid            clsid           = typeof(RemoteWebConfigurationHostServer).GUID;
            int             hr              = 0;
            COAUTHIDENTITY  ci              = null;
            IntPtr          ciptr           = IntPtr.Zero;

            try {
                guidbuf = Marshal.AllocCoTaskMem(16);
                Marshal.StructureToPtr(typeof(IRemoteWebConfigurationHostServer).GUID, guidbuf, false);
                amqi[0] = new MULTI_QI(guidbuf);

                ci = new COAUTHIDENTITY(username, domain, password);
                ciptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ci));
                Marshal.StructureToPtr(ci, ciptr, false);

                ca = new COAUTHINFO(RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr);
                captr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ca));
                Marshal.StructureToPtr(ca, captr, false);

                cs = new COSERVERINFO(server, captr);
                hr = UnsafeNativeMethods.CoCreateInstanceEx(ref clsid, IntPtr.Zero, (int)ClsCtx.RemoteServer, cs, 1, amqi);
                if ((uint)hr == 0x80040154)
                        throw new Exception(SR.GetString(SR.Make_sure_remote_server_is_enabled_for_config_access));
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                if (amqi[0].hr < 0)
                    Marshal.ThrowExceptionForHR(amqi[0].hr);
                hr = UnsafeNativeMethods.CoSetProxyBlanket(amqi[0].pItf, RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                return (IRemoteWebConfigurationHostServer)Marshal.GetObjectForIUnknown(amqi[0].pItf);
            } finally {
                if (amqi[0].pItf != IntPtr.Zero)
                {
                    Marshal.Release(amqi[0].pItf);
                    amqi[0].pItf = IntPtr.Zero;
                }
                amqi[0].piid = IntPtr.Zero;
                if (captr != IntPtr.Zero) {
                    Marshal.DestroyStructure(captr, typeof(COAUTHINFO));
                    Marshal.FreeCoTaskMem(captr);
                }
                if (ciptr != IntPtr.Zero) {
                    Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
                    Marshal.FreeCoTaskMem(ciptr);
                }
                if (guidbuf != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(guidbuf);
            }
#else // !FEATURE_PAL
            throw new NotSupportedException();
#endif // !FEATURE_PAL
        }