Example #1
0
        /**
         * Create a Dictionary based on the METADATA_RECORD.
         */
        private static Dictionary <DataField, String> ToData(METADATA_RECORD metaDataRecord)
        {
            Dictionary <DataField, String> result = new Dictionary <DataField, String>();

            result.Add(DataField.DATA_ID, metaDataRecord.dwMDIdentifier.ToString());
            switch (metaDataRecord.dwMDUserType)
            {
            case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_SERVER:
                result.Add(DataField.USER_TYPE, "IIS_MD_UT_SERVER");
                break;

            case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_FILE:
                result.Add(DataField.USER_TYPE, "IIS_MD_UT_FILE");
                break;

            case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_WAM:
                result.Add(DataField.USER_TYPE, "IIS_MD_UT_WAM");
                break;

            case (UInt32)METADATA_USER_TYPE.ASP_MD_UT_APP:
                result.Add(DataField.USER_TYPE, "ASP_MD_UT_APP");
                break;
            }
            switch (metaDataRecord.dwMDDataType)
            {
            case (UInt32)METADATA_TYPES.BINARY_METADATA:
                result.Add(DataField.DATA_TYPE, "BINARY_METADATA");
                byte[] binaryData = new byte[metaDataRecord.dwMDDataLen];
                Marshal.Copy(metaDataRecord.pbMDData, binaryData, 0, binaryData.Length);
                result.Add(DataField.VALUE, ByteArrayToString(binaryData));
                break;

            case (UInt32)METADATA_TYPES.STRING_METADATA:
                result.Add(DataField.DATA_TYPE, "STRING_METADATA");
                String stringData = Marshal.PtrToStringUni(metaDataRecord.pbMDData);
                result.Add(DataField.VALUE, stringData);
                break;

            case (UInt32)METADATA_TYPES.DWORD_METADATA:
                result.Add(DataField.DATA_TYPE, "DWORD_METADATA");
                uint dwordData = (uint)Marshal.ReadInt32(metaDataRecord.pbMDData);
                result.Add(DataField.VALUE, dwordData.ToString());
                break;

            case (UInt32)METADATA_TYPES.MULTISZ_METADATA:
                result.Add(DataField.DATA_TYPE, "MULTISZ_METADATA");
                byte[] multiSzData = new byte[metaDataRecord.dwMDDataLen];
                Marshal.Copy(metaDataRecord.pbMDData, multiSzData, 0, multiSzData.Length);
                // Trim Double Nulls
                string strings = Encoding.Unicode.GetString(multiSzData, 0, (int)(metaDataRecord.dwMDDataLen - 4));
                result.Add(DataField.VALUE, strings);
                break;
            }
            return(result);
        }
Example #2
0
        /**
         * Returns an empty list if the path is invalid, or if there are no children.
         */
        public static List <Dictionary <DataField, String> > ListData(String keyPath)
        {
            List <Dictionary <DataField, String> > data = new List <Dictionary <DataField, String> >();
            UInt32 index = 0;
            bool   done  = false;

            while (!done)
            {
                METADATA_RECORD record = new METADATA_RECORD();
                uint            len    = 0;
                try {
                    baseInterface.EnumData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, index, out len);
                } catch (COMException e) {
                    switch ((UInt32)e.ErrorCode)
                    {
                    case 0x8007007A: {   // HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
                        bool retrieved = false;
                        while (!retrieved)
                        {
                            try {
                                if (len == 0)
                                {
                                    len = 1024;
                                }
                                else
                                {
                                    len = 2 * len;
                                }
                                record.pbMDData = Marshal.AllocCoTaskMem((int)len);
                                if (record.pbMDData == IntPtr.Zero)
                                {
                                    throw new ExternalException("Unable to allocate memory for Metabase data buffer.");
                                }
                                record.dwMDDataLen = (UInt32)len;
                                baseInterface.EnumData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, index, out len);
                                retrieved = true;
                            } catch (COMException e2) {
                                if ((UInt32)e2.ErrorCode == 0x8007007A)
                                {
                                    Marshal.FreeCoTaskMem(record.pbMDData);
                                }
                                else
                                {
                                    throw e2;
                                }
                            }
                        }
                        data.Add(ToData(record));
                        break;
                    }

                    case 0x80070103:   // HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)
                        done = true;
                        break;

                    default:
                        throw e;
                    }
                } catch (DirectoryNotFoundException) {
                    // HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)
                    done = true;
                } finally {
                    if (record.pbMDData != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(record.pbMDData);
                    }
                }
                index++;
            }
            return(data);
        }
