//
        // System callbacks

        public override bool OnHookInstalled(bool success,
                                             Exception hookEx = null)
        {
            if (hookEx != null)
            {
                LogTo.Error(hookEx,
                            "InjectionLib threw an error during initialization.");
            }

            try
            {
                LogTo.Debug($"Injected lib signal, success: {success}");

                HookSuccess   = success;
                HookException = hookEx;

                HookInitEvent.Set();

                return(SMAInitEvent.WaitOne(WaitTimeout));
            }
            catch (Exception ex)
            {
                LogTo.Error(ex,
                            "Failed to Signal InitEvent for Hook Install Success");

                return(false);
            }
        }
Beispiel #2
0
        //
        // System callbacks

        public override bool OnHookInstalled(bool success,
                                             Exception hookEx = null)
        {
            if (hookEx != null)
            {
                OnException(hookEx);
            }

            try
            {
                LogTo.Debug("Injected lib signal, success: {Success}", success);

                HookSuccess   = success;
                HookException = hookEx;

                HookInitEvent.Set();

                return(SMAInitEvent.WaitOne(WaitTimeout));
            }
            catch (Exception ex)
            {
                LogTo.Error(ex, "Failed to Signal InitEvent for Hook Install Success");

                return(false);
            }
        }
        //
        // Core hook methods

        public async Task <IProcess> CreateAndHook(
            SMCollection collection,
            string binPath,
            ISMAHookSystem systemCallback,
            IEnumerable <ISMAHookIO> ioCallbacks)
        {
            LogTo.Debug("Starting and injecting SuperMemo");

            SystemCallback = systemCallback;
            IOCallbacks.AddRange(ioCallbacks);

            IOTargetFilePaths.AddRange(IOCallbacks.SelectMany(c => c.GetTargetFilePaths()));

            HookSuccess   = false;
            HookException = null;

            // Start a new IPC server
            var channelName = StartIPCServer();

            // Start SuperMemo application with given collection as parameter,
            // and immediatly install hooks
            RemoteHooking.CreateAndInject(
                binPath,
                collection.GetKnoFilePath().Quotify(),
                0,
                InjectionOptions.Default,
                SMAFileSystem.InjectionLibFile.FullPath,
                null,
                out var pId,
                channelName
                );

            LogTo.Debug("Waiting for signal from Injected library");

            // Wait for Signal from OnHookInstalled with timeout
            await HookInitEvent.WaitAsync(WaitTimeout);

            if (HookSuccess == false)
            {
                LogTo.Debug("Hook failed, aborting");

                StopIPCServer();

                var ex = new HookException("Hook setup failed: " + HookException?.Message,
                                           HookException);
                HookException = null;

                throw ex;
            }

            LogTo.Debug($"SuperMemo started and injected, pId: {pId}");

            return(new ProcessSharp <SM17Natives>(
                       pId,
                       Process.NET.Memory.MemoryType.Remote,
                       true,
                       SMA.SMA.Instance.StartupConfig.PatternsHintAddresses));
        }
Beispiel #4
0
        //
        // Core hook methods

        public async Task <ProcessSharp <SMNatives> > CreateAndHookAsync(
            SMCollection collection,
            string binPath,
            IEnumerable <ISMAHookIO> ioCallbacks,
            NativeData nativeData)
        {
            LogTo.Debug("Starting and injecting SuperMemo");

            IOCallbacks.AddRange(ioCallbacks);

            IOTargetFilePaths.AddRange(IOCallbacks.SelectMany(c => c.GetTargetFilePaths()));

            HookSuccess   = false;
            HookException = null;

            // Start a new IPC server
            var channelName = StartIPCServer();

            // Start SuperMemo application with given collection as parameter,
            // and immediately install hooks
            int pId = -1;

            try
            {
                RemoteHooking.CreateAndInject(
                    binPath,
                    collection.GetKnoFilePath().Quotify(),
                    0,
                    InjectionOptions.Default,
                    SMAFileSystem.InjectionLibFile.FullPath,
                    null,
                    out pId,
                    channelName,
                    nativeData
                    );
            }
            catch (ArgumentException ex)
            {
                LogTo.Warning(ex, "Failed to start and inject SuperMemo. Command line: '{BinPath} {V}'", binPath, collection.GetKnoFilePath().Quotify());
            }

            LogTo.Debug("Waiting for signal from Injected library");

            // Wait for Signal from OnHookInstalled with timeout
            await HookInitEvent.WaitAsync(WaitTimeout).ConfigureAwait(false);

            if (HookSuccess == false)
            {
                LogTo.Debug("Hook failed, aborting");

                StopIPCServer();

                var ex = new HookException("Hook setup failed: " + HookException?.Message,
                                           HookException);
                HookException = null;

                throw ex;
            }

            LogTo.Debug("SuperMemo started and injected, pId: {PId}", pId);

            return(new ProcessSharp <SMNatives>(
                       pId,
                       Process.NET.Memory.MemoryType.Remote,
                       true,
                       Core.CoreConfig.SuperMemo.PatternsHintAddresses,
                       nativeData));
        }