mirror of
https://github.com/greenshot/greenshot
synced 2025-08-14 02:37:03 -07:00
Code quality changes [skip ci]
This commit is contained in:
parent
61cfe004c5
commit
798ca503a5
108 changed files with 1981 additions and 2258 deletions
|
@ -38,55 +38,42 @@ namespace Greenshot.Interop {
|
|||
/// <summary>
|
||||
/// Holds reference to the actual COM object which is wrapped by this proxy
|
||||
/// </summary>
|
||||
private object _COMObject;
|
||||
private readonly object _comObject;
|
||||
|
||||
/// <summary>
|
||||
/// Type of the COM object, set on constructor after getting the COM reference
|
||||
/// </summary>
|
||||
private readonly Type _COMType;
|
||||
private readonly Type _comType;
|
||||
|
||||
/// <summary>
|
||||
/// The type of which method calls are intercepted and executed on the COM object.
|
||||
/// </summary>
|
||||
private readonly Type _InterceptType;
|
||||
private readonly Type _interceptType;
|
||||
|
||||
#endregion
|
||||
[DllImport("ole32.dll")]
|
||||
static extern int ProgIDFromCLSID([In] ref Guid clsid, [MarshalAs(UnmanagedType.LPWStr)] out string lplpszProgID);
|
||||
|
||||
#region Construction
|
||||
|
||||
/// <summary>
|
||||
/// Gets a COM object and returns the transparent proxy which intercepts all calls to the object
|
||||
/// </summary>
|
||||
/// <param name="type">Interface which defines the method and properties to intercept</param>
|
||||
/// <typeparam name="T">Interface which defines the method and properties to intercept</typeparam>
|
||||
/// <returns>Transparent proxy to the real proxy for the object</returns>
|
||||
/// <remarks>The <paramref name="type"/> must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks>
|
||||
/// <remarks>T must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks>
|
||||
public static T GetInstance<T>() {
|
||||
Type type = typeof(T);
|
||||
if (null == type) {
|
||||
throw new ArgumentNullException("type");
|
||||
throw new ArgumentNullException(nameof(T));
|
||||
}
|
||||
if (!type.IsInterface) {
|
||||
throw new ArgumentException("The specified type must be an interface.", "type");
|
||||
throw new ArgumentException("The specified type must be an interface.", nameof(T));
|
||||
}
|
||||
|
||||
ComProgIdAttribute progIDAttribute = ComProgIdAttribute.GetAttribute(type);
|
||||
if (null == progIDAttribute || null == progIDAttribute.Value || 0 == progIDAttribute.Value.Length) {
|
||||
throw new ArgumentException("The specified type must define a ComProgId attribute.", "type");
|
||||
}
|
||||
string progId = progIDAttribute.Value;
|
||||
|
||||
// Convert from clsid to Prog ID, if needed
|
||||
if (progId.StartsWith("clsid:")) {
|
||||
Guid guid = new Guid(progId.Substring(6));
|
||||
int result = ProgIDFromCLSID(ref guid, out progId);
|
||||
if (result != 0) {
|
||||
//LOG.WarnFormat("Error {0} getting progId {1}", result, progIDAttribute.Value);
|
||||
} else {
|
||||
//LOG.InfoFormat("Mapped {0} to progId {1}", progIDAttribute.Value, progId);
|
||||
}
|
||||
ComProgIdAttribute progIdAttribute = ComProgIdAttribute.GetAttribute(type);
|
||||
if (string.IsNullOrEmpty(progIdAttribute?.Value)) {
|
||||
throw new ArgumentException("The specified type must define a ComProgId attribute.", nameof(T));
|
||||
}
|
||||
string progId = progIdAttribute.Value;
|
||||
|
||||
object comObject = null;
|
||||
try {
|
||||
|
@ -96,8 +83,6 @@ namespace Greenshot.Interop {
|
|||
//LOG.DebugFormat("No current instance of {0} object available.", progId);
|
||||
} else if (comE.ErrorCode == CO_E_CLASSSTRING) {
|
||||
//LOG.WarnFormat("Unknown progId {0}", progId);
|
||||
} else {
|
||||
//LOG.Warn("Error getting active object for " + progId, comE);
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.Warn("Error getting active object for " + progId, e);
|
||||
|
@ -114,37 +99,26 @@ namespace Greenshot.Interop {
|
|||
/// Gets or creates a COM object and returns the transparent proxy which intercepts all calls to the object
|
||||
/// The ComProgId can be a normal ComProgId or a GUID prefixed with "clsid:"
|
||||
/// </summary>
|
||||
/// <param name="type">Interface which defines the method and properties to intercept</param>
|
||||
/// <typeparam name="T">Interface which defines the method and properties to intercept</typeparam>
|
||||
/// <returns>Transparent proxy to the real proxy for the object</returns>
|
||||
/// <remarks>The <paramref name="type"/> must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks>
|
||||
/// <remarks>The type must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks>
|
||||
public static T GetOrCreateInstance<T>() {
|
||||
Type type = typeof(T);
|
||||
if (null == type) {
|
||||
throw new ArgumentNullException("type");
|
||||
throw new ArgumentNullException(nameof(T));
|
||||
}
|
||||
if (!type.IsInterface) {
|
||||
throw new ArgumentException("The specified type must be an interface.", "type");
|
||||
throw new ArgumentException("The specified type must be an interface.", nameof(T));
|
||||
}
|
||||
|
||||
ComProgIdAttribute progIDAttribute = ComProgIdAttribute.GetAttribute(type);
|
||||
if (null == progIDAttribute || null == progIDAttribute.Value || 0 == progIDAttribute.Value.Length) {
|
||||
throw new ArgumentException("The specified type must define a ComProgId attribute.", "type");
|
||||
ComProgIdAttribute progIdAttribute = ComProgIdAttribute.GetAttribute(type);
|
||||
if (string.IsNullOrEmpty(progIdAttribute?.Value)) {
|
||||
throw new ArgumentException("The specified type must define a ComProgId attribute.", nameof(T));
|
||||
}
|
||||
|
||||
object comObject = null;
|
||||
Type comType = null;
|
||||
string progId = progIDAttribute.Value;
|
||||
|
||||
// Convert from clsid to Prog ID, if needed
|
||||
if (progId.StartsWith("clsid:")) {
|
||||
Guid guid = new Guid(progId.Substring(6));
|
||||
int result = ProgIDFromCLSID(ref guid, out progId);
|
||||
if (result != 0) {
|
||||
//LOG.WarnFormat("Error {0} getting progId {1}", result, progIDAttribute.Value);
|
||||
} else {
|
||||
//LOG.InfoFormat("Mapped {0} to progId {1}", progIDAttribute.Value, progId);
|
||||
}
|
||||
}
|
||||
string progId = progIdAttribute.Value;
|
||||
|
||||
try {
|
||||
comObject = Marshal.GetActiveObject(progId);
|
||||
|
@ -154,8 +128,6 @@ namespace Greenshot.Interop {
|
|||
} else if (comE.ErrorCode == CO_E_CLASSSTRING) {
|
||||
//LOG.WarnFormat("Unknown progId {0} (application not installed)", progId);
|
||||
return default(T);
|
||||
} else {
|
||||
//LOG.Warn("Error getting active object for " + progId, comE);
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.Warn("Error getting active object for " + progId, e);
|
||||
|
@ -193,10 +165,10 @@ namespace Greenshot.Interop {
|
|||
/// <returns>Transparent proxy to the real proxy for the object</returns>
|
||||
private static object Wrap(object comObject, Type type) {
|
||||
if (null == comObject) {
|
||||
throw new ArgumentNullException("comObject");
|
||||
throw new ArgumentNullException(nameof(comObject));
|
||||
}
|
||||
if (null == type) {
|
||||
throw new ArgumentNullException("type");
|
||||
throw new ArgumentNullException(nameof(type));
|
||||
}
|
||||
|
||||
COMWrapper wrapper = new COMWrapper(comObject, type);
|
||||
|
@ -214,9 +186,9 @@ namespace Greenshot.Interop {
|
|||
/// </param>
|
||||
private COMWrapper(object comObject, Type type)
|
||||
: base(type) {
|
||||
_COMObject = comObject;
|
||||
_COMType = comObject.GetType();
|
||||
_InterceptType = type;
|
||||
_comObject = comObject;
|
||||
_comType = comObject.GetType();
|
||||
_interceptType = type;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -247,17 +219,17 @@ namespace Greenshot.Interop {
|
|||
/// <see cref="IDisposable"/> interface.
|
||||
/// </param>
|
||||
private void Dispose(bool disposing) {
|
||||
if (disposing && null != _COMObject) {
|
||||
if (Marshal.IsComObject(_COMObject)) {
|
||||
if (disposing && null != _comObject) {
|
||||
if (Marshal.IsComObject(_comObject)) {
|
||||
try {
|
||||
while (Marshal.ReleaseComObject(_COMObject) > 0);
|
||||
while (Marshal.ReleaseComObject(_comObject) > 0)
|
||||
{
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.WarnFormat("Problem releasing {0}", _COMType);
|
||||
//LOG.Warn("Error: ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
_COMObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -272,7 +244,7 @@ namespace Greenshot.Interop {
|
|||
/// The full name of the intercepted type.
|
||||
/// </returns>
|
||||
public override string ToString() {
|
||||
return _InterceptType.FullName;
|
||||
return _interceptType.FullName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -282,7 +254,7 @@ namespace Greenshot.Interop {
|
|||
/// The hash code of the wrapped object.
|
||||
/// </returns>
|
||||
public override int GetHashCode() {
|
||||
return _COMObject.GetHashCode();
|
||||
return _comObject.GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -298,7 +270,7 @@ namespace Greenshot.Interop {
|
|||
if (null != value && RemotingServices.IsTransparentProxy(value)) {
|
||||
COMWrapper wrapper = RemotingServices.GetRealProxy(value) as COMWrapper;
|
||||
if (null != wrapper) {
|
||||
return _COMObject == wrapper._COMObject;
|
||||
return _comObject == wrapper._comObject;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -319,7 +291,7 @@ namespace Greenshot.Interop {
|
|||
/// </exception>
|
||||
private static Type GetByValType(Type byRefType) {
|
||||
if (null == byRefType) {
|
||||
throw new ArgumentNullException("byRefType");
|
||||
throw new ArgumentNullException(nameof(byRefType));
|
||||
}
|
||||
|
||||
if (byRefType.IsByRef) {
|
||||
|
@ -333,97 +305,6 @@ namespace Greenshot.Interop {
|
|||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Use this static method to cast a wrapped proxy to a new wrapper proxy of the supplied type.
|
||||
/// In English, use this to cast you base "COM" interface to a specialized interface.
|
||||
/// E.G. Outlook Item -> MailItem
|
||||
/// </summary>
|
||||
/// <typeparam name="T">the type you want to cast to</typeparam>
|
||||
/// <param name="wrapperProxy">The wrapper interface, e.g. something you got back from calling GetItem</param>
|
||||
/// <returns>A new wrapper proxy for the specified type</returns>
|
||||
public static T Cast<T>(object wrapperProxy) {
|
||||
if (wrapperProxy == null) {
|
||||
return default(T);
|
||||
}
|
||||
|
||||
Type newType = typeof(T);
|
||||
COMWrapper oldWrapper = RemotingServices.GetRealProxy(wrapperProxy) as COMWrapper;
|
||||
if (oldWrapper == null) {
|
||||
throw new ArgumentException("wrapper proxy was no COMWrapper");
|
||||
}
|
||||
if (oldWrapper._InterceptType.IsAssignableFrom(newType)) {
|
||||
COMWrapper newWrapper = new COMWrapper(oldWrapper._COMObject, newType);
|
||||
return (T)newWrapper.GetTransparentProxy();
|
||||
}
|
||||
throw new InvalidCastException(string.Format("{0} is not assignable from {1}", oldWrapper._InterceptType, newType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the "com" type of the wrapperproxy, making it possible to perform reflection on it.
|
||||
/// </summary>
|
||||
/// <param name="wrapperProxy">wrapperProxy to get the type from</param>
|
||||
/// <returns>Type</returns>
|
||||
public static Type GetUnderlyingTypeForWrapper(object wrapperProxy) {
|
||||
Type returnType = null;
|
||||
COMWrapper wrapper = RemotingServices.GetRealProxy(wrapperProxy) as COMWrapper;
|
||||
if (wrapper != null) {
|
||||
IDispatch dispatch = wrapper._COMObject as IDispatch;
|
||||
if (dispatch != null) {
|
||||
int result = dispatch.GetTypeInfo(0, 0, out returnType);
|
||||
if (result != 0) {
|
||||
//LOG.DebugFormat("GetTypeInfo : 0x{0} ({1})", result.ToString("X"), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the Type of a IDispatch
|
||||
/// </summary>
|
||||
/// <param name="dispatch">IDispatch to get the type object for</param>
|
||||
/// <returns>Type of the IDispatch</returns>
|
||||
public static Type GetUnderlyingType(IDispatch dispatch) {
|
||||
Type returnType = null;
|
||||
if (dispatch != null) {
|
||||
int result = dispatch.GetTypeInfo(0, 0, out returnType);
|
||||
if (result != 0) {
|
||||
//LOG.DebugFormat("GetTypeInfo : 0x{0} ({1})", result.ToString("X"), result);
|
||||
}
|
||||
}
|
||||
return returnType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dump the Type-Information for the Type to the log, this uses reflection
|
||||
/// </summary>
|
||||
/// <param name="type">Type to inspect</param>
|
||||
public static void DumpTypeInfo(Type type) {
|
||||
//LOG.InfoFormat("Type information for Type with name: {0}", type.Name);
|
||||
try {
|
||||
foreach (MemberInfo memberInfo in type.GetMembers()) {
|
||||
//LOG.InfoFormat("Member: {0};", memberInfo.ToString());
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.Error(memberException);
|
||||
}
|
||||
try {
|
||||
foreach (PropertyInfo propertyInfo in type.GetProperties()) {
|
||||
//LOG.InfoFormat("Property: {0};", propertyInfo.ToString());
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.Error(propertyException);
|
||||
}
|
||||
try {
|
||||
foreach (FieldInfo fieldInfo in type.GetFields()) {
|
||||
//LOG.InfoFormat("Field: {0};", fieldInfo.ToString());
|
||||
}
|
||||
} catch (Exception) {
|
||||
//LOG.Error(fieldException);
|
||||
}
|
||||
//LOG.InfoFormat("Type information end.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intercept method calls
|
||||
/// </summary>
|
||||
|
@ -473,22 +354,19 @@ namespace Greenshot.Interop {
|
|||
} else if ("ToString" == methodName && 0 == argCount && typeof(string) == returnType) {
|
||||
returnValue = ToString();
|
||||
} else if ("GetType" == methodName && 0 == argCount && typeof(Type) == returnType) {
|
||||
returnValue = _InterceptType;
|
||||
returnValue = _interceptType;
|
||||
} else if ("GetHashCode" == methodName && 0 == argCount && typeof(int) == returnType) {
|
||||
returnValue = GetHashCode();
|
||||
} else if ("Equals" == methodName && 1 == argCount && typeof(bool) == returnType) {
|
||||
returnValue = 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;
|
||||
var handler = callMessage.InArgs[0] as Delegate;
|
||||
if (null == handler) {
|
||||
return new ReturnMessage(new ArgumentNullException("handler"), callMessage);
|
||||
return new ReturnMessage(new ArgumentNullException(nameof(handler)), callMessage);
|
||||
}
|
||||
} else {
|
||||
invokeObject = _COMObject;
|
||||
invokeType = _COMType;
|
||||
invokeObject = _comObject;
|
||||
invokeType = _comType;
|
||||
|
||||
if (methodName.StartsWith("get_")) {
|
||||
// Property Get
|
||||
|
@ -532,7 +410,7 @@ namespace Greenshot.Interop {
|
|||
wrapper = RemotingServices.GetRealProxy(args[i]) as COMWrapper;
|
||||
if (null != wrapper) {
|
||||
originalArgs[i] = wrapper;
|
||||
args[i] = wrapper._COMObject;
|
||||
args[i] = wrapper._comObject;
|
||||
}
|
||||
} else if (0 != outArgsCount && argModifiers[0][i]) {
|
||||
byValType = GetByValType(parameters[i].ParameterType);
|
||||
|
@ -543,7 +421,7 @@ namespace Greenshot.Interop {
|
|||
if (null == args[i]) {
|
||||
args[i] = new DispatchWrapper(null);
|
||||
}
|
||||
} else if (typeof(Decimal) == byValType) {
|
||||
} else if (typeof(decimal) == byValType) {
|
||||
// If we're passing a decimal value by reference,
|
||||
// we need to pass a CurrencyWrapper to avoid a
|
||||
// type mismatch exception.
|
||||
|
@ -590,7 +468,7 @@ namespace Greenshot.Interop {
|
|||
wrapper = null;
|
||||
|
||||
byValType = GetByValType(parameter.ParameterType);
|
||||
if (typeof(Decimal) == byValType) {
|
||||
if (typeof(decimal) == byValType) {
|
||||
if (arg is CurrencyWrapper) {
|
||||
arg = ((CurrencyWrapper)arg).WrappedObject;
|
||||
}
|
||||
|
@ -599,7 +477,7 @@ namespace Greenshot.Interop {
|
|||
} else if (byValType.IsInterface) {
|
||||
if (Marshal.IsComObject(arg)) {
|
||||
wrapper = originalArgs[i];
|
||||
if (null != wrapper && wrapper._COMObject != arg) {
|
||||
if (null != wrapper && wrapper._comObject != arg) {
|
||||
wrapper.Dispose();
|
||||
wrapper = null;
|
||||
}
|
||||
|
@ -626,7 +504,7 @@ namespace Greenshot.Interop {
|
|||
/// <param name="o">object to cast</param>
|
||||
/// <returns></returns>
|
||||
public bool CanCastTo(Type toType, object o) {
|
||||
bool returnValue = _InterceptType.IsAssignableFrom(toType);
|
||||
bool returnValue = _interceptType.IsAssignableFrom(toType);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue