Ejemplo n.º 1
0
		public void RegisterAssemblySpecificSchema()
		{
			SecurityHelper.UnmanagedCode.Demand();
			Type[] instrumentedTypes = InstrumentedAttribute.GetInstrumentedTypes(this.assembly);
			StringCollection stringCollections = new StringCollection();
			StringCollection stringCollections1 = new StringCollection();
			StringCollection stringCollections2 = new StringCollection();
			string[] mofFormat = new string[(int)instrumentedTypes.Length];
			CodeWriter codeWriter = new CodeWriter();
			ReferencesCollection referencesCollection = new ReferencesCollection();
			codeWriter.AddChild(referencesCollection.UsingCode);
			referencesCollection.Add(typeof(object));
			referencesCollection.Add(typeof(ManagementClass));
			referencesCollection.Add(typeof(Marshal));
			referencesCollection.Add(typeof(SuppressUnmanagedCodeSecurityAttribute));
			referencesCollection.Add(typeof(FieldInfo));
			referencesCollection.Add(typeof(Hashtable));
			codeWriter.Line();
			CodeWriter codeWriter1 = codeWriter.AddChild("public class WMINET_Converter");
			codeWriter1.Line("public static Hashtable mapTypeToConverter = new Hashtable();");
			CodeWriter codeWriter2 = codeWriter1.AddChild("static WMINET_Converter()");
			Hashtable hashtables = new Hashtable();
			for (int i = 0; i < (int)instrumentedTypes.Length; i++)
			{
				hashtables[instrumentedTypes[i]] = string.Concat("ConvertClass_", i);
			}
			bool beCompared = this.IsSchemaToBeCompared();
			bool flag = false;
			if (!beCompared)
			{
				flag = !this.IsAssemblyRegistered();
			}
			for (int j = 0; j < (int)instrumentedTypes.Length; j++)
			{
				SchemaMapping schemaMapping = new SchemaMapping(instrumentedTypes[j], this, hashtables);
				codeWriter2.Line(string.Format("mapTypeToConverter[typeof({0})] = typeof({1});", schemaMapping.ClassType.FullName.Replace('+', '.'), schemaMapping.CodeClassName));
				if (beCompared && !this.IsClassAlreadyPresentInRepository(schemaMapping.NewClass))
				{
					flag = true;
				}
				SchemaNaming.ReplaceClassIfNecessary(schemaMapping.ClassPath, schemaMapping.NewClass);
				mofFormat[j] = SchemaNaming.GetMofFormat(schemaMapping.NewClass);
				codeWriter.AddChild(schemaMapping.Code);
				InstrumentationType instrumentationType = schemaMapping.InstrumentationType;
				switch (instrumentationType)
				{
					case InstrumentationType.Instance:
					{
						stringCollections1.Add(schemaMapping.ClassName);
						break;
					}
					case InstrumentationType.Event:
					{
						stringCollections.Add(schemaMapping.ClassName);
						break;
					}
					case InstrumentationType.Abstract:
					{
						stringCollections2.Add(schemaMapping.ClassName);
						break;
					}
				}
			}
			this.RegisterAssemblySpecificDecoupledProviderInstance();
			this.RegisterProviderAsEventProvider(stringCollections);
			this.RegisterProviderAsInstanceProvider();
			this.RegisterAssemblyAsInstrumented();
			Directory.CreateDirectory(this.DataDirectory);
			using (StreamWriter streamWriter = new StreamWriter(this.CodePath, false, Encoding.Unicode))
			{
				streamWriter.WriteLine(codeWriter);
				streamWriter.WriteLine(string.Concat("class IWOA\r\n{\r\nprotected const string DllName = \"wminet_utils.dll\";\r\nprotected const string EntryPointName = \"UFunc\";\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"GetPropertyHandle\")] public static extern int GetPropertyHandle_f27(int vFunc, IntPtr pWbemClassObject, [In][MarshalAs(UnmanagedType.LPWStr)]  string   wszPropertyName, [Out] out Int32 pType, [Out] out Int32 plHandle);\r\n//[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Byte aData);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadPropertyValue\")] public static extern int ReadPropertyValue_f29(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lBufferSize, [Out] out Int32 plNumBytes, [Out] out Byte aData);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadDWORD\")] public static extern int ReadDWORD_f30(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out] out UInt32 pdw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDWORD\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] UInt32 dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadQWORD\")] public static extern int ReadQWORD_f32(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out] out UInt64 pqw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteQWORD\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] UInt64 pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"GetPropertyInfoByHandle\")] public static extern int GetPropertyInfoByHandle_f34(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out][MarshalAs(UnmanagedType.BStr)]  out string   pstrName, [Out] out Int32 pType);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Lock\")] public static extern int Lock_f35(int vFunc, IntPtr pWbemClassObject, [In] Int32 lFlags);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Unlock\")] public static extern int Unlock_f36(int vFunc, IntPtr pWbemClassObject, [In] Int32 lFlags);\r\n\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Put\")] public static extern int Put_f5(int vFunc, IntPtr pWbemClassObject, [In][MarshalAs(UnmanagedType.LPWStr)]  string   wszName, [In] Int32 lFlags, [In] ref object pVal, [In] Int32 Type);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In][MarshalAs(UnmanagedType.LPWStr)] string str);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Byte n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref SByte n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Int16 n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref UInt16 n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\", CharSet=CharSet.Unicode)] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Char c);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDWORD\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteSingle\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Single dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteQWORD\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int64 pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDouble\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Double pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Clone\")] public static extern int Clone_f(int vFunc, IntPtr pWbemClassObject, [Out] out IntPtr ppCopy);\r\n}\r\ninterface IWmiConverter\r\n{\r\n    void ToWMI(object obj);\r\n    ManagementObject GetInstance();\r\n}\r\nclass SafeAssign\r\n{\r\n    public static UInt16 boolTrue = 0xffff;\r\n    public static UInt16 boolFalse = 0;\r\n    static Hashtable validTypes = new Hashtable();\r\n    static SafeAssign()\r\n    {\r\n        validTypes.Add(typeof(SByte), null);\r\n        validTypes.Add(typeof(Byte), null);\r\n        validTypes.Add(typeof(Int16), null);\r\n        validTypes.Add(typeof(UInt16), null);\r\n        validTypes.Add(typeof(Int32), null);\r\n        validTypes.Add(typeof(UInt32), null);\r\n        validTypes.Add(typeof(Int64), null);\r\n        validTypes.Add(typeof(UInt64), null);\r\n        validTypes.Add(typeof(Single), null);\r\n        validTypes.Add(typeof(Double), null);\r\n        validTypes.Add(typeof(Boolean), null);\r\n        validTypes.Add(typeof(String), null);\r\n        validTypes.Add(typeof(Char), null);\r\n        validTypes.Add(typeof(DateTime), null);\r\n        validTypes.Add(typeof(TimeSpan), null);\r\n        validTypes.Add(typeof(ManagementObject), null);\r\n        nullClass.SystemProperties [\"__CLASS\"].Value = \"nullInstance\";\r\n    }\r\n    public static object GetInstance(object o)\r\n    {\r\n        if(o is ManagementObject)\r\n            return o;\r\n        return null;\r\n    }\r\n    static ManagementClass nullClass = new ManagementClass(new ManagementPath(@\"", this.NamespaceName, "\"));\r\n    \r\n    public static ManagementObject GetManagementObject(object o)\r\n    {\r\n        if(o != null && o is ManagementObject)\r\n            return o as ManagementObject;\r\n        // Must return empty instance\r\n        return nullClass.CreateInstance();\r\n    }\r\n    public static object GetValue(object o)\r\n    {\r\n        Type t = o.GetType();\r\n        if(t.IsArray)\r\n            t = t.GetElementType();\r\n        if(validTypes.Contains(t))\r\n            return o;\r\n        return null;\r\n    }\r\n    public static string WMITimeToString(DateTime dt)\r\n    {\r\n        TimeSpan ts = dt.Subtract(dt.ToUniversalTime());\r\n        int diffUTC = (ts.Minutes + ts.Hours * 60);\r\n        if(diffUTC >= 0)\r\n            return String.Format(\"{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}.{6:D3}000+{7:D3}\", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, diffUTC);\r\n        return String.Format(\"{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}.{6:D3}000-{7:D3}\", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, -diffUTC);\r\n    }\r\n    public static string WMITimeToString(TimeSpan ts)\r\n    {\r\n        return String.Format(\"{0:D8}{1:D2}{2:D2}{3:D2}.{4:D3}000:000\", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);\r\n    }\r\n    public static string[] WMITimeArrayToStringArray(DateTime[] dates)\r\n    {\r\n        string[] strings = new string[dates.Length];\r\n        for(int i=0;i<dates.Length;i++)\r\n            strings[i] = WMITimeToString(dates[i]);\r\n        return strings;\r\n    }\r\n    public static string[] WMITimeArrayToStringArray(TimeSpan[] timeSpans)\r\n    {\r\n        string[] strings = new string[timeSpans.Length];\r\n        for(int i=0;i<timeSpans.Length;i++)\r\n            strings[i] = WMITimeToString(timeSpans[i]);\r\n        return strings;\r\n    }\r\n}\r\n"));
			}
			using (StreamWriter streamWriter1 = new StreamWriter(this.MofPath, false, Encoding.Unicode))
			{
				streamWriter1.WriteLine(this.GenerateMof(mofFormat));
			}
			if (flag)
			{
				SchemaNaming.RegisterSchemaUsingMofcomp(this.MofPath);
			}
		}
