Exemplo n.º 1
0
    // Test the CurrencyWrapper class.
    public void TestCurrencyWrapper()
    {
        CurrencyWrapper cw;

        // Test the (Decimal) constructor.
        cw = new CurrencyWrapper(123.45m);
        AssertEquals("CurrencyWrapper (1)", 123.45m, cw.WrappedObject);

        // Test the (Object) constructor.
        cw = new CurrencyWrapper((Object)(6123.45m));
        AssertEquals("CurrencyWrapper (2)", 6123.45m, cw.WrappedObject);

        // Test the failure cases of the (Object) constructor.
        try
        {
            cw = new CurrencyWrapper(null);
            Fail("CurrencyWrapper (3)");
        }
        catch (ArgumentException)
        {
            // Success.
        }
        try
        {
            cw = new CurrencyWrapper((Object)3);
            Fail("CurrencyWrapper (4)");
        }
        catch (ArgumentException)
        {
            // Success.
        }
    }
Exemplo n.º 2
0
#pragma warning disable 618 // CurrencyWrapper is marked obsolete
        private void CurrencyWrapper()
        {
            decimal toWrap = rand.Next() / 10.0m;
            var     val    = new CurrencyWrapper(toWrap);

            // Get and set property
            obj.Variant_Property = val;
            Assert.AreEqual(val.WrappedObject, obj.Variant_Property);
        }
Exemplo n.º 3
0
        public object Test_ObjectConversion_Currency(decimal value, Type targetType)
        {
            var cy     = new CurrencyWrapper(value);
            var result = VariantConverter.ChangeType(cy, targetType);

            if (result is DateTime dt)
            {
                return(dt.ToString(TheOneTrueDateFormat));
            }

            return(result);
        }
Exemplo n.º 4
0
 public InGameWrapper(UnityData unityData, IDeviceInput deviceInput)
 {
     instance        = this;
     logger          = new UnityLogger();
     camera          = GameObject.Find("Camera").GetComponent <Camera>();
     clockWrapper    = new ClockWrapper();
     resetLogic      = new ResetLogic();
     playersWrapper  = new PlayersWrapper(unityData.playerData, deviceInput);
     mapWrapper      = new MapWrapper(unityData.mapData, playersWrapper.GetOnlyLocalPlayer());
     spellsWrapper   = new SpellWrapper(unityData.spellData);
     cursorWrapper   = new CursorWrapper(unityData.cursorData);
     aimWrapper      = new AimWrapper(unityData.aimData, deviceInput);
     currencyWrapper = new CurrencyWrapper(unityData.currencyWrapper);
     EverythingSetupEvent();
 }
Exemplo n.º 5
0
        /// <summary>
        /// Use MNB Client to retrieve Available Currencies
        /// </summary>
        private void GetAndProcessAvailableCurrencies()
        {
            try
            {
                var currencyResponse = client.GetCurrencies(new GetCurrenciesRequestBody()).GetCurrenciesResult;

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(currencyResponse);

                XmlNodeList nodes = xmlDoc.SelectNodes(MNB_CURRENCIES_PER_CURRENCIES_PER_CURR);

                foreach (XmlNode node in nodes)
                {
                    // Skip if node is null or empty
                    if (string.IsNullOrWhiteSpace(node.InnerText))
                    {
                        continue;
                    }

                    CurrencyWrapper currencyWrapper = new CurrencyWrapper()
                    {
                        CurrencyName = node.InnerText
                    };

                    bool currencyAlreadyPresent = CurrenciesRetrieved.Any(curr => curr.CurrencyName == node.InnerText);
                    if (!currencyAlreadyPresent)
                    {
                        CurrenciesRetrieved.Add(currencyWrapper);
                    }
                }
            }
            catch (XmlException)
            {
                ShowErrorMessage("Could not parse the response from MNB while querying available currencies");
            }
            catch (System.Xml.XPath.XPathException e)
            {
                ShowErrorMessage($"XML structure is inconsistent with assumption. Could not retrieve structure of {MNB_CURRENCIES_PER_CURRENCIES_PER_CURR}");
            }
            catch (Exception e)
            {
                ShowErrorMessage(e.Message);
            }
        }
Exemplo n.º 6
0
        /// <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)
        {
            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 < 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
                if (null == args || 0 == args.Length)
                {
                    originalArgs = null;
                }
                else
                {
                    originalArgs = new COMWrapper[args.Length];
                    for (int 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 (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 = COMWrapper.Wrap(returnValue, returnType);
                        }
                    }
                    else if (returnType.IsEnum)
                    {
                        // Convert to proper Enum type
                        returnValue = Enum.Parse(returnType, returnValue.ToString());
                    }
                }

                // Handle out args
                if (0 != outArgsCount)
                {
                    outArgs = new object[args.Length];
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (!argModifiers[0][i])
                        {
                            continue;
                        }

                        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))
                            {
                                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));
        }
