コード例 #1
0
        private static GCHandle PrepareInjection(
            ManagedRemoteInfo InRemoteInfo,
            ref String InLibraryPath_x86,
            ref String InLibraryPath_x64,
            MemoryStream InPassThruStream)
        {
            // ensure full path information in case of file names...
            if ((InLibraryPath_x86 != null) && File.Exists(InLibraryPath_x86))
            {
                InLibraryPath_x86 = Path.GetFullPath(InLibraryPath_x86);
            }

            if ((InLibraryPath_x64 != null) && File.Exists(InLibraryPath_x64))
            {
                InLibraryPath_x64 = Path.GetFullPath(InLibraryPath_x64);
            }

            /*
             *  validate assembly name...
             */
            Assembly UserAsm;

            InRemoteInfo.UserLibrary = InLibraryPath_x86;

            if (NativeAPI.Is64Bit)
            {
                InRemoteInfo.UserLibrary = InLibraryPath_x64;
            }

            if (File.Exists(InRemoteInfo.UserLibrary))
            {
                // translate to assembly name
                InRemoteInfo.UserLibrary = AssemblyName.GetAssemblyName(InRemoteInfo.UserLibrary).FullName;
            }

            if ((UserAsm = Assembly.ReflectionOnlyLoad(InRemoteInfo.UserLibrary)) == null)
            {
                throw new DllNotFoundException("The given assembly could not be found.");
            }

            if ((Int32)(UserAsm.GetName().Flags & AssemblyNameFlags.PublicKey) == 0)
            {
                throw new ArgumentException("The given assembly has no strong name.");
            }

            /*
             *  Convert managed arguments to binary stream...
             */

            BinaryFormatter  Format  = new BinaryFormatter();
            IpcServerChannel Channel = IpcCreateServer <HelperServiceInterface>(
                ref InRemoteInfo.ChannelName,
                WellKnownObjectMode.Singleton);

            Format.Serialize(InPassThruStream, InRemoteInfo);

            return(GCHandle.Alloc(InPassThruStream.GetBuffer(), GCHandleType.Pinned));
        }
コード例 #2
0
        private static GCHandle PrepareInjection(
            ManagedRemoteInfo InRemoteInfo,
            ref String InLibraryPath_x86,
            ref String InLibraryPath_x64,
            MemoryStream InPassThruStream)
        {
            if (String.IsNullOrEmpty(InLibraryPath_x86) && String.IsNullOrEmpty(InLibraryPath_x64))
            {
                throw new ArgumentException("At least one library for x86 or x64 must be provided");
            }

            // ensure full path information in case of file names...
            if ((InLibraryPath_x86 != null) && File.Exists(InLibraryPath_x86))
            {
                InLibraryPath_x86 = Path.GetFullPath(InLibraryPath_x86);
            }

            if ((InLibraryPath_x64 != null) && File.Exists(InLibraryPath_x64))
            {
                InLibraryPath_x64 = Path.GetFullPath(InLibraryPath_x64);
            }

            /*
             *  validate assembly name...
             */
            Assembly UserAsm;

            InRemoteInfo.UserLibrary = InLibraryPath_x86;

            if (NativeAPI.Is64Bit)
            {
                InRemoteInfo.UserLibrary = InLibraryPath_x64;
            }

            if (File.Exists(InRemoteInfo.UserLibrary))
            {
                // translate to assembly name
                InRemoteInfo.UserLibraryName = AssemblyName.GetAssemblyName(InRemoteInfo.UserLibrary).FullName;
            }

            // Attempt to load the library by its FullName and if that fails, by its original library filename
            if ((UserAsm = Assembly.ReflectionOnlyLoad(InRemoteInfo.UserLibraryName)) == null && (UserAsm = Assembly.ReflectionOnlyLoadFrom(InRemoteInfo.UserLibrary)) == null)
            {
                throw new DllNotFoundException("The given assembly could not be found.");
            }

            // Check for a strong name if necessary
            if (InRemoteInfo.RequireStrongName && (Int32)(UserAsm.GetName().Flags & AssemblyNameFlags.PublicKey) == 0)
            {
                throw new ArgumentException("The given assembly has no strong name.");
            }

            /*
             *  Convert managed arguments to binary stream...
             */

            var Format  = new BinaryFormatter();
            var Channel = IpcCreateServer <HelperServiceInterface>(
                ref InRemoteInfo.ChannelName,
                WellKnownObjectMode.Singleton);

            Format.Serialize(InPassThruStream, InRemoteInfo);

            return(GCHandle.Alloc(InPassThruStream.GetBuffer(), GCHandleType.Pinned));
        }