Ejemplo n.º 2
0
        public void RegisterAssemblySpecificSchema()
        {
            SecurityHelper.UnmanagedCode.Demand();             // Bug#112640 - Close off any potential use from anything but fully trusted code

            Type[]           types     = InstrumentedAttribute.GetInstrumentedTypes(assembly);
            StringCollection events    = new StringCollection();
            StringCollection instances = new StringCollection();
            StringCollection abstracts = new StringCollection();

            string[]             mofs       = new string[types.Length];
            CodeWriter           code       = new CodeWriter();
            ReferencesCollection references = new ReferencesCollection();

            // Add the node with all the 'using' statements at the top
            code.AddChild(references.UsingCode);
            references.Add(typeof(Object));
            references.Add(typeof(ManagementClass));
            references.Add(typeof(Marshal));
            references.Add(typeof(System.Security.SuppressUnmanagedCodeSecurityAttribute));
            references.Add(typeof(System.Reflection.FieldInfo));
            references.Add(typeof(Hashtable));

            // Add a blank line
            code.Line();

            // Add master converter class
            CodeWriter codeWMIConverter = code.AddChild("public class WMINET_Converter");

            // Add master map of types to converters
            codeWMIConverter.Line("public static Hashtable mapTypeToConverter = new Hashtable();");

            // Add master CCTOR
            CodeWriter codeCCTOR = codeWMIConverter.AddChild("static WMINET_Converter()");

            // Make mapping of types to converter class names
            Hashtable mapTypeToConverterClassName = new Hashtable();

            for (int i = 0; i < types.Length; i++)
            {
                mapTypeToConverterClassName[types[i]] = "ConvertClass_" + i;
            }

            // [Microsoft] VSUQFE#2248 (VSWhidbey 231885)
            bool bSchemaToBeCompared = IsSchemaToBeCompared();
            bool bNewClassToCompile  = false;

            // Mof compilation is dictated by, whether the assembly is registered as instrumented or not
            if (bSchemaToBeCompared == false)
            {
                bNewClassToCompile = !IsAssemblyRegistered();
            }

            for (int i = 0; i < types.Length; i++)
            {
                SchemaMapping mapping = new SchemaMapping(types[i], this, mapTypeToConverterClassName);

                codeCCTOR.Line(String.Format("mapTypeToConverter[typeof({0})] = typeof({1});", mapping.ClassType.FullName.Replace('+', '.'), mapping.CodeClassName));                  // bug#92918 - watch for nested classes
                // [Microsoft] VSUQFE#2248 (VSWhidbey 231885)
                if (bSchemaToBeCompared == true && IsClassAlreadyPresentInRepository(mapping.NewClass) == false)
                {
                    bNewClassToCompile = true;
                }

                ReplaceClassIfNecessary(mapping.ClassPath, mapping.NewClass);

                mofs[i] = GetMofFormat(mapping.NewClass);
                code.AddChild(mapping.Code);
                switch (mapping.InstrumentationType)
                {
                case InstrumentationType.Event:
                    events.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Instance:
                    instances.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Abstract:
                    abstracts.Add(mapping.ClassName);
                    break;

                default:
                    break;
                }
            }

            RegisterAssemblySpecificDecoupledProviderInstance();
            RegisterProviderAsEventProvider(events);
            RegisterProviderAsInstanceProvider();
            RegisterAssemblyAsInstrumented();
            Directory.CreateDirectory(DataDirectory);
            using (StreamWriter log = new StreamWriter(CodePath, false, Encoding.Unicode))
            {
                log.WriteLine(code);
                log.WriteLine(iwoaDef + "new ManagementPath(@\"" + this.NamespaceName + "\")" + iwoaDefEnd);
            }

            // Always generate the MOF in unicode
            using (StreamWriter log = new StreamWriter(MofPath, false, Encoding.Unicode))
            {
                log.WriteLine(GenerateMof(mofs));
            }

            // [Microsoft] VSUQFE#2248 (VSWhidbey 231885)
            // Write the mof to a file and compile it only if there are any new classes , apart from
            // what is there in the repository
            if (bNewClassToCompile == true)
            {
                RegisterSchemaUsingMofcomp(MofPath);
                //WMICapabilities.AddAutorecoverMof(MofPath);
            }
        }
        public void RegisterAssemblySpecificSchema()
        {
            SecurityHelper.UnmanagedCode.Demand();
            Type[]           instrumentedTypes = InstrumentedAttribute.GetInstrumentedTypes(this.assembly);
            StringCollection events            = new StringCollection();
            StringCollection strings2          = new StringCollection();
            StringCollection strings3          = new StringCollection();

            string[]             mofs        = new string[instrumentedTypes.Length];
            CodeWriter           writer      = new CodeWriter();
            ReferencesCollection referencess = new ReferencesCollection();

            writer.AddChild(referencess.UsingCode);
            referencess.Add(typeof(object));
            referencess.Add(typeof(ManagementClass));
            referencess.Add(typeof(Marshal));
            referencess.Add(typeof(SuppressUnmanagedCodeSecurityAttribute));
            referencess.Add(typeof(FieldInfo));
            referencess.Add(typeof(Hashtable));
            writer.Line();
            CodeWriter writer2 = writer.AddChild("public class WMINET_Converter");

            writer2.Line("public static Hashtable mapTypeToConverter = new Hashtable();");
            CodeWriter writer3 = writer2.AddChild("static WMINET_Converter()");
            Hashtable  mapTypeToConverterClassName = new Hashtable();

            for (int i = 0; i < instrumentedTypes.Length; i++)
            {
                mapTypeToConverterClassName[instrumentedTypes[i]] = "ConvertClass_" + i;
            }
            bool flag  = this.IsSchemaToBeCompared();
            bool flag2 = false;

            if (!flag)
            {
                flag2 = !this.IsAssemblyRegistered();
            }
            for (int j = 0; j < instrumentedTypes.Length; j++)
            {
                SchemaMapping mapping = new SchemaMapping(instrumentedTypes[j], this, mapTypeToConverterClassName);
                writer3.Line(string.Format("mapTypeToConverter[typeof({0})] = typeof({1});", mapping.ClassType.FullName.Replace('+', '.'), mapping.CodeClassName));
                if (flag && !this.IsClassAlreadyPresentInRepository(mapping.NewClass))
                {
                    flag2 = true;
                }
                ReplaceClassIfNecessary(mapping.ClassPath, mapping.NewClass);
                mofs[j] = GetMofFormat(mapping.NewClass);
                writer.AddChild(mapping.Code);
                switch (mapping.InstrumentationType)
                {
                case InstrumentationType.Instance:
                    strings2.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Event:
                    events.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Abstract:
                    strings3.Add(mapping.ClassName);
                    break;
                }
            }
            this.RegisterAssemblySpecificDecoupledProviderInstance();
            this.RegisterProviderAsEventProvider(events);
            this.RegisterProviderAsInstanceProvider();
            this.RegisterAssemblyAsInstrumented();
            Directory.CreateDirectory(this.DataDirectory);
            using (StreamWriter writer4 = new StreamWriter(this.CodePath, false, Encoding.Unicode))
            {
                writer4.WriteLine(writer);
                writer4.WriteLine("class IWOA\r\n{\r\nprotected const string DllName = \"wminet_utils.dll\";\r\nprotected const string EntryPointName = \"UFunc\";\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"GetPropertyHandle\")] public static extern int GetPropertyHandle_f27(int vFunc, IntPtr pWbemClassObject, [In][MarshalAs(UnmanagedType.LPWStr)]  string   wszPropertyName, [Out] out Int32 pType, [Out] out Int32 plHandle);\r\n//[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Byte aData);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadPropertyValue\")] public static extern int ReadPropertyValue_f29(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lBufferSize, [Out] out Int32 plNumBytes, [Out] out Byte aData);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadDWORD\")] public static extern int ReadDWORD_f30(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out] out UInt32 pdw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDWORD\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] UInt32 dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"ReadQWORD\")] public static extern int ReadQWORD_f32(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out] out UInt64 pqw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteQWORD\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] UInt64 pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"GetPropertyInfoByHandle\")] public static extern int GetPropertyInfoByHandle_f34(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [Out][MarshalAs(UnmanagedType.BStr)]  out string   pstrName, [Out] out Int32 pType);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Lock\")] public static extern int Lock_f35(int vFunc, IntPtr pWbemClassObject, [In] Int32 lFlags);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Unlock\")] public static extern int Unlock_f36(int vFunc, IntPtr pWbemClassObject, [In] Int32 lFlags);\r\n\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Put\")] public static extern int Put_f5(int vFunc, IntPtr pWbemClassObject, [In][MarshalAs(UnmanagedType.LPWStr)]  string   wszName, [In] Int32 lFlags, [In] ref object pVal, [In] Int32 Type);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In][MarshalAs(UnmanagedType.LPWStr)] string str);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Byte n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref SByte n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Int16 n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\")] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref UInt16 n);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WritePropertyValue\", CharSet=CharSet.Unicode)] public static extern int WritePropertyValue_f28(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 lNumBytes, [In] ref Char c);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDWORD\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int32 dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteSingle\")] public static extern int WriteDWORD_f31(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Single dw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteQWORD\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Int64 pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"WriteDouble\")] public static extern int WriteQWORD_f33(int vFunc, IntPtr pWbemClassObject, [In] Int32 lHandle, [In] Double pw);\r\n[SuppressUnmanagedCodeSecurity, DllImport(DllName, EntryPoint=\"Clone\")] public static extern int Clone_f(int vFunc, IntPtr pWbemClassObject, [Out] out IntPtr ppCopy);\r\n}\r\ninterface IWmiConverter\r\n{\r\n    void ToWMI(object obj);\r\n    ManagementObject GetInstance();\r\n}\r\nclass SafeAssign\r\n{\r\n    public static UInt16 boolTrue = 0xffff;\r\n    public static UInt16 boolFalse = 0;\r\n    static Hashtable validTypes = new Hashtable();\r\n    static SafeAssign()\r\n    {\r\n        validTypes.Add(typeof(SByte), null);\r\n        validTypes.Add(typeof(Byte), null);\r\n        validTypes.Add(typeof(Int16), null);\r\n        validTypes.Add(typeof(UInt16), null);\r\n        validTypes.Add(typeof(Int32), null);\r\n        validTypes.Add(typeof(UInt32), null);\r\n        validTypes.Add(typeof(Int64), null);\r\n        validTypes.Add(typeof(UInt64), null);\r\n        validTypes.Add(typeof(Single), null);\r\n        validTypes.Add(typeof(Double), null);\r\n        validTypes.Add(typeof(Boolean), null);\r\n        validTypes.Add(typeof(String), null);\r\n        validTypes.Add(typeof(Char), null);\r\n        validTypes.Add(typeof(DateTime), null);\r\n        validTypes.Add(typeof(TimeSpan), null);\r\n        validTypes.Add(typeof(ManagementObject), null);\r\n        nullClass.SystemProperties [\"__CLASS\"].Value = \"nullInstance\";\r\n    }\r\n    public static object GetInstance(object o)\r\n    {\r\n        if(o is ManagementObject)\r\n            return o;\r\n        return null;\r\n    }\r\n    static ManagementClass nullClass = new ManagementClass(new ManagementPath(@\"" + this.NamespaceName + "\"));\r\n    \r\n    public static ManagementObject GetManagementObject(object o)\r\n    {\r\n        if(o != null && o is ManagementObject)\r\n            return o as ManagementObject;\r\n        // Must return empty instance\r\n        return nullClass.CreateInstance();\r\n    }\r\n    public static object GetValue(object o)\r\n    {\r\n        Type t = o.GetType();\r\n        if(t.IsArray)\r\n            t = t.GetElementType();\r\n        if(validTypes.Contains(t))\r\n            return o;\r\n        return null;\r\n    }\r\n    public static string WMITimeToString(DateTime dt)\r\n    {\r\n        TimeSpan ts = dt.Subtract(dt.ToUniversalTime());\r\n        int diffUTC = (ts.Minutes + ts.Hours * 60);\r\n        if(diffUTC >= 0)\r\n            return String.Format(\"{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}.{6:D3}000+{7:D3}\", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, diffUTC);\r\n        return String.Format(\"{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}.{6:D3}000-{7:D3}\", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, -diffUTC);\r\n    }\r\n    public static string WMITimeToString(TimeSpan ts)\r\n    {\r\n        return String.Format(\"{0:D8}{1:D2}{2:D2}{3:D2}.{4:D3}000:000\", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);\r\n    }\r\n    public static string[] WMITimeArrayToStringArray(DateTime[] dates)\r\n    {\r\n        string[] strings = new string[dates.Length];\r\n        for(int i=0;i<dates.Length;i++)\r\n            strings[i] = WMITimeToString(dates[i]);\r\n        return strings;\r\n    }\r\n    public static string[] WMITimeArrayToStringArray(TimeSpan[] timeSpans)\r\n    {\r\n        string[] strings = new string[timeSpans.Length];\r\n        for(int i=0;i<timeSpans.Length;i++)\r\n            strings[i] = WMITimeToString(timeSpans[i]);\r\n        return strings;\r\n    }\r\n}\r\n");
            }
            using (StreamWriter writer5 = new StreamWriter(this.MofPath, false, Encoding.Unicode))
            {
                writer5.WriteLine(this.GenerateMof(mofs));
            }
            if (flag2)
            {
                RegisterSchemaUsingMofcomp(this.MofPath);
            }
        }
