bool UnsafeNativeMethods.IMsoComponent.FDebugMessage(IntPtr hInst, int msg, IntPtr wparam, IntPtr lparam) { UnsafeNativeMethods.IMsoComponent c = Component; if (c != null) { return(c.FDebugMessage(hInst, msg, wparam, lparam)); } return(false); }
IntPtr UnsafeNativeMethods.IMsoComponent.HwndGetWindow(int dwWhich, int dwReserved) { UnsafeNativeMethods.IMsoComponent c = Component; if (c != null) { return(c.HwndGetWindow(dwWhich, dwReserved)); } return(IntPtr.Zero); }
bool UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(ref NativeMethods.MSG msg) { UnsafeNativeMethods.IMsoComponent c = Component; if (c != null) { return(c.FPreTranslateMessage(ref msg)); } return(false); }
bool UnsafeNativeMethods.IMsoComponentManager.FRegisterComponent(UnsafeNativeMethods.IMsoComponent component, NativeMethods.MSOCRINFOSTRUCT pcrinfo, out IntPtr dwComponentID) { if (component == null) { throw new ArgumentNullException(nameof(component)); } dwComponentID = (IntPtr)0; if (_refCount == 0) { // Our first time hooking up to the real component manager if (!_original.FRegisterComponent(this, pcrinfo, out _componentId)) { return(false); } } _refCount++; if (_components == null) { _components = new Dictionary <int, UnsafeNativeMethods.IMsoComponent>(); } _nextComponentId++; if (_nextComponentId == int.MaxValue) { _nextComponentId = 1; } bool outofMemory = false; //just in case we wrap, lets search for a free ID while (_components.ContainsKey(_nextComponentId)) { _nextComponentId++; if (_nextComponentId == int.MaxValue) { if (outofMemory) { throw new InvalidOperationException(SR.ComponentManagerProxyOutOfMemory); } outofMemory = true; _nextComponentId = 1; } } _components.Add(_nextComponentId, component); dwComponentID = (IntPtr)_nextComponentId; return(true); }
void UnsafeNativeMethods.IMsoComponent.Terminate() { if ((this._components != null) && (this._components.Values.Count > 0)) { UnsafeNativeMethods.IMsoComponent[] array = new UnsafeNativeMethods.IMsoComponent[this._components.Values.Count]; this._components.Values.CopyTo(array, 0); foreach (UnsafeNativeMethods.IMsoComponent component in array) { component.Terminate(); } } if (this._original != null) { this.RevokeComponent(); } this.Dispose(); }
bool UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent(IntPtr dwComponentID) { int dwLocalComponentID = unchecked ((int)(long)dwComponentID); if (_original == null) { return(false); } if (_components == null || dwLocalComponentID <= 0 || !_components.ContainsKey(dwLocalComponentID)) { return(false); } if (_refCount == 1 && SafeNativeMethods.GetCurrentThreadId() == _creationThread) { if (!RevokeComponent()) { return(false); } } _refCount--; _components.Remove(dwLocalComponentID); Debug.Assert(_refCount >= 0, "underflow on ref count"); if (_refCount <= 0) { Dispose(); } if (dwLocalComponentID == _activeComponentId) { _activeComponent = null; _activeComponentId = 0; } if (dwLocalComponentID == _trackingComponentId) { _trackingComponent = null; _trackingComponentId = 0; } return(true); }
void UnsafeNativeMethods.IMsoComponent.Terminate() { if (_components != null && _components.Values.Count > 0) { UnsafeNativeMethods.IMsoComponent[] components = new UnsafeNativeMethods.IMsoComponent[_components.Values.Count]; _components.Values.CopyTo(components, 0); foreach (UnsafeNativeMethods.IMsoComponent c in components) { c.Terminate(); } } if (_original != null) { RevokeComponent(); } Dispose(); }
bool UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate(IntPtr dwComponentID) { int key = (int)((long)dwComponentID); if (this._original == null) { return(false); } if (((this._components == null) || (key <= 0)) || !this._components.ContainsKey(key)) { return(false); } if (!this._original.FOnComponentActivate(this._componentId)) { return(false); } this._activeComponent = this._components[key]; this._activeComponentId = key; return(true); }
bool UnsafeNativeMethods.IMsoComponentManager.FRegisterComponent(UnsafeNativeMethods.IMsoComponent component, System.Windows.Forms.NativeMethods.MSOCRINFOSTRUCT pcrinfo, out IntPtr dwComponentID) { if (component == null) { throw new ArgumentNullException("component"); } dwComponentID = IntPtr.Zero; if ((this._refCount == 0) && !this._original.FRegisterComponent(this, pcrinfo, out this._componentId)) { return(false); } this._refCount++; if (this._components == null) { this._components = new Dictionary <int, UnsafeNativeMethods.IMsoComponent>(); } this._nextComponentId++; if (this._nextComponentId == 0x7fffffff) { this._nextComponentId = 1; } bool flag = false; while (this._components.ContainsKey(this._nextComponentId)) { this._nextComponentId++; if (this._nextComponentId == 0x7fffffff) { if (flag) { throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ComponentManagerProxyOutOfMemory")); } flag = true; this._nextComponentId = 1; } } this._components.Add(this._nextComponentId, component); dwComponentID = (IntPtr)this._nextComponentId; return(true); }
bool UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate(IntPtr dwComponentID) { int dwLocalComponentID = unchecked ((int)(long)dwComponentID); if (_original == null) { return(false); } // Activation requres us to store the currently active component. We will send data to it if (_components == null || dwLocalComponentID <= 0 || !_components.ContainsKey(dwLocalComponentID)) { return(false); } if (!_original.FOnComponentActivate(_componentId)) { return(false); } _activeComponent = _components[dwLocalComponentID]; _activeComponentId = dwLocalComponentID; return(true); }
void UnsafeNativeMethods.IMsoComponent.Terminate() { if (_components != null && _components.Values.Count>0) { UnsafeNativeMethods.IMsoComponent[] components = new UnsafeNativeMethods.IMsoComponent[_components.Values.Count]; _components.Values.CopyTo(components, 0); foreach (UnsafeNativeMethods.IMsoComponent c in components) { c.Terminate(); } } if (_original != null) { RevokeComponent(); } Dispose(); }
bool UnsafeNativeMethods.IMsoComponentManager.FSetTrackingComponent(IntPtr dwComponentID, bool fTrack) { int key = (int) ((long) dwComponentID); if (this._original == null) { return false; } if (((this._components == null) || (key <= 0)) || !this._components.ContainsKey(key)) { return false; } if (!this._original.FSetTrackingComponent(this._componentId, fTrack)) { return false; } if (fTrack) { this._trackingComponent = this._components[key]; this._trackingComponentId = key; } else { this._trackingComponent = null; this._trackingComponentId = 0; } return true; }
bool UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent(IntPtr dwComponentID) { int key = (int) ((long) dwComponentID); if (this._original == null) { return false; } if (((this._components == null) || (key <= 0)) || !this._components.ContainsKey(key)) { return false; } if (((this._refCount == 1) && (SafeNativeMethods.GetCurrentThreadId() == this._creationThread)) && !this.RevokeComponent()) { return false; } this._refCount--; this._components.Remove(key); if (this._refCount <= 0) { this.Dispose(); } if (key == this._activeComponentId) { this._activeComponent = null; this._activeComponentId = 0; } if (key == this._trackingComponentId) { this._trackingComponent = null; this._trackingComponentId = 0; } return true; }
bool UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate(IntPtr dwComponentID) { int key = (int) ((long) dwComponentID); if (this._original == null) { return false; } if (((this._components == null) || (key <= 0)) || !this._components.ContainsKey(key)) { return false; } if (!this._original.FOnComponentActivate(this._componentId)) { return false; } this._activeComponent = this._components[key]; this._activeComponentId = key; return true; }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ComponentManager.UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate"]/*' /> /// <devdoc> /// Notify component manager that component identified by dwComponentID /// (cookie returned from FRegisterComponent) has been activated. /// The active component gets the chance to process messages before they /// are dispatched (via IMsoComponent::FPreTranslateMessage) and typically /// gets first chance at idle time after the host. /// This method fails if another component is already Exclusively Active. /// In this case, FALSE is returned and SetLastError is set to /// msoerrACompIsXActive (comp usually need not take any special action /// in this case). /// Return TRUE if successful. /// </devdoc> bool UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate(IntPtr dwComponentID) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager: Component activated. ID: " + dwLocalComponentID.ToString(CultureInfo.InvariantCulture)); ComponentHashtableEntry entry = (ComponentHashtableEntry)OleComponents[dwLocalComponentID]; if (entry == null) { Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "*** Component not registered ***"); return false; } Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "New active component is : " + entry.component.ToString()); activeComponent = entry.component; return true; }
bool UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent(IntPtr dwComponentID) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); if (_original == null) return false; if (_components == null || dwLocalComponentID <= 0 || !_components.ContainsKey(dwLocalComponentID)) { return false; } if (_refCount == 1 && SafeNativeMethods.GetCurrentThreadId() == _creationThread) { if (!RevokeComponent()) { return false; } } _refCount--; _components.Remove(dwLocalComponentID); Debug.Assert(_refCount >= 0, "underflow on ref count"); if (_refCount <= 0) { Dispose(); } if (dwLocalComponentID == _activeComponentId) { _activeComponent = null; _activeComponentId = 0; } if (dwLocalComponentID == _trackingComponentId) { _trackingComponent = null; _trackingComponentId = 0; } return true; }
bool UnsafeNativeMethods.IMsoComponentManager.FSetTrackingComponent(IntPtr dwComponentID, bool fTrack) { // Tracking requres us to store the current tracking component. We will send data to it int dwLocalComponentID = unchecked((int)(long)dwComponentID); if (_original == null) { return false; } if (_components == null || dwLocalComponentID <= 0 || !_components.ContainsKey(dwLocalComponentID)) { return false; } if (!_original.FSetTrackingComponent(_componentId, fTrack)) { return false; } if (fTrack) { _trackingComponent = _components[dwLocalComponentID]; _trackingComponentId = dwLocalComponentID; } else { _trackingComponent = null; _trackingComponentId = 0; } return true; }
bool UnsafeNativeMethods.IMsoComponentManager.FOnComponentActivate(IntPtr dwComponentID) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); if (_original == null) return false; // Activation requres us to store the currently active component. We will send data to it if (_components == null || dwLocalComponentID <= 0 || !_components.ContainsKey(dwLocalComponentID)) { return false; } if (!_original.FOnComponentActivate(_componentId)) { return false; } _activeComponent = _components[dwLocalComponentID]; _activeComponentId = dwLocalComponentID; return true; }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ThreadContext.GetMessageLoop"]/*' /> /// <devdoc> /// Determines if a message loop exists on this thread. /// </devdoc> internal bool GetMessageLoop(bool mustBeActive) { // If we are already running a loop, we're fine. // If we are running in external manager we may need to make sure first the loop is active // if (messageLoopCount > (mustBeActive && externalComponentManager ? 1 : 0)) { return true; } // Also, access the ComponentManager property to demand create it, and we're also // fine if it is an external manager, because it has already pushed a loop. // if (ComponentManager != null && externalComponentManager) { if (mustBeActive == false) { return true; } UnsafeNativeMethods.IMsoComponent[] activeComponents = new UnsafeNativeMethods.IMsoComponent[1]; if (ComponentManager.FGetActiveComponent(NativeMethods.MSOCM.msogacActive, activeComponents, null, 0) && activeComponents[0] == this) { return true; } } // Finally, check if a message loop has been registered MessageLoopCallback callback = messageLoopCallback; if (callback != null) { return callback(); } // Otherwise, we do not have a loop running. // return false; }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ComponentManager.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop"]/*' /> /// <devdoc> /// Component identified by dwComponentID (cookie returned from /// FRegisterComponent) wishes to push a message loop for reason uReason. /// uReason is one the values from the msoloop enumeration (above). /// pvLoopData is data private to the component. /// The component manager should push its message loop, /// calling IMsoComponent::FContinueMessageLoop(uReason, pvLoopData) /// during each loop iteration (see IMsoComponent::FContinueMessageLoop /// comments). When IMsoComponent::FContinueMessageLoop returns FALSE, the /// component manager terminates the loop. /// Returns TRUE if component manager terminates loop because component /// told it to (by returning FALSE from IMsoComponent::FContinueMessageLoop), /// FALSE if it had to terminate the loop for some other reason. In the /// latter case, component should perform any necessary action (such as /// cleanup). /// </devdoc> bool UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop( IntPtr dwComponentID, int reason, int pvLoopData // PVOID ) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); // Hold onto old state to allow restore before we exit... // int currentLoopState = currentState; bool continueLoop = true; if (!OleComponents.ContainsKey(dwLocalComponentID)) { return false; } UnsafeNativeMethods.IMsoComponent prevActive = this.activeComponent; try { // Execute the message loop until the active component tells us to stop. // NativeMethods.MSG msg = new NativeMethods.MSG(); NativeMethods.MSG[] rgmsg = new NativeMethods.MSG[] {msg}; bool unicodeWindow = false; UnsafeNativeMethods.IMsoComponent requestingComponent; ComponentHashtableEntry entry = (ComponentHashtableEntry)OleComponents[dwLocalComponentID]; if (entry == null) { return false; } requestingComponent = entry.component; this.activeComponent = requestingComponent; Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Pushing message loop " + reason.ToString(CultureInfo.InvariantCulture)); Debug.Indent(); while (continueLoop) { // Determine the component to route the message to // UnsafeNativeMethods.IMsoComponent component; if (trackingComponent != null) { component = trackingComponent; } else if (activeComponent != null) { component = activeComponent; } else { component = requestingComponent; } bool peeked = UnsafeNativeMethods.PeekMessage(ref msg, NativeMethods.NullHandleRef, 0, 0, NativeMethods.PM_NOREMOVE); if (peeked) { rgmsg[0] = msg; continueLoop = component.FContinueMessageLoop(reason, pvLoopData, rgmsg); // If the component wants us to process the message, do it. // The component manager hosts windows from many places. We must be sensitive // to ansi / Unicode windows here. // if (continueLoop) { if (msg.hwnd != IntPtr.Zero && SafeNativeMethods.IsWindowUnicode(new HandleRef(null, msg.hwnd))) { unicodeWindow = true; UnsafeNativeMethods.GetMessageW(ref msg, NativeMethods.NullHandleRef, 0, 0); } else { unicodeWindow = false; UnsafeNativeMethods.GetMessageA(ref msg, NativeMethods.NullHandleRef, 0, 0); } if (msg.message == NativeMethods.WM_QUIT) { Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Normal message loop termination"); Application.ThreadContext.FromCurrent().DisposeThreadWindows(); if (reason != NativeMethods.MSOCM.msoloopMain) { UnsafeNativeMethods.PostQuitMessage((int)msg.wParam); } continueLoop = false; break; } // Now translate and dispatch the message. // // Reading through the rather sparse documentation, // it seems we should only call FPreTranslateMessage // on the active component. But frankly, I'm afraid of what that might break. // See ASURT 29415 for more background. if (!component.FPreTranslateMessage(ref msg)) { UnsafeNativeMethods.TranslateMessage(ref msg); if (unicodeWindow) { UnsafeNativeMethods.DispatchMessageW(ref msg); } else { UnsafeNativeMethods.DispatchMessageA(ref msg); } } } } else { // If this is a DoEvents loop, then get out. There's nothing left // for us to do. // if (reason == NativeMethods.MSOCM.msoloopDoEvents || reason == NativeMethods.MSOCM.msoloopDoEventsModal) { break; } // Nothing is on the message queue. Perform idle processing // and then do a WaitMessage. // bool continueIdle = false; if (OleComponents != null) { IEnumerator enumerator = OleComponents.Values.GetEnumerator(); while (enumerator.MoveNext()) { ComponentHashtableEntry idleEntry = (ComponentHashtableEntry)enumerator.Current; continueIdle |= idleEntry.component.FDoIdle(-1); } } // give the component one more chance to terminate the // message loop. // continueLoop = component.FContinueMessageLoop(reason, pvLoopData, null); if (continueLoop) { if (continueIdle) { // If someone has asked for idle time, give it to them. However, // don't cycle immediately; wait up to 100ms. Why? Because we don't // want someone to attach to idle, forget to detach, and then ---- // the CPU. For Windows Forms this generally isn't an issue because // our component always returns false from its idle request UnsafeNativeMethods.MsgWaitForMultipleObjectsEx(0, IntPtr.Zero, 100, NativeMethods.QS_ALLINPUT, NativeMethods.MWMO_INPUTAVAILABLE); } else { // We should call GetMessage here, but we cannot because // the component manager requires that we notify the // active component before we pull the message off the // queue. This is a bit of a problem, because WaitMessage // waits for a NEW message to appear on the queue. If a // message appeared between processing and now WaitMessage // would wait for the next message. We minimize this here // by calling PeekMessage. // if (!UnsafeNativeMethods.PeekMessage(ref msg, NativeMethods.NullHandleRef, 0, 0, NativeMethods.PM_NOREMOVE)) { UnsafeNativeMethods.WaitMessage(); } } } } } Debug.Unindent(); Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : message loop " + reason.ToString(CultureInfo.InvariantCulture) + " complete."); } finally { currentState = currentLoopState; this.activeComponent = prevActive; } return !continueLoop; }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ComponentManager.UnsafeNativeMethods.IMsoComponentManager.FSetTrackingComponent"]/*' /> /// <devdoc> /// Called to inform component manager that component identified by /// dwComponentID (cookie returned from FRegisterComponent) wishes /// to perform a tracking operation (such as mouse tracking). /// The component calls this method with fTrack == TRUE to begin the /// tracking operation and with fTrack == FALSE to end the operation. /// During the tracking operation the component manager routes messages /// to the tracking component (via IMsoComponent::FPreTranslateMessage) /// rather than to the active component. When the tracking operation ends, /// the component manager should resume routing messages to the active /// component. /// Note: component manager should perform no idle time processing during a /// tracking operation other than give the tracking component idle /// time via IMsoComponent::FDoIdle. /// Note: there can only be one tracking component at a time. /// Return TRUE if successful, FALSE otherwise. /// </devdoc> bool UnsafeNativeMethods.IMsoComponentManager.FSetTrackingComponent(IntPtr dwComponentID, bool fTrack) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); ComponentHashtableEntry entry = (ComponentHashtableEntry)OleComponents[dwLocalComponentID]; if (entry == null) { return false; } if (entry.component == trackingComponent ^ fTrack) { return false; } if (fTrack) { trackingComponent = entry.component; } else { trackingComponent = null; } return true; }
bool UnsafeNativeMethods.IMsoComponent.FDebugMessage(IntPtr hInst, int msg, IntPtr wparam, IntPtr lparam) { UnsafeNativeMethods.IMsoComponent component = this.Component; return((component != null) && component.FDebugMessage(hInst, msg, wparam, lparam)); }
void UnsafeNativeMethods.IMsoComponent.Terminate() { if ((this._components != null) && (this._components.Values.Count > 0)) { UnsafeNativeMethods.IMsoComponent[] array = new UnsafeNativeMethods.IMsoComponent[this._components.Values.Count]; this._components.Values.CopyTo(array, 0); foreach (UnsafeNativeMethods.IMsoComponent component in array) { component.Terminate(); } } if (this._original != null) { this.RevokeComponent(); } this.Dispose(); }
bool UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg) { UnsafeNativeMethods.IMsoComponent component = this.Component; return((component != null) && component.FPreTranslateMessage(ref msg)); }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ComponentManager.UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent"]/*' /> /// <devdoc> /// Undo the registration of the component identified by dwComponentID /// (the cookie returned from the FRegisterComponent method). /// Return TRUE if successful, FALSE otherwise. /// </devdoc> bool UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent(IntPtr dwComponentID) { int dwLocalComponentID = unchecked((int)(long)dwComponentID); Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager: Revoking component " + dwLocalComponentID.ToString(CultureInfo.InvariantCulture)); ComponentHashtableEntry entry = (ComponentHashtableEntry)OleComponents[dwLocalComponentID]; if (entry == null) { Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "Compoenent not registered."); return false; } if (entry.component == activeComponent) { activeComponent = null; } if (entry.component == trackingComponent) { trackingComponent = null; } OleComponents.Remove(dwLocalComponentID); return true; }