问题
Now I am in one of the threads of process A
, I need to create new process B
in current thread, and run in process B
function MyFunc()
. How can I do it ?
I found how to create a child process from current process: click . But how can I run MyFunc()
in this new process ? This 2 processes should run async, and not wait each other
like in this example:
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
ETA: I work on windows
回答1:
I assume you are running under Windows. Two different processes run in completely separate address spaces. If you are thinking of passing a pointer to MyFunc (in process 1) to the other process, this can actually be done but it is very difficult and there are much easier ways (you have to get the function itself in memory that is visible to both processes).
Apparently, you don't need a value returned since you say they run asynchronously. So all you need to know how to do is start the other process (you already know this-- CreateProcess) and how to tell it what to run. If the second process is an executable that executes MyFunc then exits, then just start it using CreateProcess, passing any arguments in lpCommandLine and let it complete when it completes.
If you need the other process to run some arbitrary function (not known until run time), you can put the set of available functions into a DLL and pass the name of the DLL and the name of the function to the second process (in lpCommandLine). The second process then loads the DLL using LoadLibrary and gets the address of the function (by name) using GetProcAddress.
If you need the other process to run some function and it does not have support for the above described command line arguments, then you can use a technique known as code injection. This is a very advanced technique which lets you run any arbitrary code in another process. I will append how to do it to this answer if this is what you need. Please post a comment if so.
I added the following after reading your comment that you need to invoke a fixed function in the other process. Put the argument string into the second argument on the command line (first argument is the executable name-- don't forget to include it!):
void MyFunc (TCHAR* argument) {
// Validate argument then do something with it...
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1) {
MyFunc(argv[1]);
}
return 0;
}
Here's how to invoke the process (Note: I think this is correct, but I did not actually compile and test this snippet. It's intended just to give you the general idea):
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&si, 0, sizeof(si));
memset (&pi, 0, sizeof(pi));
GetStartupInfo(&si);
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
TCHAR CommandLine[1024];
// MyFuncArgument (type == TCHAR*, declaration not shown) is the single argument for MyFunc
// (put it in quotes if needed):
_stprintf(CommandLine, _T("DoMyFunc.exe %s"), MyFuncArgument);
if (CreateProcess (_T(".\\DoMyFunc.exe"), CommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) != 0) {
_tprintf (_T("CreateProcess Succeeded\n"));
}
else {
_tprintf (_T("CreateProcess Failed\n"));
}
来源:https://stackoverflow.com/questions/19051248/how-to-run-a-function-in-new-process