Example #3
0
        /**
         * Returns null if there is no data for the ID.
         */
        public static Dictionary <DataField, String> GetData(String keyPath, UInt32 dataId)
        {
            METADATA_RECORD record = new METADATA_RECORD();

            record.dwMDIdentifier = dataId;
            record.dwMDAttributes = (UInt32)METADATA_ATTRIBUTES.METADATA_INHERIT;
            record.dwMDUserType   = (UInt32)METADATA_USER_TYPE.IIS_MD_UT_SERVER;
            record.dwMDDataType   = (UInt32)METADATA_TYPES.ALL_METADATA;
            try {
                uint len = 0;
                try {
                    baseInterface.GetData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, out len);
                } catch (COMException e) {
                    switch ((UInt32)e.ErrorCode)
                    {
                    case 0x8007007A:   // HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
                        bool retrieved = false;
                        while (!retrieved)
                        {
                            try {
                                if (len == 0)
                                {
                                    len = 1024;
                                }
                                else
                                {
                                    len = 2 * len;
                                }
                                record.pbMDData = Marshal.AllocCoTaskMem((int)len);
                                if (record.pbMDData == IntPtr.Zero)
                                {
                                    throw new ExternalException("Unable to allocate memory for Metabase data buffer.");
                                }
                                record.dwMDDataLen = (UInt32)len;
                                baseInterface.GetData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, out len);
                                retrieved = true;
                            } catch (COMException e2) {
                                if ((UInt32)e2.ErrorCode == 0x8007007A)
                                {
                                    Marshal.FreeCoTaskMem(record.pbMDData);
                                }
                                else
                                {
                                    throw e2;
                                }
                            }
                        }
                        return(ToData(record));

                    case 0x800CC801:   // MD_ERROR_DATA_NOT_FOUND
                        break;

                    default:
                        throw e;
                    }
                } catch (DirectoryNotFoundException) {
                    // HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)
                }
                return(null);
            } finally {
                if (record.pbMDData != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(record.pbMDData);
                }
            }
        }
Example #4
0
 /**
  * Create a Dictionary based on the METADATA_RECORD.
  */
 private static Dictionary<DataField, String> ToData(METADATA_RECORD metaDataRecord)
 {
     Dictionary<DataField, String> result = new Dictionary<DataField, String>();
     result.Add(DataField.DATA_ID, metaDataRecord.dwMDIdentifier.ToString());
     switch(metaDataRecord.dwMDUserType) {
       case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_SERVER:
     result.Add(DataField.USER_TYPE, "IIS_MD_UT_SERVER");
     break;
       case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_FILE:
     result.Add(DataField.USER_TYPE, "IIS_MD_UT_FILE");
     break;
       case (UInt32)METADATA_USER_TYPE.IIS_MD_UT_WAM:
     result.Add(DataField.USER_TYPE, "IIS_MD_UT_WAM");
     break;
       case (UInt32)METADATA_USER_TYPE.ASP_MD_UT_APP:
     result.Add(DataField.USER_TYPE, "ASP_MD_UT_APP");
     break;
     }
     switch(metaDataRecord.dwMDDataType) {
       case (UInt32)METADATA_TYPES.BINARY_METADATA:
     result.Add(DataField.DATA_TYPE, "BINARY_METADATA");
     byte[] binaryData = new byte[metaDataRecord.dwMDDataLen];
     Marshal.Copy(metaDataRecord.pbMDData, binaryData, 0, binaryData.Length);
     result.Add(DataField.VALUE, ByteArrayToString(binaryData));
     break;
       case (UInt32)METADATA_TYPES.STRING_METADATA:
     result.Add(DataField.DATA_TYPE, "STRING_METADATA");
     String stringData = Marshal.PtrToStringUni(metaDataRecord.pbMDData);
     result.Add(DataField.VALUE, stringData);
     break;
       case (UInt32)METADATA_TYPES.DWORD_METADATA:
     result.Add(DataField.DATA_TYPE, "DWORD_METADATA");
     uint dwordData = (uint)Marshal.ReadInt32(metaDataRecord.pbMDData);
     result.Add(DataField.VALUE, dwordData.ToString());
     break;
       case (UInt32)METADATA_TYPES.MULTISZ_METADATA:
     result.Add(DataField.DATA_TYPE, "MULTISZ_METADATA");
     byte[] multiSzData = new byte[metaDataRecord.dwMDDataLen];
     Marshal.Copy(metaDataRecord.pbMDData, multiSzData, 0, multiSzData.Length);
     // Trim Double Nulls
     string strings = Encoding.Unicode.GetString(multiSzData, 0, (int)(metaDataRecord.dwMDDataLen - 4));
     result.Add(DataField.VALUE, strings);
     break;
     }
     return result;
 }