Ejemplo n.º 4
0
		public void RegisterAssemblySpecificSchema()
		{
			SecurityHelper.UnmanagedCode.Demand(); // Bug#112640 - Close off any potential use from anything but fully trusted code

			Type[] types = InstrumentedAttribute.GetInstrumentedTypes(assembly);
			StringCollection events = new StringCollection();
			StringCollection instances = new StringCollection();
			StringCollection abstracts = new StringCollection();
			string[] mofs = new string[types.Length];
			CodeWriter code = new CodeWriter();
			ReferencesCollection references = new ReferencesCollection();

			// Add the node with all the 'using' statements at the top
			code.AddChild(references.UsingCode);
			references.Add(typeof(Object));
			references.Add(typeof(ManagementClass));
			references.Add(typeof(Marshal));
			references.Add(typeof(System.Security.SuppressUnmanagedCodeSecurityAttribute));
			references.Add(typeof(System.Reflection.FieldInfo));
			references.Add(typeof(Hashtable));

			// Add a blank line
			code.Line();

			// Add master converter class
			CodeWriter codeWMIConverter = code.AddChild("public class WMINET_Converter");
            
			// Add master map of types to converters
			codeWMIConverter.Line("public static Hashtable mapTypeToConverter = new Hashtable();");
          
			// Add master CCTOR
			CodeWriter codeCCTOR = codeWMIConverter.AddChild("static WMINET_Converter()");

			// Make mapping of types to converter class names
			Hashtable mapTypeToConverterClassName = new Hashtable();

			for(int i=0;i<types.Length;i++)
				mapTypeToConverterClassName[types[i]] = "ConvertClass_" + i;

			// [[....]] VSUQFE#2248 (VSWhidbey 231885)
			bool bSchemaToBeCompared = IsSchemaToBeCompared();
			bool bNewClassToCompile = false;

			// Mof compilation is dictated by, whether the assembly is registered as instrumented or not
			if (bSchemaToBeCompared == false)
			{
				bNewClassToCompile = !IsAssemblyRegistered();
			}

			for(int i=0;i<types.Length;i++)
			{
				SchemaMapping mapping = new SchemaMapping(types[i], this, mapTypeToConverterClassName);

				codeCCTOR.Line(String.Format("mapTypeToConverter[typeof({0})] = typeof({1});", mapping.ClassType.FullName.Replace('+', '.'), mapping.CodeClassName));  // bug#92918 - watch for nested classes
				// [[....]] VSUQFE#2248 (VSWhidbey 231885)
				if (bSchemaToBeCompared == true && IsClassAlreadyPresentInRepository(mapping.NewClass) == false)
				{
					bNewClassToCompile = true;
				}

				ReplaceClassIfNecessary(mapping.ClassPath, mapping.NewClass);

				mofs[i] = GetMofFormat(mapping.NewClass);
				code.AddChild(mapping.Code);
				switch(mapping.InstrumentationType)
				{
					case InstrumentationType.Event:
						events.Add(mapping.ClassName);
						break;

					case InstrumentationType.Instance:
						instances.Add(mapping.ClassName);
						break;

					case InstrumentationType.Abstract:
						abstracts.Add(mapping.ClassName);
						break;

					default:
						break;
				}
			}

			RegisterAssemblySpecificDecoupledProviderInstance();
			RegisterProviderAsEventProvider(events);
			RegisterProviderAsInstanceProvider();
			RegisterAssemblyAsInstrumented();
			Directory.CreateDirectory(DataDirectory);
			using(StreamWriter log = new StreamWriter(CodePath, false, Encoding.Unicode))
			{
				log.WriteLine(code);
				log.WriteLine(iwoaDef+"new ManagementPath(@\""+this.NamespaceName+"\")"+iwoaDefEnd);
			}

			// Always generate the MOF in unicode
			using(StreamWriter log = new StreamWriter(MofPath, false, Encoding.Unicode))
			{
				log.WriteLine(GenerateMof(mofs));
			}

			// [[....]] VSUQFE#2248 (VSWhidbey 231885)
			// Write the mof to a file and compile it only if there are any new classes , apart from
			// what is there in the repository
			if (bNewClassToCompile == true)
			{
			RegisterSchemaUsingMofcomp ( MofPath ) ;
			//WMICapabilities.AddAutorecoverMof(MofPath);
		}
		}
        public void RegisterAssemblySpecificSchema()
        {
            SecurityHelper.UnmanagedCode.Demand();             // Bug#112640 - Close off any potential use from anything but fully trusted code
            Type[] types = InstrumentedAttribute.GetInstrumentedTypes(assembly);

            StringCollection events    = new StringCollection();
            StringCollection instances = new StringCollection();
            StringCollection abstracts = new StringCollection();

            string[]             mofs       = new string[types.Length];
            CodeWriter           code       = new CodeWriter();
            ReferencesCollection references = new ReferencesCollection();

            // Add the node with all the 'using' statements at the top
            code.AddChild(references.UsingCode);
            references.Add(typeof(Object));
            references.Add(typeof(ManagementClass));
            references.Add(typeof(Marshal));
            references.Add(typeof(System.Security.SuppressUnmanagedCodeSecurityAttribute));
            references.Add(typeof(System.Reflection.FieldInfo));
            references.Add(typeof(Hashtable));

            // Add a blank line
            code.Line();

            // Add master converter class
            CodeWriter codeWMIConverter = code.AddChild("public class WMINET_Converter");

            // Add master map of types to converters
            codeWMIConverter.Line("public static Hashtable mapTypeToConverter = new Hashtable();");

            // Add master CCTOR
            CodeWriter codeCCTOR = codeWMIConverter.AddChild("static WMINET_Converter()");

            // Make mapping of types to converter class names
            Hashtable mapTypeToConverterClassName = new Hashtable();

            for (int i = 0; i < types.Length; i++)
            {
                mapTypeToConverterClassName[types[i]] = "ConvertClass_" + i;
            }

            for (int i = 0; i < types.Length; i++)
            {
                SchemaMapping mapping = new SchemaMapping(types[i], this, mapTypeToConverterClassName);

                codeCCTOR.Line(String.Format("mapTypeToConverter[typeof({0})] = typeof({1});", mapping.ClassType.FullName.Replace('+', '.'), mapping.CodeClassName));  // bug#92918 - watch for nested classes

                ReplaceClassIfNecessary(mapping.ClassPath, mapping.NewClass);
                mofs[i] = GetMofFormat(mapping.NewClass);
                code.AddChild(mapping.Code);
                switch (mapping.InstrumentationType)
                {
                case InstrumentationType.Event:
                    events.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Instance:
                    instances.Add(mapping.ClassName);
                    break;

                case InstrumentationType.Abstract:
                    abstracts.Add(mapping.ClassName);
                    break;

                default:
                    break;
                }
            }

            RegisterAssemblySpecificDecoupledProviderInstance();
            RegisterProviderAsEventProvider(events);
            RegisterProviderAsInstanceProvider();

            RegisterAssemblyAsInstrumented();

            Directory.CreateDirectory(DataDirectory);

            using (StreamWriter log = new StreamWriter(CodePath, false, Encoding.Unicode))
            {
                log.WriteLine(code);
                log.WriteLine(iwoaDef);
            }

            // Always generate the MOF in unicode
            using (StreamWriter log = new StreamWriter(MofPath, false, Encoding.Unicode))
            {
                log.WriteLine(GenerateMof(mofs));
            }
            RegisterSchemaUsingMofcomp(MofPath);
            //WMICapabilities.AddAutorecoverMof(MofPath);
        }