创建独立进程!

发布于 2024-10-20 02:12:48 字数 1079 浏览 2 评论 0原文

我正在尝试从 C++ 中的服务创建一个进程。这个新进程是作为子进程创建的。我想创建一个独立的进程而不是子进程...

我正在使用 CreateProcess 函数来实现相同的目的。由于我创建的新进程是一个子进程,当我尝试在服务级别杀死进程树时,它也会杀死子进程......我不希望发生这种情况。我希望创建的新进程独立于服务运行。

请同样的建议.. 谢谢..

代码

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);    // Start the child process.
ZeroMemory( &pi, sizeof(pi) );
si.dwFlags = STARTF_USESHOWWINDOW;

if(bRunOnWinLogonDesktop)
{
    if(csDesktopName.empty())
        si.lpDesktop = _T("winsta0\\default");
    else
        _tcscpy(si.lpDesktop, csDesktopName.c_str());
}

if(bHide)
    si.wShowWindow = SW_HIDE;     /* maybe even SW_HIDE */
else
    si.wShowWindow = SW_SHOW;     /* maybe even SW_HIDE */



TCHAR szCmdLine[512];
_tcscpy(szCmdLine, csCmdLine.c_str());

if( !CreateProcess( NULL,
                szCmdLine,
               NULL,
               NULL,
               FALSE,
               CREATE_NEW_PROCESS_GROUP,
               NULL,        
               NULL,        
               &si,        
               &pi ) ) 

I am trying to create a process from a service in C++. This new process is creating as a child process. I want to create an independent process and not a child process...

I am using CreateProcess function for the same. Since the new process i create is a child process when i try to kill process tree at the service level it is killing the child process too... I dont want this to happen. I want the new process created to run independent of the service.

Please advice on the same..
Thanks..

Code

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);    // Start the child process.
ZeroMemory( &pi, sizeof(pi) );
si.dwFlags = STARTF_USESHOWWINDOW;

if(bRunOnWinLogonDesktop)
{
    if(csDesktopName.empty())
        si.lpDesktop = _T("winsta0\\default");
    else
        _tcscpy(si.lpDesktop, csDesktopName.c_str());
}

if(bHide)
    si.wShowWindow = SW_HIDE;     /* maybe even SW_HIDE */
else
    si.wShowWindow = SW_SHOW;     /* maybe even SW_HIDE */



TCHAR szCmdLine[512];
_tcscpy(szCmdLine, csCmdLine.c_str());

if( !CreateProcess( NULL,
                szCmdLine,
               NULL,
               NULL,
               FALSE,
               CREATE_NEW_PROCESS_GROUP,
               NULL,        
               NULL,        
               &si,        
               &pi ) ) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-10-27 02:12:48

关闭子进程的线程和进程处理程序后,它仍然是 Process Explorer 中的子进程,但结束父进程不会导致子进程终止。

CreateProcess( NULL,
            szCmdLine,
           NULL,
           NULL,
           FALSE,
           CREATE_NEW_PROCESS_GROUP,
           NULL,        
           NULL,        
           &si,        
           &pi ); 
if(pi.hThread)
    CloseHandle(pi.hTread);
if(pi.hProcess)
    CloseHandle(pi.hProcess);

我在 ReactOS 的 cmd.exe 来源,在执行过程中 '启动'命令。

After closing thread and process handlers of the child process, it's still a child in the Process Explorer, but ending parent process doesn't cause termination of the child one.

CreateProcess( NULL,
            szCmdLine,
           NULL,
           NULL,
           FALSE,
           CREATE_NEW_PROCESS_GROUP,
           NULL,        
           NULL,        
           &si,        
           &pi ); 
if(pi.hThread)
    CloseHandle(pi.hTread);
if(pi.hProcess)
    CloseHandle(pi.hProcess);

I've found this decision in sources of cmd.exe of the ReactOS, in the procedure of executing 'start' command.

冬天的雪花 2024-10-27 02:12:48

创建一个中间进程(使用create_new_process_group),然后创建真正的进程。

Service
  -> Intermediate Process
     -> Real Process

中间进程应该在启动真正进程后立即退出。

Create an intermediate process (use create_new_process_group), which then creates the real process.

Service
  -> Intermediate Process
     -> Real Process

The Intermediate process should exit as soon as it's launched the real process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文