none
Loading an External EXE Program into Memory and Calling/Running it as Many Times as I Wish - Not Working - Please Kindly Help Sir RRS feed

  • Question

  • 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

        //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?

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    //Needed
    using System.Reflection;
    using System.IO;
    
    namespace MemoryLauncher
    {
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            RunInternalExe("x.exe");
        }
    
        private static void RunInternalExe(string exeName)
        {
            //Get the current assembly
            Assembly assembly = Assembly.GetExecutingAssembly();
    
            //Get the assembly's root name
            string rootName = assembly.GetName().Name;
    
            //Get the resource stream
            Stream resourceStream = assembly.GetManifestResourceStream(rootName + "." + exeName);
    
            //Verify the internal exe exists
            if (resourceStream == null)
                return;
    
            //Read the raw bytes of the resource
            byte[] resourcesBuffer = new byte[resourceStream.Length];
    
            resourceStream.Read(resourcesBuffer, 0, resourcesBuffer.Length);
            resourceStream.Close();
    
            //Load the bytes as an assembly
            Assembly exeAssembly = Assembly.Load(resourcesBuffer);
    
            //Execute the assembly
            exeAssembly.EntryPoint.Invoke(null, null); //no parameters
        }
    }
    }


    Sunday, August 18, 2019 11:26 PM

All replies

  • Hi andykheemeng,

    Thank you for posting here and apologize for the delayed response.

    According to your description, I have some questions to confirm with you.

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

    I use while loop to make a test on my side, but I get no error.

    How do you run the EXE on the second call? Could you provide some details about the errors?

    >>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 

    What kind of interaction do you need?

    Please provide more informations, and it will help us to analyze your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 29, 2019 6:41 AM
    Moderator