Exemplo n.º 7
0
    private unsafe static void TestByRef()
    {
        object obj;

        obj = (byte)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Byte(ref obj, NumericValue));

        obj = (sbyte)NumericValue;
        Assert.IsTrue(Marshal_ByRef_SByte(ref obj, (sbyte)NumericValue));

        obj = (short)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int16(ref obj, NumericValue));

        obj = (ushort)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt16(ref obj, NumericValue));

        obj = (int)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int32(ref obj, NumericValue));

        obj = (uint)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt32(ref obj, NumericValue));

        obj = (long)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int64(ref obj, NumericValue));

        obj = (ulong)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt64(ref obj, NumericValue));

        obj = (float)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Single(ref obj, NumericValue));

        obj = (double)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Double(ref obj, NumericValue));

        obj = StringValue;
        Assert.IsTrue(Marshal_ByRef_String(ref obj, StringValue));

        obj = new BStrWrapper(null);
        Assert.IsTrue(Marshal_ByRef_String(ref obj, null));

        obj = CharValue;
        Assert.IsTrue(Marshal_ByRef_Char(ref obj, CharValue));

        obj = true;
        Assert.IsTrue(Marshal_ByRef_Boolean(ref obj, true));

        obj = DateValue;
        Assert.IsTrue(Marshal_ByRef_DateTime(ref obj, DateValue));

        obj = DecimalValue;
        Assert.IsTrue(Marshal_ByRef_Decimal(ref obj, DecimalValue));

        obj = new CurrencyWrapper(DecimalValue);
        Assert.IsTrue(Marshal_ByRef_Currency(ref obj, DecimalValue));

        obj = DBNull.Value;
        Assert.IsTrue(Marshal_ByRef_Null(ref obj));

        obj = System.Reflection.Missing.Value;
        Assert.IsTrue(Marshal_ByRef_Missing(ref obj));

        obj = null;
        Assert.IsTrue(Marshal_ByRef_Empty(ref obj));

        obj = new object();
        Assert.IsTrue(Marshal_ByRef_Object(ref obj));

        obj = new UnknownWrapper(new object());
        Assert.IsTrue(Marshal_ByRef_Object_IUnknown(ref obj));

        obj = DecimalValue;
        Assert.IsTrue(Marshal_ChangeVariantType(ref obj, NumericValue));
        Assert.IsTrue(obj is int);
        Assert.AreEqual(NumericValue, (int)obj);
    }
Exemplo n.º 8
0
        /// <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;

            if (null == callMessage)
            {
                Log.Debug().WriteLine("Message type not implemented: {0}", myMessage.GetType());
                return(null);
            }

            var method = callMessage.MethodBase as MethodInfo;

            if (null == method)
            {
                Log.Debug().WriteLine("Unrecognized Invoke call: {0}", 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 removeHandler = methodName.StartsWith("remove_");
                methodName = methodName.Substring(removeHandler ? 7 : 4);
                // TODO: Something is missing here
                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;

                object[]      args;
                ParameterInfo parameter;
                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
                Type         byValType;
                ComWrapper   wrapper;
                ComWrapper[] originalArgs;
                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 (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]);
                            }
                        }
                    }
                }

                do
                {
                    try
                    {
                        returnValue = invokeType.InvokeMember(methodName, flags, null, invokeObject, args, argModifiers, null, null);
                        break;
                    }
                    catch (InvalidComObjectException icoEx)
                    {
                        // Should assist BUG-1616 and others
                        Log.Warn().WriteLine(
                            "COM object {0} has been separated from its underlying RCW cannot be used. The COM object was released while it was still in use on another thread.",
                            _interceptType.FullName);
                        return(new ReturnMessage(icoEx, callMessage));
                    }
                    catch (Exception ex)
                    {
                        // Test for rejected
                        var comEx = ex as COMException ?? ex.InnerException as COMException;
                        if (comEx != null && (comEx.ErrorCode == RPC_E_CALL_REJECTED || comEx.ErrorCode == RPC_E_FAIL))
                        {
                            var destinationName = _targetName;
                            // Try to find a "catchy" name for the rejecting application
                            if (destinationName != null && destinationName.Contains("."))
                            {
                                destinationName = destinationName.Substring(0, destinationName.IndexOf(".", StringComparison.Ordinal));
                            }
                            if (destinationName == null)
                            {
                                destinationName = _interceptType.FullName;
                            }

                            // TODO: Log destinationName and rejected information
                            Log.Error().WriteLine("Error while creating {0}: {1}", destinationName, comEx.ErrorCode);
                        }
                        // Not rejected OR pressed cancel
                        return(new ReturnMessage(ex, callMessage));
                    }
                } while (true);

                // 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, _targetName);
                        }
                    }
                    else if (returnType.IsEnum)
                    {
                        // Convert to proper Enum type
                        returnValue = Enum.Parse(returnType, returnValue.ToString());
                    }
                }

                // Handle out args
                if (0 != outArgsCount)
                {
                    outArgs = new object[args.Length];
                    for (var i = 0; i < parameters.Length; i++)
                    {
                        if (!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)
                        {
                            var currencyWrapper = arg as CurrencyWrapper;
                            if (currencyWrapper != null)
                            {
                                arg = currencyWrapper.WrappedObject;
                            }
                        }
                        else if (byValType.IsEnum)
                        {
                            arg = Enum.Parse(byValType, arg.ToString());
                        }
                        else if (byValType.IsInterface && Marshal.IsComObject(arg))
                        {
                            wrapper = originalArgs[i];
                            if (null != wrapper && wrapper._comObject != arg)
                            {
                                wrapper.Dispose();
                                wrapper = null;
                            }

                            if (null == wrapper)
                            {
                                wrapper = new ComWrapper(arg, byValType, _targetName);
                            }
                            arg = wrapper.GetTransparentProxy();
                        }
                        outArgs[i] = arg;
                    }
                }
            }

            return(new ReturnMessage(returnValue, outArgs, outArgsCount, callMessage.LogicalCallContext, callMessage));
        }
Exemplo n.º 9
0
        /// <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)
        {
            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)
            {
                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_")))
            {
                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 = _COMObject;
                invokeType   = _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 < 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
                if (null == args || 0 == args.Length)
                {
                    originalArgs = null;
                }
                else
                {
                    originalArgs = new COMWrapper[args.Length];
                    for (int 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 (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]);
                            }
                        }
                    }
                }

                do
                {
                    try
                    {
                        returnValue = invokeType.InvokeMember(methodName, flags, null, invokeObject, args, argModifiers, null, null);
                        break;
                    }
                    catch (Exception ex)
                    {
                        // Test for rejected
                        COMException comEx = ex as COMException;
                        if (comEx == null)
                        {
                            comEx = ex.InnerException as COMException;
                        }
                        if (comEx != null && (comEx.ErrorCode == RPC_E_CALL_REJECTED || comEx.ErrorCode == RPC_E_FAIL))
                        {
                            string destinationName = _TargetName;
                            // Try to find a "catchy" name for the rejecting application
                            if (destinationName != null && destinationName.Contains("."))
                            {
                                destinationName = destinationName.Substring(0, destinationName.IndexOf("."));
                            }
                            if (destinationName == null)
                            {
                                destinationName = _InterceptType.FullName;
                            }
                            DialogResult result = MessageBox.Show(Language.GetFormattedString("com_rejected", destinationName), Language.GetString("com_rejected_title"), MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                            if (result == DialogResult.OK)
                            {
                                continue;
                            }
                        }
                        // Not rejected OR pressed cancel
                        return(new ReturnMessage(ex, callMessage));
                    }
                } while (true);

                // 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, _TargetName);
                        }
                    }
                    else if (returnType.IsEnum)
                    {
                        // Convert to proper Enum type
                        returnValue = Enum.Parse(returnType, returnValue.ToString());
                    }
                }

                // Handle out args
                if (0 != outArgsCount)
                {
                    outArgs = new object[args.Length];
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (!argModifiers[0][i])
                        {
                            continue;
                        }

                        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))
                            {
                                wrapper = originalArgs[i];
                                if (null != wrapper && wrapper._COMObject != arg)
                                {
                                    wrapper.Dispose();
                                    wrapper = null;
                                }

                                if (null == wrapper)
                                {
                                    wrapper = new COMWrapper(arg, byValType, _TargetName);
                                }
                                arg = wrapper.GetTransparentProxy();
                            }
                        }
                        outArgs[i] = arg;
                    }
                }
            }

            return(new ReturnMessage(returnValue, outArgs, outArgsCount, callMessage.LogicalCallContext, callMessage));
        }