Example #5
0
 /**
  * Returns an empty list if the path is invalid, or if there are no children.
  */
 public static List<Dictionary<DataField, String>> ListData(String keyPath)
 {
     List<Dictionary<DataField, String>> data = new List<Dictionary<DataField, String>>();
     UInt32 index = 0;
     bool done = false;
     while(!done) {
     METADATA_RECORD record = new METADATA_RECORD();
     uint len = 0;
     try {
     baseInterface.EnumData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, index, out len);
     } catch (COMException e) {
     switch((UInt32)e.ErrorCode) {
       case 0x8007007A: { // HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
     bool retrieved = false;
     while (!retrieved) {
         try {
         if (len == 0) {
             len = 1024;
         } else {
             len = 2 * len;
         }
         record.pbMDData = Marshal.AllocCoTaskMem((int)len);
         if (record.pbMDData == IntPtr.Zero) {
             throw new ExternalException("Unable to allocate memory for Metabase data buffer.");
         }
         record.dwMDDataLen = (UInt32)len;
         baseInterface.EnumData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, index, out len);
         retrieved = true;
         } catch (COMException e2) {
         if ((UInt32)e2.ErrorCode == 0x8007007A) {
             Marshal.FreeCoTaskMem(record.pbMDData);
         } else {
             throw e2;
         }
         }
     }
     data.Add(ToData(record));
     break;
       }
       case 0x80070103: // HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)
     done = true;
     break;
       default:
     throw e;
     }
     } catch (DirectoryNotFoundException) {
     // HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)
     done = true;
     } finally {
     if (record.pbMDData != IntPtr.Zero) {
     Marshal.FreeCoTaskMem(record.pbMDData);
     }
     }
     index++;
     }
     return data;
 }
Example #6
0
 /**
  * Returns null if there is no data for the ID.
  */
 public static Dictionary<DataField, String> GetData(String keyPath, UInt32 dataId)
 {
     METADATA_RECORD record = new METADATA_RECORD();
     record.dwMDIdentifier = dataId;
     record.dwMDAttributes = (UInt32)METADATA_ATTRIBUTES.METADATA_INHERIT;
     record.dwMDUserType = (UInt32)METADATA_USER_TYPE.IIS_MD_UT_SERVER;
     record.dwMDDataType = (UInt32)METADATA_TYPES.ALL_METADATA;
     try {
     uint len = 0;
     try {
     baseInterface.GetData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, out len);
     } catch (COMException e) {
     switch((UInt32)e.ErrorCode) {
       case 0x8007007A: // HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
     bool retrieved = false;
     while (!retrieved) {
         try {
         if (len == 0) {
             len = 1024;
         } else {
             len = 2 * len;
         }
         record.pbMDData = Marshal.AllocCoTaskMem((int)len);
         if (record.pbMDData == IntPtr.Zero) {
             throw new ExternalException("Unable to allocate memory for Metabase data buffer.");
         }
         record.dwMDDataLen = (UInt32)len;
         baseInterface.GetData(METADATA_MASTER_ROOT_HANDLE, keyPath, ref record, out len);
         retrieved = true;
         } catch (COMException e2) {
         if ((UInt32)e2.ErrorCode == 0x8007007A) {
             Marshal.FreeCoTaskMem(record.pbMDData);
         } else {
             throw e2;
         }
         }
     }
     return ToData(record);
       case 0x800CC801: // MD_ERROR_DATA_NOT_FOUND
     break;
       default:
     throw e;
     }
     } catch (DirectoryNotFoundException) {
     // HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)
     }
     return null;
     } finally {
     if (record.pbMDData != IntPtr.Zero) {
     Marshal.FreeCoTaskMem(record.pbMDData);
     }
     }
 }