Exemplo n.º 1
0
        public static void CompileMofFile(string fileName, string serverAndNamespace, string user,
                                          string authority, string password)
        {
            const string method = "CompileMofFile";

            if (fileName == null)
            {
                throw new NullParameterException(typeof(WmiUtil), method, "fileName");
            }
            if (serverAndNamespace == null)
            {
                throw new NullParameterException(typeof(WmiUtil), method, "serverAndNamespace");
            }

            // The MofCompiler returns a completely useless error if it cannot open the file, so
            // instead of calling CompileFile() read the MOF file into a buffer and call CompileBuffer().

            int length;

            byte[] buffer;
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                length = (int)stream.Length;
                buffer = new byte[length];
                stream.Read(buffer, 0, length);
            }

            // Create a MofCompiler COM object.

            Win32.IMofCompiler mofCompiler = (Win32.IMofCompiler)ComInterop.CreateComInstance(
                new System.Guid(Constants.Win32.IIDs.IID_MofCompiler), typeof(Win32.IMofCompiler));
            Debug.Assert(mofCompiler != null, "mofCompiler != null");

            // Compile the MOF file.

            Win32.WBEM_COMPILE_STATUS_INFO status = new Win32.WBEM_COMPILE_STATUS_INFO();
            try
            {
                mofCompiler.CompileBuffer(length, buffer, serverAndNamespace, user, authority, password,
                                          Win32.WBEM_COMPILER_OPTIONS.None, Win32.WBEM_COMPILER_OPTIONS.None,
                                          Win32.WBEM_COMPILER_OPTIONS.None, ref status);
            }
            finally
            {
                Marshal.ReleaseComObject(mofCompiler);
            }

            // Check the status code and throw an exception if the compilation failed.

            if (status.lPhaseError != 0)
            {
                string facility;
                string message = GetWmiErrorMessage(status.hRes, out facility);

                throw new WmiMofFileCompileException(typeof(WmiUtil), method, fileName,
                                                     serverAndNamespace, status.lPhaseError, status.ObjectNum, status.FirstLine,
                                                     status.LastLine, status.hRes, facility, message);
            }
        }
Exemplo n.º 2
0
        private static string GetWmiErrorMessage(int hResult, out string facility)
        {
            Win32.IWbemStatusCodeText statusCodeText = (Win32.IWbemStatusCodeText)ComInterop.CreateComInstance(
                new System.Guid(Constants.Win32.IIDs.IID_WbemClassObject), typeof(Win32.IWbemStatusCodeText));
            Debug.Assert(statusCodeText != null, "statusCodeText != null");

            try
            {
                facility = statusCodeText.GetFacilityCodeText(hResult, 0, 0);
                return(statusCodeText.GetErrorCodeText(hResult, 0, 0).TrimEnd(new char[] { '\r', '\n' }));
            }
            finally
            {
                Marshal.ReleaseComObject(statusCodeText);
            }
        }