private static int __CLR_CALLFUNCTION__(string pwzArgument) { IntPtr ParamaterPtr = new IntPtr(Convert.ToInt64(pwzArgument, 16)); using (DllInjector Injector = DllInjector.GetCurrentProcess()) { Injector.AcquireProcessHandle(); CLRCallFunctionParam P = new CLRCallFunctionParam(); try { byte[] Bytes = Injector.ReadPrefixedBytes(Injector.ReadIntPtr(ParamaterPtr)); using (MemoryStream MS = new MemoryStream(Bytes)) P = (CLRCallFunctionParam)__CLR_SERIALIZER__.Deserialize(MS); Type[] ParamaterTypes = new Type[P.ParamaterAssemblies.Length]; for (int i = 0; i < ParamaterTypes.Length; i++) { ParamaterTypes[i] = LoadAssemblySerializationBinder.Default.BindToType(P.ParamaterAssemblies[i], P.ParamaterDeclaringTypes[i]); } P.ReturnValue = LoadAssemblySerializationBinder .Default .BindToType(P.Assembly, P.DeclaringType) .GetMethod(P.Function, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, ParamaterTypes, null) .Invoke(null, P.Paramaters); } catch (Exception ex) { P.Exception = ex; } using (MemoryStream MS = new MemoryStream()) { __CLR_SERIALIZER__.Serialize(MS, P); Injector.WriteIntPtr(ParamaterPtr, Injector.WritePrefixedBytes(MS.ToArray())); } } return((int)Win32.ERROR_SUCCESS); }
private object CallManagedFunction(string AssemblyPath, string DeclaringType, string FunctionName, object[] Paramaters, Type[] ParamaterTypes) { if (string.IsNullOrEmpty(AssemblyPath)) { throw new ArgumentNullException("AssemblyPath"); } if (string.IsNullOrEmpty(DeclaringType)) { throw new ArgumentNullException("DeclaringType"); } if (string.IsNullOrEmpty(FunctionName)) { throw new ArgumentNullException("FunctionName"); } this.ThrowIfDisposed(); this.ThrowIfNoHandle(); this.ThrowIfNoCLR(); if (Paramaters == null) { Paramaters = new object[0]; } if (ParamaterTypes == null) { ParamaterTypes = Type.GetTypeArray(Paramaters); } CLRCallFunctionParam P = new CLRCallFunctionParam() { Assembly = AssemblyPath, DeclaringType = DeclaringType, Function = FunctionName, ParamaterAssemblies = new string[ParamaterTypes.Length], ParamaterDeclaringTypes = new string[ParamaterTypes.Length], Paramaters = Paramaters }; string ParamaterAssembly; string ParamaterDeclaringType; for (int i = 0; i < ParamaterTypes.Length; i++) { LoadAssemblySerializationBinder.Default.BindToName(ParamaterTypes[i], out ParamaterAssembly, out ParamaterDeclaringType); P.ParamaterAssemblies[i] = ParamaterAssembly ?? ParamaterTypes[i].Assembly.FullName; P.ParamaterDeclaringTypes[i] = ParamaterDeclaringType ?? ParamaterTypes[i].FullName; } MemoryHandle BytesPtr; using (MemoryStream MS = new MemoryStream()) { __CLR_SERIALIZER__.Serialize(MS, P); BytesPtr = this.WritePrefixedBytes(MS.ToArray()); } using (BytesPtr) using (MemoryHandle ParamaterPtr = this.WriteIntPtr(BytesPtr)) { uint res = (uint)this.CallManagedFunctionThreadProc(__CLR_CALLFUNCTION__, ParamaterPtr.ToString("X16")); if (res != Win32.ERROR_SUCCESS) { throw new Exception(string.Format("__CLR_CALLFUNCTION__ failed, with result {0}", res)); } using (MemoryHandle NewBytesPtr = this.ReadMemoryHandle(ParamaterPtr)) using (MemoryStream MS = new MemoryStream(this.ReadPrefixedBytes(NewBytesPtr))) P = (CLRCallFunctionParam)__CLR_SERIALIZER__.Deserialize(MS); } if (P.Exception != null) { Utilities.PreserveStackTrace(P.Exception); throw P.Exception; } Array.Copy(P.Paramaters, Paramaters, Paramaters.Length); return(P.ReturnValue); }