🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Loading an External EXE Program into Memory and Calling/Running it as many times as I wish

Started by
2 comments, last by Adam Miles 4 years, 10 months ago

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?

Here is the code


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

 

Advertisement

Have you tried using Process?
C# Process.Start()

Pretty sure you can set up startInfo and call it repeatedly and wait for a retrun event. And MS has a post for simulated key presses which you can combine with FindWindow().

Simulated Key Presses

This doesn't have anything to do with Graphics or GPU programming.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

This topic is closed to new replies.

Advertisement