Example #1
0
    public static void CallChainFromManaged(int a, int b)
    {
        ArithmeticOperationDelegate latebound;

        try
        {
            // Simplest: Just DllImport ...
            Log(string.Format("{0} + {1} = {2}", a, b, AddTwoNumbersInNative(a, b)));

            // not with a callback..
            Log(string.Format("{0} - {1} = {2}", a, b, SubtractTwoNumbersUsingInlineCallback(a, b, SubtractTwoNumbersInManagedCode)));

            // This one function is just a DllImport too.. (like Addition)
            // but in this case the function can also be called from C++.
            Log(string.Format("{0} * {1} = {2}", a, b, MultiplyTwoNumbersUsingExportedFunction(a, b)));

            //now we use a latebound function.
            // Gotcha here is that this does not do great error handling..  this might throw if the latebound method pointer is not set
            latebound = GetLateBoundDivison();
            Log(string.Format("{0} / {1} = {2}", a * b, b, latebound(a * b, b)));


            MActivationParamsAsParam activationParams = new MActivationParamsAsParam();
            activationParams.commandLineBufferSize = 1024;
            activationParams.commandLine           = new String('\0', activationParams.commandLineBufferSize);
            if (TryGetActivationParams(ref activationParams))
            {
                Log(string.Format("is First: {0}, params:{1}", activationParams.isFirstActivation, activationParams.commandLine));
            }

            MActivationParamsAsReturnValue activationParams2 = GetActivationParams();
            if (activationParams2.commandLinePtr != IntPtr.Zero)
            {
                string commandLine = Marshal.PtrToStringUni(activationParams2.commandLinePtr);
                int    targetLevel = 1;
                bool   isRoomScale = false;
                if (!TryParseCommandLine(commandLine))
                {
                    Log("Failed to parse: " + commandLine);
                }
            }
            else
            {
                Log("No commandline ");
            }
        }
        catch (EntryPointNotFoundException eeh)
        {
            Log(string.Format("{0} was not found. If you are trying to use it in UNITY_EDITOR, use a plugin for this", eeh.Message));
        }

        // Another approach to late binding.
        // This is safer on checks if your function is not guaranteed to exist.
        try
        {
            IntPtr lateboundSubtractionPtr = GetLateBoundSubtractionAddress();
            if (lateboundSubtractionPtr != IntPtr.Zero)
            {
                ArithmeticOperationDelegate lateboundSubtraction = Marshal.GetDelegateForFunctionPointer(lateboundSubtractionPtr, typeof(ArithmeticOperationDelegate)) as ArithmeticOperationDelegate;
                if (lateboundSubtraction != null)
                {
                    Log(string.Format("{0} - {1} = {2}", a, b, lateboundSubtraction(a, b)));
                }
            }
            else
            {
                Log("Subtraction was not defined");
            }
        }
        catch (Exception ex)
        {
            Log(ex.Message);
        }


        SetComparisonCallback(CompareManagedCallback);
    }
Example #2
0
 extern static bool TryGetActivationParams(ref MActivationParamsAsParam p);