From 0aa8e4347825a91a97be4cc5ce823463ed4b1c30 Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 6 Mar 2012 17:43:23 +0000 Subject: [PATCH] Refactoring the interop code to a separate project. Still need to add the interop DLL to the final exe... or maybe I want to use references inside the projects... git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1689 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotPlugin/Core/CoreConfiguration.cs | 6 +- GreenshotPlugin/Core/ImageHelper.cs | 2 +- GreenshotPlugin/Core/LanguageHelper.cs | 2 +- GreenshotPlugin/Core/LogHelper.cs | 2 +- GreenshotPlugin/Core/NetworkHelper.cs | 2 +- GreenshotPlugin/Core/WindowsHelper.cs | 2 +- GreenshotPlugin/GreenshotPlugin.csproj | 6 + GreenshotPlugin/IniFile/IniAttributes.cs | 2 +- GreenshotPlugin/IniFile/IniConfig.cs | 2 +- GreenshotPlugin/IniFile/IniReader.cs | 2 +- GreenshotPlugin/IniFile/IniSection.cs | 2 +- GreenshotPlugin/IniFile/IniValue.cs | 2 +- GreenshotPlugin/Interop/COMWrapper.cs | 490 ------------------ GreenshotPlugin/Interop/ComProgIdAttribute.cs | 81 --- 14 files changed, 18 insertions(+), 585 deletions(-) delete mode 100644 GreenshotPlugin/Interop/COMWrapper.cs delete mode 100644 GreenshotPlugin/Interop/ComProgIdAttribute.cs diff --git a/GreenshotPlugin/Core/CoreConfiguration.cs b/GreenshotPlugin/Core/CoreConfiguration.cs index 91417465f..0672fb45a 100644 --- a/GreenshotPlugin/Core/CoreConfiguration.cs +++ b/GreenshotPlugin/Core/CoreConfiguration.cs @@ -25,7 +25,8 @@ using System.Drawing; using System.IO; using System.Windows.Forms; using Greenshot.Plugin; -using IniFile; +using Greenshot.Interop.Office; +using Greenshot.IniFile; namespace GreenshotPlugin.Core { public enum ClipboardFormat { @@ -37,9 +38,6 @@ namespace GreenshotPlugin.Core { public enum WindowCaptureMode { Screen, GDI, Aero, AeroTransparent, Auto } - public enum EmailFormat { - Text, HTML - } /// /// Description of CoreConfiguration. diff --git a/GreenshotPlugin/Core/ImageHelper.cs b/GreenshotPlugin/Core/ImageHelper.cs index 0aaece1ed..bb921c3a7 100644 --- a/GreenshotPlugin/Core/ImageHelper.cs +++ b/GreenshotPlugin/Core/ImageHelper.cs @@ -27,7 +27,7 @@ using System.Drawing.Drawing2D; using GreenshotPlugin.UnmanagedHelpers; using GreenshotPlugin.Core; -using IniFile; +using Greenshot.IniFile; using System.Runtime.InteropServices; namespace GreenshotPlugin.Core { diff --git a/GreenshotPlugin/Core/LanguageHelper.cs b/GreenshotPlugin/Core/LanguageHelper.cs index 14b4d43aa..077f406b1 100644 --- a/GreenshotPlugin/Core/LanguageHelper.cs +++ b/GreenshotPlugin/Core/LanguageHelper.cs @@ -27,7 +27,7 @@ using System.Text; using System.Threading; using System.Xml; -using IniFile; +using Greenshot.IniFile; using Microsoft.Win32; namespace GreenshotPlugin.Core { diff --git a/GreenshotPlugin/Core/LogHelper.cs b/GreenshotPlugin/Core/LogHelper.cs index 4df56e5fd..bd2f3aee6 100644 --- a/GreenshotPlugin/Core/LogHelper.cs +++ b/GreenshotPlugin/Core/LogHelper.cs @@ -23,7 +23,7 @@ using System.IO; using System.Reflection; using System.Windows.Forms; -using IniFile; +using Greenshot.IniFile; using log4net; using log4net.Appender; using log4net.Config; diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index 8671f0919..7e289c02e 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -24,7 +24,7 @@ using System.IO; using System.Net; using System.Text; -using IniFile; +using Greenshot.IniFile; namespace GreenshotPlugin.Core { /// diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 4d94bdaad..4ed110f3b 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -33,7 +33,7 @@ using System.Windows.Forms; using Greenshot.Plugin; using GreenshotPlugin.UnmanagedHelpers; -using IniFile; +using Greenshot.IniFile; /// /// Code for handling with "windows" diff --git a/GreenshotPlugin/GreenshotPlugin.csproj b/GreenshotPlugin/GreenshotPlugin.csproj index 06348a975..a585f6f60 100644 --- a/GreenshotPlugin/GreenshotPlugin.csproj +++ b/GreenshotPlugin/GreenshotPlugin.csproj @@ -124,6 +124,12 @@ + + + {ADD4E400-56E1-4E31-A5C2-070BEB238A0F} + GreenshotInterop + + rmdir /S /Q "$(SolutionDir)bin\$(Configuration)\Plugins" "$(SolutionDir)\tools\TortoiseSVN\SubWCRev.exe" "$(ProjectDir)\" "$(ProjectDir)\Properties\AssemblyInfo.cs.template" "$(ProjectDir)\Properties\AssemblyInfo.cs" diff --git a/GreenshotPlugin/IniFile/IniAttributes.cs b/GreenshotPlugin/IniFile/IniAttributes.cs index 86c508d7a..2b8faa7cf 100644 --- a/GreenshotPlugin/IniFile/IniAttributes.cs +++ b/GreenshotPlugin/IniFile/IniAttributes.cs @@ -20,7 +20,7 @@ */ using System; -namespace IniFile { +namespace Greenshot.IniFile { /// /// Attribute for telling that this class is linked to a section in the ini-configuration /// diff --git a/GreenshotPlugin/IniFile/IniConfig.cs b/GreenshotPlugin/IniFile/IniConfig.cs index ce0a11e6f..bb8659680 100644 --- a/GreenshotPlugin/IniFile/IniConfig.cs +++ b/GreenshotPlugin/IniFile/IniConfig.cs @@ -26,7 +26,7 @@ using System.Reflection; using System.Text; using System.Threading; -namespace IniFile { +namespace Greenshot.IniFile { public class IniConfig { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(IniConfig)); private const string INI_EXTENSION = ".ini"; diff --git a/GreenshotPlugin/IniFile/IniReader.cs b/GreenshotPlugin/IniFile/IniReader.cs index bb2b91543..4098225bc 100644 --- a/GreenshotPlugin/IniFile/IniReader.cs +++ b/GreenshotPlugin/IniFile/IniReader.cs @@ -23,7 +23,7 @@ using System.Collections.Generic; using System.IO; using System.Text; -namespace IniFile { +namespace Greenshot.IniFile { public static class IniReader { private const string SECTION_START = "["; private const string SECTION_END = "]"; diff --git a/GreenshotPlugin/IniFile/IniSection.cs b/GreenshotPlugin/IniFile/IniSection.cs index 2c6d6717d..444a3847f 100644 --- a/GreenshotPlugin/IniFile/IniSection.cs +++ b/GreenshotPlugin/IniFile/IniSection.cs @@ -21,7 +21,7 @@ using System; using System.Collections.Generic; -namespace IniFile { +namespace Greenshot.IniFile { /// /// Base class for all IniSections /// diff --git a/GreenshotPlugin/IniFile/IniValue.cs b/GreenshotPlugin/IniFile/IniValue.cs index d1a56455c..3fd85f080 100644 --- a/GreenshotPlugin/IniFile/IniValue.cs +++ b/GreenshotPlugin/IniFile/IniValue.cs @@ -21,7 +21,7 @@ using System; using System.Reflection; -namespace IniFile { +namespace Greenshot.IniFile { /// /// A container to be able to pass the value from a IniSection around. /// diff --git a/GreenshotPlugin/Interop/COMWrapper.cs b/GreenshotPlugin/Interop/COMWrapper.cs deleted file mode 100644 index 8d4960635..000000000 --- a/GreenshotPlugin/Interop/COMWrapper.cs +++ /dev/null @@ -1,490 +0,0 @@ -/* - * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom - * - * For more information see: http://getgreenshot.org/ - * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -using System; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Runtime.Remoting; -using System.Runtime.Remoting.Messaging; -using System.Runtime.Remoting.Proxies; - -namespace Greenshot.Interop { - /// - /// Wraps a late-bound COM server. - /// - public sealed class COMWrapper : RealProxy, IDisposable { - private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(COMWrapper)); - private const int MK_E_UNAVAILABLE = -2147221021; - #region Private Data - - /// - /// Holds reference to the actual COM object which is wrapped by this proxy - /// - private object _COMObject; - - /// - /// Type of the COM object, set on constructor after getting the COM reference - /// - private Type _COMType; - - /// - /// The type of which method calls are intercepted and executed on the COM object. - /// - private Type _InterceptType; - - #endregion - - #region Construction - - /// - /// Gets a COM object and returns the transparent proxy which intercepts all calls to the object - /// - /// Interface which defines the method and properties to intercept - /// Transparent proxy to the real proxy for the object - /// The must be an interface decorated with the attribute. - public static object GetInstance(Type type) { - if (null == type) { - throw new ArgumentNullException("type"); - } - if (!type.IsInterface) { - throw new ArgumentException("The specified type must be an interface.", "type"); - } - - ComProgIdAttribute progID = ComProgIdAttribute.GetAttribute(type); - if (null == progID || null == progID.Value || 0 == progID.Value.Length) { - throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); - } - - object comObject = null; - Type comType = Type.GetTypeFromProgID(progID.Value, true); - try { - comObject = Marshal.GetActiveObject(progID.Value); - } catch (COMException comE) { - if (comE.ErrorCode == MK_E_UNAVAILABLE) { - LOG.DebugFormat("No current instance of {0} object available.", progID.Value); - } else { - LOG.Warn("Error getting active object for " + progID.Value, comE); - } - } catch (Exception e) { - LOG.Warn("Error getting active object for " + progID.Value, e); - } - - if (comObject != null) { - COMWrapper wrapper = new COMWrapper(comObject, type); - return wrapper.GetTransparentProxy(); - } - return null; - } - - /// - /// Gets or creates a COM object and returns the transparent proxy - /// which intercepts all calls to the object - /// - /// Interface which defines the method and properties to intercept - /// Transparent proxy to the real proxy for the object - /// The must be an interface decorated with the attribute. - public static object GetOrCreateInstance(Type type) { - if (null == type) { - throw new ArgumentNullException("type"); - } - if (!type.IsInterface) { - throw new ArgumentException("The specified type must be an interface.", "type"); - } - - ComProgIdAttribute progID = ComProgIdAttribute.GetAttribute(type); - if (null == progID || null == progID.Value || 0 == progID.Value.Length) { - throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); - } - - object comObject = null; - Type comType = Type.GetTypeFromProgID(progID.Value, true); - try { - comObject = Marshal.GetActiveObject(progID.Value); - } catch (COMException comE) { - if (comE.ErrorCode == MK_E_UNAVAILABLE) { - LOG.DebugFormat("No current instance of {0} object available.", progID.Value); - } else { - LOG.Warn("Error getting active object for " + progID.Value, comE); - } - } catch (Exception e) { - LOG.Warn("Error getting active object for " + progID.Value, e); - } - // Did we get the current instance? If not, try to create a new - if (comObject == null) { - try { - comObject = Activator.CreateInstance(comType); - if (comObject != null) { - LOG.DebugFormat("Created new instance of {0} object.", progID.Value); - } - } catch (Exception e) { - LOG.Warn("Error creating object for " + progID.Value, e); - } - } - if (comObject != null) { - COMWrapper wrapper = new COMWrapper(comObject, type); - return wrapper.GetTransparentProxy(); - } - throw new TypeLoadException(string.Format("Unable to get or create an instance of the specified COM server \"{0}\".", progID.Value)); - } - - /// - /// Wrap an object and return the transparent proxy which intercepts all calls - /// to the object - /// - /// An object to intercept - /// Interface which defines the method and properties to intercept - /// Transparent proxy to the real proxy for the object - public static object Wrap(object comObject, Type type) { - if (null == comObject) { - throw new ArgumentNullException("comObject"); - } - if (null == type) { - throw new ArgumentNullException("type"); - } - - COMWrapper wrapper = new COMWrapper(comObject, type); - return wrapper.GetTransparentProxy(); - } - - /// - /// Constructor - /// - /// - /// The COM object to wrap. - /// - /// - /// The interface type to impersonate. - /// - private COMWrapper(object comObject, Type type) : base( type ) { - this._COMObject = comObject; - this._COMType = comObject.GetType(); - this._InterceptType = type; - } - - #endregion - - #region Clean up - - /// - /// If is not called, we need to make - /// sure that the COM object is still cleaned up. - /// - ~COMWrapper() { - LOG.DebugFormat("Finalize {0}", this._InterceptType.ToString()); - this.Dispose(false); - } - - /// - /// Cleans up the COM object. - /// - public void Dispose() { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Release the COM reference - /// - /// - /// if this was called from the - /// interface. - /// - private void Dispose(bool disposing) { - if (null != this._COMObject) { - if(Marshal.IsComObject(this._COMObject)) { - try { - while (Marshal.ReleaseComObject(this._COMObject) > 0) ; - } catch (Exception ex) { - LOG.WarnFormat("Problem releasing {0}", _COMType); - LOG.Warn("Error: ", ex); - } - } - - this._COMObject = null; - } - } - - #endregion - - #region Object methods - - /// - /// Returns a string representing the wrapped object. - /// - /// - /// The full name of the intercepted type. - /// - public override string ToString() { - return this._InterceptType.FullName; - } - - /// - /// Returns the hash code of the wrapped object. - /// - /// - /// The hash code of the wrapped object. - /// - public override int GetHashCode() { - return this._COMObject.GetHashCode(); - } - - /// - /// Compares this object to another. - /// - /// - /// The value to compare to. - /// - /// - /// if the objects are equal. - /// - public override bool Equals(object value) { - if (null != value && RemotingServices.IsTransparentProxy(value)) { - COMWrapper wrapper = RemotingServices.GetRealProxy(value) as COMWrapper; - if (null != wrapper) { - return this._COMObject == wrapper._COMObject; - } - } - - return base.Equals(value); - } - - /// - /// Returns the base type for a reference type. - /// - /// - /// The reference type. - /// - /// - /// The base value type. - /// - /// - /// is . - /// - private static Type GetByValType(Type byRefType) { - if (null == byRefType) { - throw new ArgumentNullException("byRefType"); - } - - if (byRefType.IsByRef) { - string name = byRefType.FullName; - name = name.Substring(0, name.Length - 1); - byRefType = byRefType.Assembly.GetType(name, true); - } - - return byRefType; - } - - #endregion - - /// - /// Intercept method calls - /// - /// - /// Contains information about the method being called - /// - /// - /// A . - /// - public override IMessage Invoke(IMessage myMessage) { - IMethodCallMessage callMessage = myMessage as IMethodCallMessage; - if (null == callMessage) { - LOG.DebugFormat("Message type not implemented: {0}", myMessage.GetType().ToString()); - return null; - } - - MethodInfo method = callMessage.MethodBase as MethodInfo; - if (null == method) { - LOG.DebugFormat("Unrecognized Invoke call: {0}", callMessage.MethodBase.ToString()); - return null; - } - - object returnValue = null; - object[] outArgs = null; - int outArgsCount = 0; - - string methodName = method.Name; - Type returnType = method.ReturnType; - BindingFlags flags = BindingFlags.InvokeMethod; - int argCount = callMessage.ArgCount; - - object invokeObject; - Type invokeType; - Type byValType; - - object[] args; - object arg; - COMWrapper[] originalArgs; - COMWrapper wrapper; - - ParameterModifier[] argModifiers = null; - ParameterInfo[] parameters = null; - ParameterInfo parameter; - - if ("Dispose" == methodName && 0 == argCount && typeof(void) == returnType) { - this.Dispose(); - } else if ("ToString" == methodName && 0 == argCount && typeof(string) == returnType) { - returnValue = this.ToString(); - } else if ("GetType" == methodName && 0 == argCount && typeof(System.Type) == returnType) { - returnValue = this._InterceptType; - } else if ("GetHashCode" == methodName && 0 == argCount && typeof(int) == returnType) { - returnValue = this.GetHashCode(); - } else if ("Equals" == methodName && 1 == argCount && typeof(bool) == returnType) { - returnValue = this.Equals(callMessage.Args[0]); - } else if (1 == argCount && typeof(void) == returnType && (methodName.StartsWith("add_") || methodName.StartsWith("remove_"))) { - bool removeHandler = methodName.StartsWith("remove_"); - methodName = methodName.Substring(removeHandler ? 7 : 4); - - Delegate handler = callMessage.InArgs[0] as Delegate; - if (null == handler) { - return new ReturnMessage(new ArgumentNullException("handler"), callMessage); - } - } else { - invokeObject = this._COMObject; - invokeType = this._COMType; - - if (methodName.StartsWith("get_")) { - // Property Get - methodName = methodName.Substring(4); - flags = BindingFlags.GetProperty; - args = callMessage.InArgs; - } else if (methodName.StartsWith("set_")) { - // Property Set - methodName = methodName.Substring(4); - flags = BindingFlags.SetProperty; - args = callMessage.InArgs; - } else { - args = callMessage.Args; - if (null != args && 0 != args.Length) { - // Modifiers for ref / out parameters - argModifiers = new ParameterModifier[1]; - argModifiers[0] = new ParameterModifier(args.Length); - - parameters = method.GetParameters(); - for(int i=0; i. - */ -using System; - -namespace Greenshot.Interop { - /// - /// An attribute to specifiy the ProgID of the COM class to create. (As suggested by Kristen Wegner) - /// - [AttributeUsage(AttributeTargets.Interface, Inherited=false, AllowMultiple=false)] - public sealed class ComProgIdAttribute : Attribute { - private string _value; - - /// - /// Extracts the attribute from the specified type. - /// - /// - /// The interface type. - /// - /// - /// The . - /// - /// - /// is . - /// - public static ComProgIdAttribute GetAttribute(Type interfaceType) { - if (null == interfaceType) { - throw new ArgumentNullException("interfaceType"); - } - - Type attributeType = typeof(ComProgIdAttribute); - object[] attributes = interfaceType.GetCustomAttributes(attributeType, false); - - if (null == attributes || 0 == attributes.Length) { - Type[] interfaces = interfaceType.GetInterfaces(); - for(int i=0; iConstructor - /// The COM ProgID. - public ComProgIdAttribute(string value) { - _value = value; - } - - /// - /// Returns the COM ProgID - /// - public string Value { - get { return _value; } - } - } -}