Ejemplo n.º 1
0
 /// <summary>
 ///     Terminates the thread.
 /// </summary>
 /// <param name="exitCode">The exit code of the thread to close.</param>
 public void Terminate(int exitCode = 0)
 {
     if (IsAlive)
     {
         AThreadHelper.TerminateThread(Handle, exitCode);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Gets the termination status of the thread.
        /// </summary>
        public T GetExitCode <T>()
        {
            // Get the exit code of the thread (can be nullable)
            var ret = AThreadHelper.GetExitCodeThread(Handle);

            // Return the exit code or the default value of T if there's no exit code
            return(ret.HasValue ? MarshalType <T> .PtrToObject(ProcessPlus, ret.Value) : default(T));
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Either suspends the thread, or if the thread is already suspended, has no effect.
 /// </summary>
 /// <returns>A new instance of the <see cref="AFrozenThread" /> class. If this object is disposed, the thread is resumed.</returns>
 public IAFrozenThread Suspend()
 {
     if (!IsAlive)
     {
         return(null);
     }
     AThreadHelper.SuspendThread(Handle);
     return(new AFrozenThread(this));
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ARemoteThread" /> class.
 /// </summary>
 /// <param name="processPlus">The reference of the <see cref="AProcessSharp" /> object.</param>
 /// <param name="thread">The native <see cref="ProcessThread" /> object.</param>
 public ARemoteThread(AProcessSharp processPlus, ProcessThread thread)
 {
     // Save the parameters
     ProcessPlus = processPlus;
     Native      = thread;
     // Save the thread id
     Id = thread.Id;
     // Open the thread
     Handle = AThreadHelper.OpenThread(ThreadAccessFlags.AllAccess, Id);
 }
Ejemplo n.º 5
0
        /// <summary>
        ///     Resumes a thread that has been suspended.
        /// </summary>
        public void Resume()
        {
            // Check if the thread is still alive
            if (!IsAlive)
            {
                return;
            }

            // Start the thread
            AThreadHelper.ResumeThread(Handle);

            // Start a task to clean the memory used by the parameter if we created the thread
            if (_parameter != null && !_parameterCleaner.IsCompleted)
            {
                _parameterCleaner.Start();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Creates a thread that runs in the remote process.
        /// </summary>
        /// <param name="address">
        ///     A pointer to the application-defined function to be executed by the thread and represents
        ///     the starting address of the thread in the remote process.
        /// </param>
        /// <param name="isStarted">Sets if the thread must be started just after being created.</param>
        /// <returns>A new instance of the <see cref="ARemoteThread" /> class.</returns>
        public IARemoteThread Create(IntPtr address, bool isStarted = true)
        {
            //Create the thread
            var ret = AThreadHelper.NtQueryInformationThread(
                AThreadHelper.CreateRemoteThread(Process.Handle, address, IntPtr.Zero, ThreadCreationFlags.Suspended));

            // Find the managed object corresponding to this thread
            // todo should thread id be an intpr?
            var result = new ARemoteThread(Process, Process.ThreadFactory.NativeThreads.First(t => t.Id == ret.ClientId.UniqueThread.ToInt32()));

            // If the thread must be started
            if (isStarted)
            {
                result.Resume();
            }
            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Creates a thread that runs in the remote process.
        /// </summary>
        /// <param name="address">
        ///     A pointer to the application-defined function to be executed by the thread and represents
        ///     the starting address of the thread in the remote process.
        /// </param>
        /// <param name="parameter">A variable to be passed to the thread function.</param>
        /// <param name="isStarted">Sets if the thread must be started just after being created.</param>
        /// <returns>A new instance of the <see cref="ARemoteThread" /> class.</returns>
        public IARemoteThread Create(IntPtr address, dynamic parameter, bool isStarted = true)
        {
            // Marshal the parameter
            var marshalledParameter = AMarshalValue.Marshal(Process, parameter);

            //Create the thread
            var ret = AThreadHelper.NtQueryInformationThread(
                AThreadHelper.CreateRemoteThread(Process.Handle, address, marshalledParameter.Reference,
                                                 ThreadCreationFlags.Suspended));

            // Find the managed object corresponding to this thread
            var result = new ARemoteThread(Process, Process.ThreadFactory.NativeThreads.First(t => t.Id == ret.ThreadId),
                                           marshalledParameter);

            if (isStarted)
            {
                result.Resume();
            }
            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Gets the linear address of a specified segment.
        /// </summary>
        /// <param name="segment">The segment to get.</param>
        /// <returns>A <see cref="IntPtr" /> pointer corresponding to the linear address of the segment.</returns>
        public IntPtr GetRealSegmentAddress(SegmentRegisters segment)
        {
            // Get a selector entry for the segment
            LdtEntry entry;

            switch (segment)
            {
            case SegmentRegisters.Cs:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegCs);
                break;

            case SegmentRegisters.Ds:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegDs);
                break;

            case SegmentRegisters.Es:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegEs);
                break;

            case SegmentRegisters.Fs:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegFs);
                break;

            case SegmentRegisters.Gs:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegGs);
                break;

            case SegmentRegisters.Ss:
                entry = AThreadHelper.GetThreadSelectorEntry(Handle, Context.SegSs);
                break;

            default:
                throw new InvalidEnumArgumentException("segment");
            }
            // Compute the linear address
            return(new IntPtr(entry.BaseLow | (entry.BaseMid << 16) | (entry.BaseHi << 24)));
        }
Ejemplo n.º 9
0
 /// <summary>
 ///     Blocks the calling thread until a thread terminates or the specified time elapses.
 /// </summary>
 /// <param name="time">The timeout.</param>
 /// <returns>The return value is a flag that indicates if the thread terminated or if the time elapsed.</returns>
 public WaitValues Join(TimeSpan time)
 {
     return(AThreadHelper.WaitForSingleObject(Handle, time));
 }
Ejemplo n.º 10
0
 /// <summary>
 ///     Blocks the calling thread until the thread terminates.
 /// </summary>
 public void Join()
 {
     AThreadHelper.WaitForSingleObject(Handle);
 }