Ejemplo n.º 1
0
        public static object[] CoCreateInstanceEx(Guid clsid, ClsCtx ctx, string servername, Guid[] iids,
                                                  RpcAuthnSrv authent, RpcAuthzSrv author, string serverprinc,
                                                  RpcAuthnLevel level, RpcImpersLevel impers,
                                                  string domain, string user, string password)
        {
            int num = iids.Length;

            MULTI_QI[] amqi = new MULTI_QI[num];

            IntPtr guidbuf = Marshal.AllocCoTaskMem(num * 16);        // allocate memory for IIDs

            for (int i = 0; i < num; i++)
            {
                IntPtr piid = (IntPtr)((int)guidbuf + (i * 16));
                Marshal.StructureToPtr(iids[i], piid, false);
                amqi[i] = new MULTI_QI(piid);
            }

            COAUTHIDENTITY ci    = new COAUTHIDENTITY(user, domain, password);
            IntPtr         ciptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ci));

            Marshal.StructureToPtr(ci, ciptr, false);

            COAUTHINFO ca;

            if (string.IsNullOrEmpty(user))
            {
                ca = new COAUTHINFO(authent, author, serverprinc, level, impers, IntPtr.Zero /* ptr to coauth*/);
            }
            else
            {
                ca = new COAUTHINFO(authent, author, serverprinc, level, impers, ciptr);
            }
            IntPtr captr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ca));

            Marshal.StructureToPtr(ca, captr, false);

            COSERVERINFO cs = new COSERVERINFO(servername, captr);

            int hr = CoCreateInstanceEx(ref clsid, IntPtr.Zero, (int)ctx, cs, num, amqi);

            Marshal.DestroyStructure(captr, typeof(COAUTHINFO));
            Marshal.FreeCoTaskMem(captr);
            Marshal.FreeCoTaskMem(guidbuf);

            if (hr < 0)                        // FAILED()
            {
                Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
                Marshal.FreeCoTaskMem(ciptr);
                return(null);
            }

            int refcount;

            object[] ifret = new object[num];
            for (int i = 0; i < num; i++)
            {
                if (amqi[i].hr != 0)
                {
                    ifret[i] = (int)amqi[i].hr;
                    continue;
                }

                IntPtr ip = amqi[i].pItf;
                amqi[i].pItf = IntPtr.Zero;
                ifret[i]     = Marshal.GetObjectForIUnknown(ip);
                refcount     = Marshal.Release(ip);
                continue;
#if COMPROXY
                hr = CoSetProxyBlanket(ip, authent, author, serverprinc, level, impers, ciptr, 0);
                if (hr < 0)                        // FAILED()
                {
                    ifret[i] = (int)hr;
                }
                else
                {
                    ifret[i] = Marshal.GetObjectForIUnknown(ip);
                }

                refcount = Marshal.Release(ip);
 #endif
            }

            Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
            Marshal.FreeCoTaskMem(ciptr);

            return(ifret);
        }
Ejemplo n.º 2
0
        public static object[] CoCreateInstanceEx(Guid clsid, ClsCtx ctx, string servername, Guid[] iids,
                                                        RpcAuthnSrv authent, RpcAuthzSrv author, string serverprinc,
                                                        RpcAuthnLevel level, RpcImpersLevel impers,
                                                        string domain,  string user, string password)
        {
            int num = iids.Length;
                MULTI_QI[] amqi = new MULTI_QI[num];

                IntPtr guidbuf = Marshal.AllocCoTaskMem(num * 16);    // allocate memory for IIDs
                for (int i = 0; i < num; i++)
                {
                    IntPtr piid = (IntPtr)((int)guidbuf + (i * 16));
                    Marshal.StructureToPtr(iids[i], piid, false);
                    amqi[i] = new MULTI_QI(piid);
                }

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

                COAUTHINFO ca;
                if (string.IsNullOrEmpty(user))
                {
                    ca = new COAUTHINFO(authent, author, serverprinc, level, impers, IntPtr.Zero /* ptr to coauth*/);
                }
                else
                {
                    ca = new COAUTHINFO(authent, author, serverprinc, level, impers, ciptr);

                }
                IntPtr captr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ca));
                Marshal.StructureToPtr(ca, captr, false);

                COSERVERINFO cs = new COSERVERINFO(servername, captr);

                int hr = CoCreateInstanceEx(ref clsid, IntPtr.Zero, (int)ctx, cs, num, amqi);

                Marshal.DestroyStructure(captr, typeof(COAUTHINFO));
                Marshal.FreeCoTaskMem(captr);
                Marshal.FreeCoTaskMem(guidbuf);

                if (hr < 0)                    // FAILED()
                {
                    Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
                    Marshal.FreeCoTaskMem(ciptr);
                    return null;
                }

                int refcount;
                object[] ifret = new object[num];
                for (int i = 0; i < num; i++)
                {
                    if (amqi[i].hr != 0)
                    {
                        ifret[i] = (int)amqi[i].hr;
                        continue;
                    }

                    IntPtr ip = amqi[i].pItf;
                    amqi[i].pItf = IntPtr.Zero;
                    ifret[i] = Marshal.GetObjectForIUnknown(ip);
                    refcount = Marshal.Release(ip);
                    continue;
            #if COMPROXY

                    hr = CoSetProxyBlanket(ip, authent, author, serverprinc, level, impers, ciptr, 0);
                    if (hr < 0)                    // FAILED()
                        ifret[i] = (int)hr;
                    else
                        ifret[i] = Marshal.GetObjectForIUnknown(ip);

                    refcount = Marshal.Release(ip);
             #endif
                }

                Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
                Marshal.FreeCoTaskMem(ciptr);

                return ifret;
        }