Ejemplo n.º 1
0
        /// <summary>
        /// Loads <see cref="ConnectionData"/> from the <see cref="IntPtr"/> specified.
        /// </summary>
        /// <param name="unmanagedInfoPointer"></param>
        /// <param name="formatter"></param>
        public static ConnectionData <T, U> LoadData(
            IntPtr unmanagedInfoPointer,
            IUserDataFormatter formatter)
        {
            var data = new ConnectionData <T, U>
            {
                State         = ConnectionState.Valid,
                UnmanagedInfo = (T)Activator.CreateInstance(typeof(T))
            };

            try
            {
                // Get the unmanaged data containing the remote user parameters
                Marshal.PtrToStructure(unmanagedInfoPointer, data.UnmanagedInfo);

                // Deserialize user data class passed to CoreLoad
                data.RemoteInfo = formatter.Deserialize <U>(
                    data.UnmanagedInfo.UserData,
                    data.UnmanagedInfo.UserDataSize);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.ToString());
            }
            return(data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads <see cref="PluginConfiguration{T,U}"/> from the <see cref="IntPtr"/> specified
        /// in <paramref name="unmanagedInfoPointer"/>.
        /// </summary>
        /// <param name="unmanagedInfoPointer">Pointer to the user arguments data.</param>
        /// <param name="formatter">Deserializer for the user arguments data.</param>
        public static PluginConfiguration <T, U> LoadData(
            IntPtr unmanagedInfoPointer,
            IUserDataFormatter formatter)
        {
            var data = new PluginConfiguration <T, U>
            {
                State         = PluginInitializationState.Loading,
                UnmanagedInfo = (T)Activator.CreateInstance(typeof(T))
            };

            try
            {
                // Get the unmanaged data containing the remote user parameters
                Marshal.PtrToStructure(unmanagedInfoPointer, data.UnmanagedInfo);

                // Deserialize user data class passed to CoreLoad
                data.RemoteInfo = formatter.Deserialize <U>(
                    data.UnmanagedInfo.UserData,
                    data.UnmanagedInfo.UserDataSize);
            }
            catch (Exception e)
            {
                data.State = PluginInitializationState.Failed;
                Debug.WriteLine(e.ToString());
            }
            return(data);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Extract serialized parameters from a stream into a parameter list.
 /// </summary>
 /// <param name="paramArray">The list to store the extracted parameters to.</param>
 /// <param name="formatter">Extracts serialized objects into their original type.</param>
 private static void DeserializeParameters(object[] paramArray, IUserDataFormatter formatter)
 {
     for (int i = 1; i < paramArray.Length; ++i)
     {
         using (Stream ms = new MemoryStream((byte[])paramArray[i]))
         {
             paramArray[i] = formatter.Deserialize <object>(ms);
         }
     }
 }
Ejemplo n.º 4
0
        public void ShouldSerializeAndDeserializeClassWithStream()
        {
            IUserDataFormatter formatter          = CreateFormatter();
            const int          integerMemberValue = 1;

            using (Stream serializationStream = new MemoryStream())
            {
                var objectToSerialize = new UserDataFormatterTestClass {
                    IntegerMember = integerMemberValue
                };
                formatter.Serialize(serializationStream, objectToSerialize);

                Assert.NotEqual(0, serializationStream.Length);

                serializationStream.Position = 0;
                var deserializedObject = formatter.Deserialize <UserDataFormatterTestClass>(serializationStream);

                Assert.NotNull(deserializedObject);
                Assert.Equal(integerMemberValue, deserializedObject.IntegerMember);
            }
        }
Ejemplo n.º 5
0
        private static void LoadUserLibrary(Assembly assembly, object[] paramArray, string helperPipeName, IUserDataFormatter formatter)
        {
            Type entryPoint = FindEntryPoint(assembly);

            for (int i = 1; i < paramArray.Length; ++i)
            {
                using (Stream ms = new MemoryStream((byte[])paramArray[i]))
                {
                    paramArray[i] = formatter.Deserialize <object>(ms);
                }
            }

            MethodInfo runMethod = FindMatchingMethod(entryPoint, EntryPointMethodName, paramArray);

            if (runMethod == null)
            {
                throw new MissingMethodException($"Failed to find the function 'Run' in {assembly.FullName}");
            }

            var instance = InitializeInstance(entryPoint, paramArray);

            if (instance == null)
            {
                throw new MissingMethodException($"Failed to find the constructor {entryPoint.Name} in {assembly.FullName}");
            }

            SendInjectionComplete(helperPipeName, Process.GetCurrentProcess().Id);
            try
            {
                // Execute the CoreHook plugin entry point
                runMethod.Invoke(instance, BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding |
                                 BindingFlags.InvokeMethod, null, paramArray, null);
            }
            finally
            {
                Release(entryPoint);
            }
        }