Exemplo n.º 10
0
        public void Ctor_ObjectValue(double value)
        {
            var wrapper = new CurrencyWrapper((object)(decimal)value);

            Assert.Equal((decimal)value, wrapper.WrappedObject);
        }
	// Test the CurrencyWrapper class.
	public void TestCurrencyWrapper()
			{
				CurrencyWrapper cw;

				// Test the (Decimal) constructor.
				cw = new CurrencyWrapper(123.45m);
				AssertEquals("CurrencyWrapper (1)", 123.45m, cw.WrappedObject);

				// Test the (Object) constructor.
				cw = new CurrencyWrapper((Object)(6123.45m));
				AssertEquals("CurrencyWrapper (2)", 6123.45m, cw.WrappedObject);

				// Test the failure cases of the (Object) constructor.
				try
				{
					cw = new CurrencyWrapper(null);
					Fail("CurrencyWrapper (3)");
				}
				catch(ArgumentException)
				{
					// Success.
				}
				try
				{
					cw = new CurrencyWrapper((Object)3);
					Fail("CurrencyWrapper (4)");
				}
				catch(ArgumentException)
				{
					// Success.
				}
			}
Exemplo n.º 12
0
    private unsafe static void TestByRef(bool hasComSupport)
    {
        object obj;

        obj = (byte)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Byte(ref obj, NumericValue));

        obj = (sbyte)NumericValue;
        Assert.IsTrue(Marshal_ByRef_SByte(ref obj, (sbyte)NumericValue));

        obj = (short)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int16(ref obj, NumericValue));

        obj = (ushort)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt16(ref obj, NumericValue));

        obj = (int)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int32(ref obj, NumericValue));

        obj = (uint)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt32(ref obj, NumericValue));

        obj = (long)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Int64(ref obj, NumericValue));

        obj = (ulong)NumericValue;
        Assert.IsTrue(Marshal_ByRef_UInt64(ref obj, NumericValue));

        obj = (float)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Single(ref obj, NumericValue));

        obj = (double)NumericValue;
        Assert.IsTrue(Marshal_ByRef_Double(ref obj, NumericValue));

        obj = StringValue;
        Assert.IsTrue(Marshal_ByRef_String(ref obj, StringValue));

        obj = new BStrWrapper(null);
        Assert.IsTrue(Marshal_ByRef_String(ref obj, null));

        obj = CharValue;
        Assert.IsTrue(Marshal_ByRef_Char(ref obj, CharValue));

        obj = true;
        Assert.IsTrue(Marshal_ByRef_Boolean(ref obj, true));

        obj = DateValue;
        Assert.IsTrue(Marshal_ByRef_DateTime(ref obj, DateValue));

        obj = DecimalValue;
        Assert.IsTrue(Marshal_ByRef_Decimal(ref obj, DecimalValue));

        obj = new CurrencyWrapper(DecimalValue);
        Assert.IsTrue(Marshal_ByRef_Currency(ref obj, DecimalValue));

        obj = DBNull.Value;
        Assert.IsTrue(Marshal_ByRef_Null(ref obj));

        obj = System.Reflection.Missing.Value;
        Assert.IsTrue(Marshal_ByRef_Missing(ref obj));

        obj = null;
        Assert.IsTrue(Marshal_ByRef_Empty(ref obj));

        if (hasComSupport)
        {
            obj = new object();
            Assert.IsTrue(Marshal_ByRef_Object(ref obj));

            obj = new UnknownWrapper(new object());
            Assert.IsTrue(Marshal_ByRef_Object_IUnknown(ref obj));

            obj = new GenerateIClassX();
            Assert.IsTrue(Marshal_ByRef_Object(ref obj));

            obj = new UnknownWrapper(new GenerateIClassX());
            Assert.IsTrue(Marshal_ByRef_Object_IUnknown(ref obj));
        }
        else
        {
            Assert.Throws <NotSupportedException>(
                () =>
            {
                obj = new object();
                Marshal_ByRef_Object(ref obj);
            },
                "Built-in COM has been disabled via a feature switch");
            Assert.Throws <NotSupportedException>(
                () =>
            {
                obj = new UnknownWrapper(new object());
                Marshal_ByRef_Object_IUnknown(ref obj);
            },
                "Built-in COM has been disabled via a feature switch");
        }

        obj = DecimalValue;
        Assert.IsTrue(Marshal_ChangeVariantType(ref obj, NumericValue));
        Assert.IsTrue(obj is int);
        Assert.AreEqual(NumericValue, (int)obj);
    }
Exemplo n.º 13
0
 public static extern void GetWrappedCurrencyForInt(int i, out CurrencyWrapper currency);
Exemplo n.º 14
0
 public static extern bool ValidateAndChangeWrappedCurrency(ref CurrencyWrapper currency, int expected, int newValue);
Exemplo n.º 15
0
 public static extern bool WrappedCurrencyEqualToInt(CurrencyWrapper currency, int i);
Exemplo n.º 16
0
        public object Test_ObjectConversion_Currency_Localized(decimal value, Type targetType, string locale)
        {
            var cy = new CurrencyWrapper(value);

            return(VariantConverter.ChangeType(cy, targetType, new CultureInfo(locale)));
        }