Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / WinForms / Managed / System / WinForms / ComponentModel / COM2Interop / ComNativeDescriptor.cs / 1 / ComNativeDescriptor.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms.ComponentModel.Com2Interop { using System.Runtime.Serialization.Formatters; using System.Runtime.Remoting; using System.Runtime.InteropServices; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.Design; using Microsoft.Win32; ////// /// Top level mapping layer between COM Object and TypeDescriptor. /// /// internal class ComNativeDescriptor : TypeDescriptionProvider { private static ComNativeDescriptor handler = null; private AttributeCollection staticAttrs = new AttributeCollection(new Attribute[]{BrowsableAttribute.Yes, DesignTimeVisibleAttribute.No}); ////// /// Our collection of Object managers (Com2Properties) for native properties /// private WeakHashtable nativeProps = new WeakHashtable(); ////// /// Our collection of browsing handlers, which are stateless and shared across objects. /// private Hashtable extendedBrowsingHandlers = new Hashtable(); ////// /// We increment this every time we look at an Object, at specified /// intervals, we run through the properies list to see if we should /// delete any. /// private int clearCount = 0; private const int CLEAR_INTERVAL = 25; internal static ComNativeDescriptor Instance { get { if (handler == null) { handler = new ComNativeDescriptor(); } return handler; } } [ System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode") ] // called via reflection for AutomationExtender stuff. Don't delete! // public static object GetNativePropertyValue(object component, string propertyName, ref bool succeeded) { return Instance.GetPropertyValue(component, propertyName, ref succeeded); } ////// This method returns a custom type descriptor for the given type / object. /// The objectType parameter is always valid, but the instance parameter may /// be null if no instance was passed to TypeDescriptor. The method should /// return a custom type descriptor for the object. If the method is not /// interested in providing type information for the object it should /// return null. /// /// This method is prototyped as virtual, and by default returns null /// if no parent provider was passed. If a parent provider was passed, /// this method will invoke the parent provider's GetTypeDescriptor /// method. /// public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { return new ComTypeDescriptor(this, instance); } internal string GetClassName(Object component) { string name = null; // does IVsPerPropretyBrowsing supply us a name? if (component is NativeMethods.IVsPerPropertyBrowsing) { int hr = ((NativeMethods.IVsPerPropertyBrowsing)component).GetClassName(ref name); if (NativeMethods.Succeeded(hr) && name != null) { return name; } // otherwise fall through... } UnsafeNativeMethods.ITypeInfo pTypeInfo = Com2TypeInfoProcessor.FindTypeInfo(component, true); if (pTypeInfo == null) { //Debug.Fail("The current component failed to return an ITypeInfo"); return ""; } if (pTypeInfo != null) { string desc = null; try { pTypeInfo.GetDocumentation(NativeMethods.MEMBERID_NIL, ref name, ref desc, null, null); // strip the leading underscores while (name != null && name.Length > 0 && name[0] == '_') { name = name.Substring(1); } return name; } catch { } } return ""; } internal TypeConverter GetConverter(Object component) { return TypeDescriptor.GetConverter(typeof(IComponent)); } internal Object GetEditor(Object component, Type baseEditorType) { return TypeDescriptor.GetEditor(component.GetType(), baseEditorType); } internal string GetName(Object component) { if (!(component is UnsafeNativeMethods.IDispatch)) { return ""; } int dispid = Com2TypeInfoProcessor.GetNameDispId((UnsafeNativeMethods.IDispatch)component); if (dispid != NativeMethods.MEMBERID_NIL) { bool success = false; object value = GetPropertyValue(component, dispid, ref success); if (success && value != null) { return value.ToString(); } } return ""; } internal Object GetPropertyValue(Object component, string propertyName, ref bool succeeded) { if (!(component is UnsafeNativeMethods.IDispatch)) { return null; } UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component; string[] names = new string[]{propertyName}; int[] dispid = new int[1]; dispid[0] = NativeMethods.DISPID_UNKNOWN; Guid g = Guid.Empty; try { int hr = iDispatch.GetIDsOfNames(ref g, names, 1, SafeNativeMethods.GetThreadLCID(), dispid); if (dispid[0] == NativeMethods.DISPID_UNKNOWN || NativeMethods.Failed(hr)) { return null; } } catch { return null; } return GetPropertyValue(component, dispid[0], ref succeeded); } internal Object GetPropertyValue(Object component, int dispid, ref bool succeeded) { if (!(component is UnsafeNativeMethods.IDispatch)) { return null; } Object[] pVarResult = new Object[1]; if (GetPropertyValue(component, dispid, pVarResult) == NativeMethods.S_OK) { succeeded = true; return pVarResult[0]; } else { succeeded = false; return null; } } internal int GetPropertyValue(Object component, int dispid, Object[] retval) { if (!(component is UnsafeNativeMethods.IDispatch)) { return NativeMethods.E_NOINTERFACE; } UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component; try { Guid g = Guid.Empty; NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO(); int hr; try{ hr = iDispatch.Invoke(dispid, ref g, SafeNativeMethods.GetThreadLCID(), NativeMethods.DISPATCH_PROPERTYGET, new NativeMethods.tagDISPPARAMS(), retval, pExcepInfo, null); /*if (hr != NativeMethods.S_OK){ Com2PropertyDescriptor.PrintExceptionInfo(pExcepInfo); } */ if (hr == NativeMethods.DISP_E_EXCEPTION) { hr = pExcepInfo.scode; } } catch (ExternalException ex){ hr = ex.ErrorCode; } return hr; } catch { //Debug.Fail(e.ToString() + " " + component.GetType().GUID.ToString() + " " + component.ToString()); } return NativeMethods.E_FAIL; } ////// /// Checks if the given dispid matches the dispid that the Object would like to specify /// as its identification proeprty (Name, ID, etc). /// internal bool IsNameDispId(Object obj, int dispid) { if (obj == null || !obj.GetType().IsCOMObject) { return false; } return dispid == Com2TypeInfoProcessor.GetNameDispId((UnsafeNativeMethods.IDispatch)obj); } ////// /// Checks all our property manages to see if any have become invalid. /// private void CheckClear(Object component) { // walk the list every so many calls if ((++clearCount % CLEAR_INTERVAL) == 0) { lock(nativeProps) { clearCount = 0; List
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- COM2PropertyPageUITypeConverter.cs
- Model3D.cs
- DispatcherProcessingDisabled.cs
- CompareValidator.cs
- StrokeSerializer.cs
- RijndaelManagedTransform.cs
- Column.cs
- CryptoApi.cs
- NullableFloatAverageAggregationOperator.cs
- PointConverter.cs
- ListBase.cs
- PrtTicket_Base.cs
- WsatServiceCertificate.cs
- RuntimeHandles.cs
- Argument.cs
- ViewGenerator.cs
- PassportAuthenticationModule.cs
- ReadOnlyDictionary.cs
- EndPoint.cs
- Listbox.cs
- ExpressionVisitor.cs
- RegexCompiler.cs
- IPipelineRuntime.cs
- DecryptedHeader.cs
- FocusTracker.cs
- WizardStepBase.cs
- RegexCharClass.cs
- EntityConnectionStringBuilder.cs
- HijriCalendar.cs
- HostingMessageProperty.cs
- ApplicationServiceManager.cs
- BindingExpression.cs
- MetadataArtifactLoaderResource.cs
- ToolboxItemFilterAttribute.cs
- ImmutablePropertyDescriptorGridEntry.cs
- Link.cs
- EntityDataSourceMemberPath.cs
- OleDbParameter.cs
- NamespaceEmitter.cs
- Int16AnimationBase.cs
- MenuItemAutomationPeer.cs
- XPathItem.cs
- PersistenceProviderFactory.cs
- sqlmetadatafactory.cs
- __ConsoleStream.cs
- WrapPanel.cs
- WebBrowsableAttribute.cs
- SQLDecimalStorage.cs
- WebControlsSection.cs
- Calendar.cs
- Selector.cs
- TemplateControlBuildProvider.cs
- SiteMapDesignerDataSourceView.cs
- KeyGesture.cs
- WizardPanelChangingEventArgs.cs
- SqlStream.cs
- NameSpaceExtractor.cs
- Crc32.cs
- ScriptIgnoreAttribute.cs
- KeysConverter.cs
- VirtualPath.cs
- ProcessProtocolHandler.cs
- SqlBooleanizer.cs
- ValidationErrorInfo.cs
- CfgParser.cs
- unsafenativemethodstextservices.cs
- WorkflowNamespace.cs
- ClrProviderManifest.cs
- RSAProtectedConfigurationProvider.cs
- ObjectQueryExecutionPlan.cs
- StylusPointPropertyInfo.cs
- PerfCounters.cs
- TextServicesDisplayAttribute.cs
- SiteMapDataSourceView.cs
- SafeNativeMethods.cs
- KnownAssemblyEntry.cs
- ResourceProperty.cs
- DetailsViewUpdatedEventArgs.cs
- TrailingSpaceComparer.cs
- HealthMonitoringSectionHelper.cs
- UserPersonalizationStateInfo.cs
- WebPartRestoreVerb.cs
- RuntimeHelpers.cs
- ScrollBarRenderer.cs
- DtdParser.cs
- BasePattern.cs
- Pair.cs
- RectangleHotSpot.cs
- NativeCppClassAttribute.cs
- DataGridViewMethods.cs
- OptimalTextSource.cs
- XmlCharType.cs
- HostProtectionPermission.cs
- FreezableDefaultValueFactory.cs
- HttpCacheVaryByContentEncodings.cs
- GeometryDrawing.cs
- SignatureResourceHelper.cs
- FontStyles.cs
- DispatchOperationRuntime.cs
- AsyncOperationManager.cs