DrDavid Bowman

DrDavid Bowman

  • 0
  • 5
  • 5.1k

Loading up an External EXE Program into Memory and Run it

Aug 18 2019 6:48 PM
A very good day to you All,

I hope you are doing Very Well,

Please kindly see the attached code for a C# Program

This C# Program will Load an External EXE Program into Memory

This C# Program will then do a Function Call to Run that EXE Program that sits in Memory

The External EXE Program is an Executable that sending Keypress to the Desktop 

It does nothing except sending out keypresses to the Desktop

The Calling Function to Run the EXE From Memory is listed below
  1. //Execute the assembly
    exeAssembly.EntryPoint.Invoke(null, null); //no parameters
Is there any way for me to call the above function as many times as I want so that I can keep on running the EXE from memory as many times as I want ?

The below code works for the 1st call to run the EXE

On the second call to run the EXE - The code returns an error message and is not able to proceed any further

What I would like to be able to do is to 

1) Load the EXE into Memory

2) Set it up in such a way where I can Call the Invoke Function as many times as I wish in order to Run the External Keypress Program as many times as I wish

Is there any way to do this ?

Secondly I would like for the external EXE program to be able to interact with all the surrounding applications that are running on the Windows Desktop

This is because the external EXE program is actually a program that sends out keypresses

I am calling and running this external EXE program multiple times in order to deliver multiple keypresses many many times over

The keypresses are to interact with any Running APP that I have opened in the Windows desktop

And thus I would like for this keypresses app to be able to affect and interact with all the apps on the Windows desktop

How can I modify the code so that I can allow for this external EXE to be able to interact with all running desktop apps?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Windows.Forms;  
  4. //Needed  
  5. using System.Reflection;  
  6. using System.IO;  
  8. namespace MemoryLauncher  
  9. {  
  10. static class Program  
  11. {  
  12.     ///   
  13.     /// The main entry point for the application.  
  14.     ///   
  15.     [STAThread]  
  16.     static void Main()  
  17.     {  
  18.         RunInternalExe("x.exe");  
  19.     }  
  21.     private static void RunInternalExe(string exeName)  
  22.     {  
  23.         //Get the current assembly  
  24.         Assembly assembly = Assembly.GetExecutingAssembly();  
  26.         //Get the assembly's root name  
  27.         string rootName = assembly.GetName().Name;  
  29.         //Get the resource stream  
  30.         Stream resourceStream = assembly.GetManifestResourceStream(rootName + "." + exeName);  
  32.         //Verify the internal exe exists  
  33.         if (resourceStream == null)  
  34.             return;  
  36.         //Read the raw bytes of the resource  
  37.         byte[] resourcesBuffer = new byte[resourceStream.Length];  
  39.         resourceStream.Read(resourcesBuffer, 0, resourcesBuffer.Length);  
  40.         resourceStream.Close();  
  42.         //Load the bytes as an assembly  
  43.         Assembly exeAssembly = Assembly.Load(resourcesBuffer);  
  45.         //Execute the assembly  
  46.         exeAssembly.EntryPoint.Invoke(nullnull); //no parameters  
  47.     }  
  48. }  
  49. }  

Answers (3)