Most barebones example of parent/child process in windows on code::blocks (C++)
1) I'm looking for a very barebones example of two functions (Parent, Child) respectively that would create a child process, then connect the child to the parent (where the parent can access the variables in the child process). Please keep the example as simple as possible as I am pretty sure windows code is purposefully designed to be as complicated and confusing as possible.
2) Alternatively, I am also willing to consider alternates to the WINAPI calls for creating parent/child processes (so long as it's compatible with windows).
The compiler is code::blocks, OS is Vista. Compatibility with other OS is greatly preferred, if at all possible (I know WINAPI isn't, but it's the only method I am aware of). It would be really nice if the functions had similarities to the unix functions (fork, for example).
3) For a harder alternative, how could I suspend a function in such a way I can do other things (then return back to it)? The function is already defined and can't be altered.
Updated:
Context: In a sense, there's a graphical front end (parent) and a function that creates physical 开发者_运维技巧[.png] images (child). The problem is, the physical image rendering is a blocking process, and I want a graphical loading screen during the time it runs (creating someimage.png loading bar sorta thing). There's only two ways to do that - parent-child or a interrupt call.
The child can run the function until completion where the parent just reads an updated variable to display (from the child), or the process just temporarily pauses the functions, draws the loading image to the screen, then re-resumes.
You're not really asking a question, but let's take this for one :-)
Basically you want a mini debugger. This is no longer true after clarifying what's really needed, but I'll leave the snippet in place, you never know who might be interested anyway.
The most barebone code for a mini-debugger looks like this:
int main()
{
STARTUPINFO si = STARTUPINFO();
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
if(CreateProcess("child.exe", 0, 0, 0, 0, DEBUG_PROCESS, 0, 0, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{ // maybe print some error, or don't
return -1;
}
DEBUG_EVENT e;
while (WaitForDebugEvent(&e, INFINITE))
{
handle_debug_event(e); // in here goes your handling
}
return 0;
}
This will notify you automatically when DLLs are loaded/unloaded and when threads are created and die, and when unhandled exceptions or breakpoints are encountered. It also gives the parent process the necessary privilegues to read and write the child's process memory, modify its thread context, and set breakpoints.
Refer to the DEBUG_EVENT
documentation on MSDN and do whatever you like inside your handle_debug_event
function.
精彩评论