简体   繁体   中英

How to embed and run .exe from C# or C/C++

I have .exe file (I created it many years ago in C++), I want to run this .exe from my new program.exe, which is written in C#/C++(Console Aplication). Can I embed old .exe to new code(C++/C#) and create one new .exe, which will include old one?

Edit: I can run from C++ with:

system("path\\to\\exe");

or in C#:

Process.Start(path);

But both use path, I need to embed into new .exe

Start by embedding it as a resource inside the application. Once there, the binary data from it can either be accessed and loaded into the application dynamically and you can access the EAT (Export Address Table) to find the functions you need to use. This marshalling is difficult in C# but you can just choose to extract the application. I believe to keep it simple, this may satisfy your needs.

void ExtractAndRun(string assemblyResource)
{
    var tempPath = Path.Combine(Environment.CurrentDirectory, "uapweaelsirx.exe");
    var res = GetType().Assembly.GetManifestResourceStream(assemblyResource);
    var fileBuffer = new byte[(int)stream.Length];
    res.Read(fileBuffer, 0, fileBuffer.Length);
    File.WriteAllBytes(tempPath, fileBuffer);

    Process.Start(tempPath);
}

If your process doesn't run terribly long or you just wish to incorporate it for the sake of neatness to your users, this solution may be an acceptable one to choose. If not, comment and I shall provide an alternative solution with edit. Alternatively downvote and I'll post another solution in a new answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM