/// <summary> /// Intercept method calls /// </summary> /// <param name="myMessage"> /// Contains information about the method being called /// </param> /// <returns> /// A <see cref="ReturnMessage" />. /// </returns> public override IMessage Invoke(IMessage myMessage) { var callMessage = myMessage as IMethodCallMessage; var method = callMessage?.MethodBase as MethodInfo; if (method == null) { if (callMessage != null) { Debug.WriteLine($"Unrecognized Invoke call: {callMessage.MethodBase}"); } return(null); } object returnValue = null; object[] outArgs = null; var outArgsCount = 0; var methodName = method.Name; var returnType = method.ReturnType; var flags = BindingFlags.InvokeMethod; var argCount = callMessage.ArgCount; ParameterModifier[] argModifiers = null; ParameterInfo[] parameters = null; if ("Dispose" == methodName && 0 == argCount && typeof(void) == returnType) { Dispose(); } else if ("ToString" == methodName && 0 == argCount && typeof(string) == returnType) { returnValue = ToString(); } else if ("GetType" == methodName && 0 == argCount && typeof(Type) == returnType) { 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_"))) { var handler = callMessage.InArgs[0] as Delegate; if (null == handler) { return(new ReturnMessage(new ArgumentNullException(nameof(handler)), callMessage)); } } else { var invokeObject = _comObject; var invokeType = _comType; ParameterInfo parameter; object[] args; 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 (var i = 0; i < parameters.Length; i++) { parameter = parameters[i]; if (parameter.IsOut || parameter.ParameterType.IsByRef) { argModifiers[0][i] = true; outArgsCount++; } } if (0 == outArgsCount) { argModifiers = null; } } } // Un-wrap wrapped COM objects before passing to the method COMWrapper[] originalArgs; COMWrapper wrapper; Type byValType; if (null == args || 0 == args.Length) { originalArgs = null; } else { originalArgs = new COMWrapper[args.Length]; for (var i = 0; i < args.Length; i++) { if (null != args[i] && RemotingServices.IsTransparentProxy(args[i])) { wrapper = RemotingServices.GetRealProxy(args[i]) as COMWrapper; if (null != wrapper) { originalArgs[i] = wrapper; args[i] = wrapper._comObject; } } else if (argModifiers != null && 0 != outArgsCount && argModifiers[0][i]) { byValType = GetByValType(parameters[i].ParameterType); if (byValType.IsInterface) { // If we're passing a COM object by reference, and // the parameter is null, we need to pass a // DispatchWrapper to avoid a type mismatch exception. if (null == args[i]) { args[i] = new DispatchWrapper(null); } } 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. // http://support.microsoft.com/?kbid=837378 args[i] = new CurrencyWrapper(args[i]); } } } } try { returnValue = invokeType.InvokeMember(methodName, flags, null, invokeObject, args, argModifiers, null, null); } catch (Exception ex) { return(new ReturnMessage(ex, callMessage)); } // Handle enum and interface return types if (null != returnValue) { if (returnType.IsInterface) { // Wrap the returned value in an intercepting COM wrapper if (Marshal.IsComObject(returnValue)) { returnValue = Wrap(returnValue, returnType); } } else if (returnType.IsEnum) { // Convert to proper Enum type returnValue = Enum.Parse(returnType, returnValue.ToString()); } } // Handle out args if (0 != outArgsCount) { if (args != null && parameters != null) { outArgs = new object[args.Length]; for (var i = 0; i < parameters.Length; i++) { if (argModifiers != null && !argModifiers[0][i]) { continue; } var arg = args[i]; if (null == arg) { continue; } parameter = parameters[i]; wrapper = null; byValType = GetByValType(parameter.ParameterType); if (typeof(decimal) == byValType) { if (arg is CurrencyWrapper) { arg = ((CurrencyWrapper)arg).WrappedObject; } } else if (byValType.IsEnum) { arg = Enum.Parse(byValType, arg.ToString()); } else if (byValType.IsInterface) { if (Marshal.IsComObject(arg)) { if (originalArgs != null) { wrapper = originalArgs[i]; } if (null != wrapper && wrapper._comObject != arg) { wrapper.Dispose(); wrapper = null; } if (null == wrapper) { wrapper = new COMWrapper(arg, byValType); } arg = wrapper.GetTransparentProxy(); } } outArgs[i] = arg; } } } } return(new ReturnMessage(returnValue, outArgs, outArgsCount, callMessage.LogicalCallContext, callMessage)); }
public static int Main(string[] args) { if (args.Length == 0) { Console.WriteLine(Usage); return(-1); } var filename = args[0]; var language = ModiLanguage.ENGLISH; if (args.Length >= 2) { language = (ModiLanguage)Enum.Parse(typeof(ModiLanguage), args[1]); } var orientimage = true; if (args.Length >= 3) { orientimage = bool.Parse(args[2]); } var straightenImage = true; if (args.Length >= 4) { straightenImage = bool.Parse(args[3]); } try { if (File.Exists(filename) || "-c".Equals(filename)) { using (var document = COMWrapper.GetOrCreateInstance <IDocument>()) { if (document == null) { Console.WriteLine("MODI not installed"); return(-2); } if ("-c".Equals(filename)) { return(0); } document.Create(filename); document.OCR(language, orientimage, straightenImage); var modiImage = document.Images[0]; var layout = modiImage.Layout; if (layout != null) { #if DEBUG if (layout.Words != null) { foreach (var word in ToEnumerable(layout.Words)) { if (word.Rects != null) { foreach (var rect in ToEnumerable(word.Rects)) { Debug.WriteLine($"Rect {rect.Left},{rect.Top},{rect.Right},{rect.Bottom} - Word {word.Text} : Confidence: {word.RecognitionConfidence}"); } } } } #endif if (layout.Text != null) { // For for BUG-1884: // Although trim is done in the OCR Plugin, it does make sense in the command too. Console.WriteLine(layout.Text.Trim()); } } document.Close(false); return(0); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(-1); }