如何在新过程中运行功能? [英] How to run a function in new process?

查看:64
本文介绍了如何在新过程中运行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我处于进程 A 的线程之一中,我需要在当前进程中创建新进程 B 线程,并在进程 B 函数 MyFunc()中运行。我该怎么做 ?
我发现了如何从当前进程创建子进程:单击。但是如何在此新过程中运行 MyFunc()?这两个进程应该异步运行,而不要像本例一样互相等待

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:
我在Windows上工作

ETA: I work on windows

推荐答案

我假设您正在Windows下运行。两个不同的进程在完全独立的地址空间中运行。如果您正在考虑将指向MyFunc的指针(在过程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).

显然,您不需要返回值,因为您说它们异步运行。因此,您只需要知道如何做就可以启动另一个过程(您已经知道CreateProcess)并知道如何运行。如果第二个进程是执行MyFunc然后退出的可执行文件,则只需使用CreateProcess启动它,并在lpCommandLine中传递任何参数,并在完成时使其完成。

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.

需要另一个进程来运行任意函数(直到运行时才知道),您可以将可用函数集放入DLL中,并将DLL的名称和函数的名称传递给第二个进程(在lpCommandLine中)。然后,第二个进程使用LoadLibrary加载DLL,并使用GetProcAddress获取函数的地址(按名称)。

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"));
       }

这篇关于如何在新过程中运行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