🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
6 messages
6 total messages Started by Colin B Maharaj Tue, 02 Jan 2007 11:02
Launching notepad etc
#2826
Author: Colin B Maharaj
Date: Tue, 02 Jan 2007 11:02
52 lines
2570 bytes
Hi all, firstly happy new year to everyone!
I want to launch a session of notepad and
have it separated from the calling application.

You see when I call an external program, it seems to
be attached somehow with the calling process in that
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;


}
Re: Launching notepad etc
#2827
Author: Hans Galema
Date: Tue, 02 Jan 2007 11:15
8 lines
1136 bytes
Colin B Maharaj wrote:

> I want to launch a session of notepad and
> have it separated from the calling application.

Just use ShellExecute().

Hans.
Re: Launching notepad etc
#2828
Author: Bob Gonder
Date: Tue, 02 Jan 2007 16:35
44 lines
2368 bytes
Colin B Maharaj wrote:

>You see when I call an external program, it seems to
>be attached somehow with the calling process in that
>If I stop my application (in the BCB IDE) my
>application does not close unless all called processes
>are stopped.

I think that might be an IDE issue, not an issue with your code.

But, there are a few things I would change...

>   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;

Why aren't you setting All members to 0 ?
Easiest way to do the above is

   STARTUPINFO             siStartInfo = {sizeof(STARTUPINFO), 0};
   siStartInfo.lpTitle     = Title;

>   char *cmdln_str = new char [MAX_PATH];

Why are your putting this on the heap?
Faster is
	char	cmdln_str[MAX_PATH+1];

>   strncpy(cmdln_str, strCmdLine.c_str(), MAX_PATH);

Even faster would be to use strCmdLine.c_str() directly in the
CreateProcess call.

>     GetExitCodeProcess(piProcInfo.hProcess, &exitCode);

	Why are you doing this here?
There isn't much chance that the spawned application has even finished
initialization yet, let alone run to completion?
Re: Launching notepad etc
#2834
Author: 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
Re: Launching notepad etc
#2835
Author: Dennis Jones
Date: Wed, 03 Jan 2007 18:44
81 lines
3477 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.

If it's of any use to you, here is a function I wrote to handle various ways 
of invoking external processes:

//---------------------------------------------------------------------------
static bool StartProcess( LPCSTR lpszImageName,
                          LPCSTR lpszCommandLine,
                          bool bWaitFinish=true,
                          PHANDLE phProcess=NULL,
                          DWORD *pProcessId=NULL,
                          PHANDLE phThread=NULL,
                          DWORD *pThreadId=NULL,
                          DWORD *pExitCode=NULL )
{
  DWORD dwExitCode = 0;

  /* Structures necessary for open/close process */
 PROCESS_INFORMATION ProcInfo;
  STARTUPINFO StartInfo;
  memset( &StartInfo, 0, sizeof(STARTUPINFO) );
  StartInfo.cb = sizeof(STARTUPINFO);

  if (!CreateProcess( (LPSTR)lpszImageName,
                    (LPSTR)lpszCommandLine,
                    NULL,
                    NULL,
                    TRUE,
                    DETACHED_PROCESS|NORMAL_PRIORITY_CLASS,
                    NULL,
                    NULL,
                    &StartInfo,
                    &ProcInfo ))
  {
    return false;
 }
  else
  {
    if ( phProcess != NULL ) *phProcess = ProcInfo.hProcess;
    if ( phThread != NULL ) *phThread = ProcInfo.hThread;
    if ( pProcessId != NULL ) *pProcessId = ProcInfo.dwProcessId;
    if ( pThreadId != NULL ) *pThreadId = ProcInfo.dwThreadId;
    if ( bWaitFinish )  // if we want to wait for this process to complete 
before returning
    {
      if ( WaitForSingleObject( ProcInfo.hProcess, INFINITE) != 
WAIT_FAILED )
      {
        GetExitCodeProcess( ProcInfo.hProcess, &dwExitCode );
      }
      CloseHandle( ProcInfo.hProcess );
      ProcInfo.hProcess = NULL;
      CloseHandle( ProcInfo.hThread );
      ProcInfo.hThread = NULL;
    }
    else
    {
      if ( phProcess == NULL )
      {
        CloseHandle( ProcInfo.hProcess );
        ProcInfo.hProcess = NULL;
      }
      if ( phThread == NULL )
      {
        CloseHandle( ProcInfo.hThread );
        ProcInfo.hThread = NULL;
      }
    }
  }
  if ( pExitCode != NULL ) *pExitCode = dwExitCode;
  return true;
}
//---------------------------------------------------------------------------

- Dennis 
Re: Launching notepad etc
#2666
Author: Colin B Maharaj
Date: Sun, 28 Jan 2007 00:11
14 lines
1477 bytes
Bob Gonder wrote:
> Colin B Maharaj wrote:
> 
>> You see when I call an external program, it seems to
>> be attached somehow with the calling process in that
>> If I stop my application (in the BCB IDE) my
>> application does not close unless all called processes
>> are stopped.
> 
> I think that might be an IDE issue, not an issue with your code.
> 
>

Yes it was, reinstalled BCB6 on a new PC I got and it works like a charm.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads