Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / TransactionBridge / Microsoft / Transactions / Wsat / Clusters / ClusterRegistryConfigurationProvider.cs / 1 / ClusterRegistryConfigurationProvider.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
// Implement the ConfigurationProvider base class for the cluster registry
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
using System.ServiceModel.Diagnostics;
using Microsoft.Transactions.Bridge;
using Microsoft.Transactions.Wsat.Messaging;
using Microsoft.Transactions.Wsat.Protocol;
using Microsoft.Win32;
namespace Microsoft.Transactions.Wsat.Clusters
{
class ClusterRegistryConfigurationProvider : ConfigurationProvider
{
SafeHKey hKey;
public ClusterRegistryConfigurationProvider(SafeHResource hResource)
{
this.hKey = SafeNativeMethods.GetClusterResourceKey(hResource,
RegistryRights.ReadKey |
RegistryRights.EnumerateSubKeys |
RegistryRights.QueryValues);
if (this.hKey.IsInvalid)
{
int gle = Marshal.GetLastWin32Error();
this.hKey.SetHandleAsInvalid();
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ConfigurationProviderException(SR.GetString(SR.GetClusterResourceKeyFailed, gle)));
}
if (DebugTrace.Verbose)
DebugTrace.Trace(TraceLevel.Verbose, "Opened cluster resource key");
}
ClusterRegistryConfigurationProvider(SafeHKey rootKey, string subKey)
{
// If the rootKey is null or invalid, we just nod and return default values
if (rootKey != null && !rootKey.IsInvalid)
{
int ret = SafeNativeMethods.ClusterRegOpenKey(rootKey,
subKey,
RegistryRights.ReadKey |
RegistryRights.EnumerateSubKeys |
RegistryRights.QueryValues,
out this.hKey);
if (ret != SafeNativeMethods.ERROR_SUCCESS)
{
Utility.CloseInvalidOutSafeHandle(this.hKey);
if (ret != SafeNativeMethods.ERROR_FILE_NOT_FOUND)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ConfigurationProviderException(SR.GetString(SR.ClusterRegOpenKeyFailed, ret)));
}
}
if (DebugTrace.Verbose)
DebugTrace.Trace(TraceLevel.Verbose,
"ClusterRegOpenKey for {0} returned {1}",
subKey, ret);
}
}
public override void Dispose()
{
if (this.hKey != null && !this.hKey.IsInvalid)
this.hKey.Dispose();
}
byte[] QueryValue(string value, RegistryValueKind valueType)
{
if (this.hKey == null || this.hKey.IsInvalid)
return null;
RegistryValueKind type;
uint cb = 0;
int ret = SafeNativeMethods.ClusterRegQueryValue(this.hKey,
value,
out type,
null,
ref cb);
if (ret == SafeNativeMethods.ERROR_SUCCESS ||
ret == SafeNativeMethods.ERROR_MORE_DATA)
{
if (valueType != type)
return null;
byte[] buffer = new byte[cb];
ret = SafeNativeMethods.ClusterRegQueryValue(this.hKey,
value,
out type,
buffer,
ref cb);
if (ret == SafeNativeMethods.ERROR_SUCCESS)
{
if (valueType != type)
return null;
return buffer;
}
}
if (DebugTrace.Verbose)
DebugTrace.Trace(TraceLevel.Verbose,
"ClusterRegQueryValue for {0} returned {1}",
value, ret);
if (ret == SafeNativeMethods.ERROR_FILE_NOT_FOUND)
return null;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ConfigurationProviderException(SR.GetString(SR.ClusterRegQueryValueFailed, ret)));
}
public override int ReadInteger(string value, int defaultValue)
{
byte[] buffer = QueryValue(value, RegistryValueKind.DWord);
if (buffer == null)
return defaultValue;
return (int)BitConverter.ToUInt32(buffer, 0);
}
public override string ReadString(string value, string defaultValue)
{
byte[] buffer = QueryValue(value, RegistryValueKind.String);
if (buffer == null)
return defaultValue;
try
{
return Encoding.Unicode.GetString(buffer, 0, buffer.Length - 2);
}
catch (ArgumentException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ConfigurationProviderException(SR.GetString(SR.ClusterRegQueryValueInvalidResults, value), e));
}
}
public override string[] ReadMultiString(string value, string[] defaultValue)
{
byte[] buffer = QueryValue(value, RegistryValueKind.MultiString);
if (buffer == null)
return defaultValue;
List list = new List(5);
string item;
int index = 0;
while ((item = GetStringFromMultiSz(value, buffer, ref index)) != null)
{
list.Add(item);
}
return list.ToArray();
}
public override ConfigurationProvider OpenKey(string key)
{
return new ClusterRegistryConfigurationProvider(this.hKey, key);
}
string GetStringFromMultiSz(string value, byte[] buffer, ref int index)
{
// E.g. abc\0def\0\0
// This loop terminates when it finds a null terminating character
// The index will be left pointing at the first null terminator it finds
int start = index;
for (;
index < buffer.Length - 1 && BitConverter.ToChar(buffer, index) != 0;
index += 2) ;
// If we found no valid characters, we're done
if (start == index)
return null;
// Skip past the null terminator at which we stopped
index += 2;
// Convert the characters we found to a string
try
{
return Encoding.Unicode.GetString(buffer, start, index - start - 2);
}
catch (ArgumentException e)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ConfigurationProviderException(SR.GetString(SR.ClusterRegQueryValueInvalidResults, value), e));
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AutomationPeer.cs
- CompressedStack.cs
- Accessible.cs
- ConfigXmlAttribute.cs
- BinHexEncoding.cs
- ByteRangeDownloader.cs
- controlskin.cs
- PartialTrustHelpers.cs
- Regex.cs
- DetailsViewPagerRow.cs
- Semaphore.cs
- Matrix3D.cs
- connectionpool.cs
- SmiMetaData.cs
- SelectedDatesCollection.cs
- ContourSegment.cs
- ObjectSecurity.cs
- TargetFrameworkUtil.cs
- PreviewKeyDownEventArgs.cs
- DataServiceQueryOfT.cs
- TextTreeFixupNode.cs
- StreamSecurityUpgradeAcceptorBase.cs
- UnmanagedHandle.cs
- CommandField.cs
- DesignTimeData.cs
- ContainsSearchOperator.cs
- Grant.cs
- ObjectQueryProvider.cs
- ContainerAction.cs
- InkPresenterAutomationPeer.cs
- DataGridTextColumn.cs
- XPathNavigatorKeyComparer.cs
- EntityDataSourceReferenceGroup.cs
- ContentTypeSettingDispatchMessageFormatter.cs
- TypeConverterAttribute.cs
- MultiBindingExpression.cs
- Literal.cs
- EditorBrowsableAttribute.cs
- ClockController.cs
- PTManager.cs
- PointAnimationUsingPath.cs
- RelationshipDetailsRow.cs
- X509AsymmetricSecurityKey.cs
- EventSinkHelperWriter.cs
- RegistrySecurity.cs
- ComponentDispatcherThread.cs
- SaveFileDialog.cs
- AdCreatedEventArgs.cs
- WindowsFont.cs
- SizeConverter.cs
- HyperLink.cs
- ComponentResourceKey.cs
- TemplateXamlParser.cs
- _NegoStream.cs
- ECDiffieHellmanPublicKey.cs
- SchemaTypeEmitter.cs
- ObjectHelper.cs
- InputLanguageSource.cs
- Size3DValueSerializer.cs
- DesignParameter.cs
- TreeNodeSelectionProcessor.cs
- ScriptBehaviorDescriptor.cs
- ClientBuildManager.cs
- BasicExpressionVisitor.cs
- ECDiffieHellmanCng.cs
- GenericPrincipal.cs
- SystemMulticastIPAddressInformation.cs
- WindowsGraphicsCacheManager.cs
- ModulesEntry.cs
- PartialCachingControl.cs
- SecurityProtocolFactory.cs
- UrlAuthFailedErrorFormatter.cs
- QilList.cs
- CodeDirectoryCompiler.cs
- XslCompiledTransform.cs
- TextViewBase.cs
- _AuthenticationState.cs
- XsdDateTime.cs
- OracleFactory.cs
- DynamicQueryableWrapper.cs
- TypedTableGenerator.cs
- UriExt.cs
- NamedElement.cs
- UndoEngine.cs
- ClientScriptManager.cs
- SqlGenerator.cs
- SuppressMessageAttribute.cs
- BigInt.cs
- CodeDOMUtility.cs
- PropertyMapper.cs
- StringUtil.cs
- hresults.cs
- ConfigurationValue.cs
- Reference.cs
- ValidateNames.cs
- DbConnectionStringCommon.cs
- IntegerFacetDescriptionElement.cs
- SingleTagSectionHandler.cs
- XmlIlGenerator.cs
- EditorZoneBase.cs