예제 #1
0
        public void Revoke(Object o)
        {
            GCHandle h = GCHandle.Alloc(o, GCHandleType.Weak);

            if (mapIDToRef.ContainsKey(h.GetHashCode()))
            {
                mapIDToRef.Remove(h.GetHashCode());
            }
        }
예제 #2
0
        private static void ValidateGCHandle(GCHandle handle, GCHandleType type, object target)
        {
            try
            {
                Assert.Equal(target, handle.Target);
                Assert.True(handle.IsAllocated);

                Assert.NotEqual(IntPtr.Zero, GCHandle.ToIntPtr(handle));
                Assert.Equal(GCHandle.ToIntPtr(handle), (IntPtr)handle);
                Assert.Equal(((IntPtr)handle).GetHashCode(), handle.GetHashCode());

                if (type == GCHandleType.Pinned)
                {
                    if (target == null)
                    {
                        Assert.Equal(IntPtr.Zero, handle.AddrOfPinnedObject());
                    }
                    else
                    {
                        Assert.NotEqual(IntPtr.Zero, handle.AddrOfPinnedObject());
                    }
                }
            }
            finally
            {
                handle.Free();
                Assert.False(handle.IsAllocated);
            }
        }
예제 #3
0
        public void Publish(Object o)
        {
            GCHandle h = GCHandle.Alloc(o, GCHandleType.Weak);

            mapIDToRef.Add(h.GetHashCode(), h);
        }
예제 #4
0
        /// <summary>
        /// 現在の System.Runtime.InteropServices.GCHandle オブジェクトの識別子を返します
        /// </summary>
        /// <returns>現在の System.Runtime.InteropServices.GCHandle オブジェクトの識別子</returns>
#else
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
#endif
        public override int GetHashCode()
        {
            return(handle.GetHashCode());
        }
예제 #5
0
    public bool RunTest()
    {
        Object   o   = new Object();
        GCHandle gc  = GCHandle.Alloc(o);
        GCHandle gc2 = GCHandle.Alloc(o);
        GCHandle gc3 = gc;

        if (gc.Equals(null))
        {
            Console.WriteLine("Equals null failed");
            return(false);
        }

        if (gc.Equals(new Object()))
        {
            Console.WriteLine("Equals new Object failed");
            return(false);
        }

        if (gc.Equals(gc2))
        {
            Console.WriteLine("Equals GCHandle 1 failed");
            return(false);
        }

        if (!gc.Equals(gc3))
        {
            Console.WriteLine("Equals GCHandle 2 failed");
            return(false);
        }


        if (gc == gc2)
        {
            Console.WriteLine("== GCHandle 1 failed");
            return(false);
        }

        if (!(gc == gc3))
        {
            Console.WriteLine("== GCHandle 2 failed");
            return(false);
        }

        if (gc.GetHashCode() == gc2.GetHashCode())
        {
            Console.WriteLine("GetHashCode 1 failed");
            return(false);
        }

        if (gc.GetHashCode() != gc3.GetHashCode())
        {
            Console.WriteLine("GetHashCode 2 failed");
            return(false);
        }


        if (!(gc != gc2))
        {
            Console.WriteLine("!= GCHandle 1 failed");
            return(false);
        }

        if (gc != gc3)
        {
            Console.WriteLine("!= GCHandle 2 failed");
            return(false);
        }

        return(true);
    }
예제 #6
0
        int IWbemServices_Old.GetObjectAsync_([In][MarshalAs(UnmanagedType.BStr)]  string strObjectPath,
                                              [In] Int32 lFlags,
                                              [In][MarshalAs(UnmanagedType.Interface)]  IWbemContext pCtx,
                                              [In][MarshalAs(UnmanagedType.Interface)]  IWbemObjectSink pResponseHandler)
        {
//            pResponseHandler.SetStatus(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, null);
//            Marshal.ReleaseComObject(pResponseHandler);
//            return (int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND);

            Match match = Regex.Match(strObjectPath.ToLower(), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\"");

            if (match.Success == false)
            {
                pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero);
                Marshal.ReleaseComObject(pResponseHandler);
                return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND));
            }

            string className  = match.Groups[1].Value;
            string instanceId = match.Groups[2].Value;
            string processId  = match.Groups[3].Value;


            if (Instrumentation.ProcessIdentity != processId)
            {
                pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero);
                Marshal.ReleaseComObject(pResponseHandler);
                return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND));
            }

            int id = ((IConvertible)instanceId).ToInt32(null);

            if (InstrumentedAssembly.mapIDToRef.ContainsKey(id))
            {
                GCHandle h = (GCHandle)InstrumentedAssembly.mapIDToRef[id];
                if (h.IsAllocated)
                {
                    IWbemClassObjectFreeThreaded classObj;
                    pNamespace.GetObject_(h.Target.GetType().Name, 0, pCtx, out classObj, IntPtr.Zero);
                    IWbemClassObjectFreeThreaded inst;
                    classObj.SpawnInstance_(0, out inst);

                    Object o = h.GetHashCode();
                    inst.Put_("InstanceId", 0, ref o, 0);
                    o = Instrumentation.ProcessIdentity;
                    inst.Put_("ProcessId", 0, ref o, 0);
                    ConvertFuncToWMI func = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[h.Target.GetType()];
//                    func(h.Target, inst);
//                    pResponseHandler.Indicate_(1, ref inst);

                    pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero);
                    Marshal.ReleaseComObject(pResponseHandler);
                    return(0);
                }
            }
            pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero);
            Marshal.ReleaseComObject(pResponseHandler);
            return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND));

#if xxx
            IWbemClassObject classObj = null;
            IWbemCallResult  result   = null;
            pNamespace.GetObject("TestInstance", 0, pCtx, ref classObj, ref result);
            IWbemClassObject inst;
            classObj.SpawnInstance(0, out inst);

            TestInstance testInstance = (TestInstance)mapNameToTestInstance[match.Groups[1].Value];

            Object o = (object)testInstance.name;
            inst.Put("name", 0, ref o, 0);
            o = (object)testInstance.value;
            inst.Put("value", 0, ref o, 0);

            pResponseHandler.Indicate(1, new IWbemClassObject[] { inst });
            pResponseHandler.SetStatus_(0, 0, IntPtr.Zero, IntPtr.Zero);
            Marshal.ReleaseComObject(pResponseHandler);
#endif
        }