问题
I know that is strange situation, but I need to embed an EXE file (or the assembly code) into my project, so it can be started only by the application (it can't create the EXE in the filesystem and start it)...
Is it possible?
Edit:
It's not a .NET EXE. Anyway I added the Test.exe file as a resource to my project and I did this
Dim exestr As Stream = Nothing
Dim a As Assembly = Assembly.GetExecutingAssembly
exestr = a.GetManifestResourceStream("Test.exe")
回答1:
I'm thinking create a RAM disk, write your exe file/s to it from your .NET resource file, and execute the file there. Nothing touches physical disk.
I don't have a link for a programmable RAM disk API but you can likely find something to manipulate from your program. The other benefit is virus scanners can still acknowledge it for security.
Also I'm thinking if your app is running on disk then there's no good reason why the executable in question can't be on disk too. If you're piping it in over the network (downloading) it or something, then save to disk first and erase it when done execution.
回答2:
Not possible. This was a core design decision made by Dave Cutler and co when they designed Windows NT. A process must be backed by a file on the file system. Windows uses a memory mapped file to map the code of the process to virtual memory. This is not different for a .NET program, even though a lot of its code gets JIT compiled to memory. The assembly that contains the IL gets mapped the same way.
Good for more than just efficient operating system operation, users and virus scanners really dislike executable code popping out of nowhere.
回答3:
If the EXE is a .Net assembly, you can embed the compiled binary in your (outer) program, load it using Assembly.Load(byte[])
. EDIT: Since it isn't, you can't.
After loading the assembly, you can call functions from the inner assembly (such as the Main
method) like any other assembly, or you can write AppDomain.CurrentDomain.ExecuteAssembly(innerAssembly.FullName)
.
来源:https://stackoverflow.com/questions/2383400/net-embed-an-exe-file-into-my-project