🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2834

Re: Launching notepad etc

#2834
From: Dennis Jones
Date: Wed, 03 Jan 2007 18:39
76 lines
3481 bytes
"Colin B Maharaj" <noreply@myhost.com> wrote in message
news:459a3b8f$1@newsgroups.borland.com...

> If I stop my application (in the BCB IDE) my
> application does not close unless all called processes
> are stopped.
>
> here is my code......
> void WinSystem2(const AnsiString& strCmdLine, char *Title)
> {
>
>   PROCESS_INFORMATION     piProcInfo;
>   STARTUPINFO             siStartInfo;
>   // Set up members of STARTUPINFO structure.
>   siStartInfo.cb          = sizeof(STARTUPINFO);
>   siStartInfo.lpReserved  = NULL;
>   siStartInfo.lpReserved2 = NULL;
>   siStartInfo.cbReserved2 = 0;
>   siStartInfo.lpDesktop   = NULL;
>   siStartInfo.lpTitle     = Title;
>   siStartInfo.dwFlags     = 0;
>
>   char *cmdln_str = new char [MAX_PATH];
>   strncpy(cmdln_str, strCmdLine.c_str(), MAX_PATH);
>
>   // Create the child process.
>   bool success = CreateProcess(
>     NULL,
>     cmdln_str,
>     NULL,           // process security attributes
>     NULL,           // primary thread security attributes
>     0,              // handles are inherited
>     0,              // creation flags
>     NULL,           // use parent's environment
>     NULL,           // use parent's current directory
>     &siStartInfo,   // STARTUPINFO pointer
>     &piProcInfo);   // receives PROCESS_INFORMATION
>
>    if (success == true)
>    {                              // process handle
>     CloseHandle(piProcInfo.hThread);
>     DWORD exitCode;
>     GetExitCodeProcess(piProcInfo.hProcess, &exitCode);
> CloseHandle(piProcInfo.hProcess);
>    }
>    delete cmdln_str;
>
>
> }

As Bob has already pointed out, you have a few issues with your code that
should be fixed (such as, remove the call to "GetExitCodeProcess()"...it's
is being used at an improper time, and you aren't using the return value for
anything).

However, I believe the solution to your problem is to create the process
"detached":

>   bool success = CreateProcess(
>     NULL,
>     cmdln_str,
>     NULL,           // process security attributes
>     NULL,           // primary thread security attributes
>     0,              // handles are inherited
>     DETACHED_PROCESS,              // creation flags
>     NULL,           // use parent's environment
>     NULL,           // use parent's current directory
>     &siStartInfo,   // STARTUPINFO pointer
>     &piProcInfo);   // receives PROCESS_INFORMATION

Also, you should close the process' thread handle:

CloseHandle( piProcInfo.hThread );

- Dennis
Technical Details
Message-ID: <459bf822$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <459a3b8f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,fec586f4dcaf98b5
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <459a3b8f$1@newsgroups.borland.com>
Subject: Re: Launching notepad etc
Date: Wed, 3 Jan 2007 10:39:10 -0800
Lines: 80
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 71.245.109.58
Message-ID: <459bf822$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1167849506 71.245.109.58 (3 Jan 2007 10:38:26 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail