C# – Catching exceptions from child process

cwindows

I am looking for any info.
(I know some C++ with win32 and C#)

The Idea:

The idea is to help a program with missing files load. In the end I wanted to see if I could have my program launch a simple c++ executable that requires a dll which prints "hello world" to standard output. The quirk would be that the dll is not present in the folder. The c++ program instead of crashing is suspended and the main program has to request the dll from some source. When the dll is loaded, the printing process is resumed and continues running.

Example of execution.

MainProcess => start("a.exe");
a.exe => LoadLibrary("test.dll");
OS responds with "file does not exist".
MainProcess catches said exception.

MainProcess suspends a.exe like in this example.

MainProcess finds dll.
MainProcess dispatches the exception.
MainProcess tells gets a.exe to attempt loading again.

Are there cases where this kind of thing would not work?
Can a parent capture exceptions meant for children?

How would one do that?

Also is it possible to tell exactly which files a process will need before running it?

Best Answer

Also is it possible to tell exactly which files a process will need before running it?

This is not feasible. First, there are many APIs that load files. You'd need to scan for every one of them. Then, you'd need to figure out what the appropriate parameter would be at runtime. Consider this code:

char foo[100]
scanf("%s",foo);
FILE* fp = open(foo)

Well, there you are hosed. How do you predict what the user might enter?

("Hey", you might say, "I tagged my question C++. What's with the scanf!?" Well, see, that's exactly it. That is legal C++ code, so you'd have to scan for code like that, not just std::cout.)

Even if you could ignore that, what about third-party libraries? They load files. For most developers, most files are probably loaded under the covers.

But say you ignore all that...only test for things explicitly listed in the file. But you are in a separate process, and the user just did a DEL importantlibrary.dll.

Now in a constrained system, maybe you could avoid user entry and avoid file system changes, and you might think you are ok. But you'd still have to track every code-path, with every set of possible inputs, until the program stops running.

Which means you have to prove your program will stop running. Oops. That is often [https://en.wikipedia.org/wiki/NP-completeness](Not possible).

Now if you are talking just about DLLs loaded by an EXE on load (and not dynamically at runtime, that is potentially tractable.

Related Topic