2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2000-09-18 15:18:56 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
2008-04-11 10:38:56 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2000-09-18 15:18:56 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2006-09-17 08:57:38 +00:00
|
|
|
// MARKER(update_precomp.py): autogen include statement, do not remove
|
|
|
|
#include "precompiled_basic.hxx"
|
|
|
|
|
2000-09-18 15:18:56 +00:00
|
|
|
|
|
|
|
#ifdef WNT
|
|
|
|
#include <tools/prewin.h>
|
|
|
|
#include "winbase.h"
|
|
|
|
#include <tools/postwin.h>
|
|
|
|
#endif
|
|
|
|
#include <tools/errcode.hxx>
|
2007-06-27 13:15:52 +00:00
|
|
|
#include <basic/sbxcore.hxx>
|
2000-09-18 15:18:56 +00:00
|
|
|
#include <tools/string.hxx>
|
|
|
|
#include <osl/file.hxx>
|
2010-10-21 15:34:02 -05:00
|
|
|
#include <osl/process.h>
|
2000-09-18 15:18:56 +00:00
|
|
|
|
2007-06-27 13:15:52 +00:00
|
|
|
#include <basic/ttstrhlp.hxx>
|
2000-09-18 15:18:56 +00:00
|
|
|
|
|
|
|
//#ifndef _BYTE_STRING_LIST
|
|
|
|
//DECLARE_LIST( ByteStringList, ByteString * );
|
|
|
|
//#define _BYTE_STRING_LIST
|
|
|
|
//#endif
|
|
|
|
|
2007-06-27 13:15:52 +00:00
|
|
|
#include <basic/process.hxx>
|
2000-09-18 15:18:56 +00:00
|
|
|
|
|
|
|
Process::Process()
|
2010-10-21 15:34:02 -05:00
|
|
|
: m_nArgumentCount( 0 )
|
|
|
|
, m_pArgumentList( NULL )
|
|
|
|
, m_nEnvCount( 0 )
|
|
|
|
, m_pEnvList( NULL )
|
|
|
|
, m_aProcessName()
|
|
|
|
, m_pProcess( NULL )
|
2006-06-19 16:37:17 +00:00
|
|
|
, bWasGPF( FALSE )
|
2000-09-18 15:18:56 +00:00
|
|
|
, bHasBeenStarted( FALSE )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-21 15:34:02 -05:00
|
|
|
#define FREE_USTRING_LIST( count, list ) \
|
|
|
|
if ( count && list ) \
|
|
|
|
{ \
|
|
|
|
for ( unsigned int i = 0; i < count; ++i ) \
|
|
|
|
{ \
|
|
|
|
rtl_uString_release( list[i] ); \
|
|
|
|
list[i] = NULL; \
|
|
|
|
} \
|
|
|
|
delete[] list; \
|
|
|
|
} \
|
|
|
|
count = 0; \
|
|
|
|
list = NULL;
|
|
|
|
|
2000-09-18 15:18:56 +00:00
|
|
|
Process::~Process()
|
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
FREE_USTRING_LIST( m_nArgumentCount, m_pArgumentList );
|
|
|
|
FREE_USTRING_LIST( m_nEnvCount, m_pEnvList );
|
|
|
|
if ( m_pProcess )
|
|
|
|
osl_freeProcessHandle( m_pProcess );
|
2000-09-18 15:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL Process::ImplIsRunning()
|
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
if ( m_pProcess && bHasBeenStarted )
|
2000-09-18 15:18:56 +00:00
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
oslProcessInfo aProcessInfo;
|
|
|
|
osl_getProcessInfo(m_pProcess, osl_Process_EXITCODE, &aProcessInfo );
|
|
|
|
if ( !(aProcessInfo.Fields & osl_Process_EXITCODE) )
|
2000-09-18 15:18:56 +00:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Process::ImplGetExitCode()
|
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
if ( m_pProcess )
|
2000-09-18 15:18:56 +00:00
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
oslProcessInfo aProcessInfo;
|
|
|
|
osl_getProcessInfo(m_pProcess, osl_Process_EXITCODE, &aProcessInfo );
|
|
|
|
if ( !(aProcessInfo.Fields & osl_Process_EXITCODE) )
|
2000-09-18 15:18:56 +00:00
|
|
|
SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
|
|
|
|
return aProcessInfo.Code;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-06-17 10:47:29 +00:00
|
|
|
void Process::SetImage( const String &aAppPath, const String &aAppParams, const Environment *pEnv )
|
2009-04-25 00:18:20 +00:00
|
|
|
{ // Set image file of executable
|
2010-10-21 15:34:02 -05:00
|
|
|
if ( m_pProcess && ImplIsRunning() )
|
2000-09-18 15:18:56 +00:00
|
|
|
SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
|
|
|
|
else
|
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
FREE_USTRING_LIST( m_nArgumentCount, m_pArgumentList );
|
|
|
|
FREE_USTRING_LIST( m_nEnvCount, m_pEnvList );
|
|
|
|
osl_freeProcessHandle( m_pProcess );
|
|
|
|
m_pProcess = NULL;
|
2000-09-18 15:18:56 +00:00
|
|
|
|
|
|
|
xub_StrLen i, nCount = aAppParams.GetQuotedTokenCount( CUniString("\"\"" ), ' ' );
|
2001-03-21 14:07:28 +00:00
|
|
|
::rtl::OUString *pParamList = new ::rtl::OUString[nCount];
|
2003-03-26 11:04:51 +00:00
|
|
|
|
|
|
|
xub_StrLen nParamCount = 0;
|
2000-09-18 15:18:56 +00:00
|
|
|
for ( i = 0 ; i < nCount ; i++ )
|
|
|
|
{
|
2003-03-26 11:04:51 +00:00
|
|
|
::rtl::OUString aTemp = ::rtl::OUString(aAppParams.GetQuotedToken( i, CUniString("\"\"''" ), ' ' ));
|
2000-09-18 15:18:56 +00:00
|
|
|
if ( aTemp.getLength() )
|
2003-03-26 11:04:51 +00:00
|
|
|
{
|
|
|
|
pParamList[nParamCount] = aTemp;
|
|
|
|
nParamCount++;
|
|
|
|
}
|
2000-09-18 15:18:56 +00:00
|
|
|
}
|
2010-10-21 15:34:02 -05:00
|
|
|
m_nArgumentCount = nParamCount;
|
|
|
|
m_pArgumentList = new rtl_uString*[m_nArgumentCount];
|
|
|
|
for ( i = 0 ; i < m_nArgumentCount ; i++ )
|
|
|
|
{
|
|
|
|
m_pArgumentList[i] = NULL;
|
|
|
|
rtl_uString_assign( &(m_pArgumentList[i]), pParamList[i].pData );
|
|
|
|
}
|
|
|
|
delete [] pParamList;
|
2004-06-17 10:47:29 +00:00
|
|
|
|
|
|
|
if ( pEnv )
|
|
|
|
{
|
2010-10-21 15:34:02 -05:00
|
|
|
m_pEnvList = new rtl_uString*[pEnv->size()];
|
2004-06-17 10:47:29 +00:00
|
|
|
|
2010-10-21 15:34:02 -05:00
|
|
|
m_nEnvCount = 0;
|
2004-06-17 10:47:29 +00:00
|
|
|
Environment::const_iterator aIter = pEnv->begin();
|
|
|
|
while ( aIter != pEnv->end() )
|
|
|
|
{
|
|
|
|
::rtl::OUString aTemp = ::rtl::OUString( (*aIter).first );
|
|
|
|
aTemp += ::rtl::OUString::createFromAscii( "=" );
|
|
|
|
aTemp += ::rtl::OUString( (*aIter).second );
|
2010-10-21 15:34:02 -05:00
|
|
|
m_pEnvList[m_nEnvCount] = NULL;
|
|
|
|
rtl_uString_assign( &(m_pEnvList[m_nEnvCount]), aTemp.pData );
|
|
|
|
m_nEnvCount++;
|
2004-06-17 10:47:29 +00:00
|
|
|
aIter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-21 14:07:28 +00:00
|
|
|
::rtl::OUString aNormalizedAppPath;
|
2001-05-10 14:59:46 +00:00
|
|
|
osl::FileBase::getFileURLFromSystemPath( ::rtl::OUString(aAppPath), aNormalizedAppPath );
|
2010-10-21 15:34:02 -05:00
|
|
|
m_aProcessName = aNormalizedAppPath;;
|
2000-09-18 15:18:56 +00:00
|
|
|
bHasBeenStarted = FALSE;
|
2003-05-22 10:01:04 +00:00
|
|
|
|
2000-09-18 15:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL Process::Start()
|
2009-04-25 00:18:20 +00:00
|
|
|
{ // Start program
|
2000-09-18 15:18:56 +00:00
|
|
|
BOOL bSuccess=FALSE;
|
2010-10-21 15:34:02 -05:00
|
|
|
if ( m_pProcess && !ImplIsRunning() )
|
2000-09-18 15:18:56 +00:00
|
|
|
{
|
|
|
|
bWasGPF = FALSE;
|
|
|
|
#ifdef WNT
|
|
|
|
// sal_uInt32 nErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
|
|
|
sal_uInt32 nErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
2010-10-21 15:34:02 -05:00
|
|
|
bSuccess = osl_executeProcess(
|
|
|
|
m_aProcessName.pData,
|
|
|
|
m_pArgumentList,
|
|
|
|
m_nArgumentCount,
|
|
|
|
osl_Process_SEARCHPATH
|
|
|
|
/*| osl_Process_DETACHED*/
|
|
|
|
/*| osl_Process_WAIT*/,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
m_pEnvList,
|
|
|
|
m_nEnvCount,
|
|
|
|
&m_pProcess ) == osl_Process_E_None;
|
|
|
|
|
2000-09-18 15:18:56 +00:00
|
|
|
#ifdef WNT
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
bWasGPF = TRUE;
|
2009-04-25 00:18:20 +00:00
|
|
|
// TODO: Output debug message !!
|
2000-09-18 15:18:56 +00:00
|
|
|
}
|
|
|
|
nErrorMode = SetErrorMode(nErrorMode);
|
|
|
|
#endif
|
|
|
|
bHasBeenStarted = bSuccess;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
|
|
|
|
return bSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG Process::GetExitCode()
|
2009-04-25 00:18:20 +00:00
|
|
|
{ // ExitCode of program after execution
|
2000-09-18 15:18:56 +00:00
|
|
|
return ImplGetExitCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL Process::IsRunning()
|
2009-04-25 00:18:20 +00:00
|
|
|
{
|
2000-09-18 15:18:56 +00:00
|
|
|
return ImplIsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL Process::WasGPF()
|
2009-04-25 00:18:20 +00:00
|
|
|
{ // Did the process fail?
|
2000-09-18 15:18:56 +00:00
|
|
|
#ifdef WNT
|
|
|
|
return ImplGetExitCode() == 3221225477;
|
|
|
|
#else
|
|
|
|
return bWasGPF;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-09-20 11:29:06 +00:00
|
|
|
BOOL Process::Terminate()
|
|
|
|
{
|
|
|
|
if ( ImplIsRunning() )
|
2010-10-21 15:34:02 -05:00
|
|
|
return osl_terminateProcess(m_pProcess) == osl_Process_E_None;
|
2005-06-21 09:30:46 +00:00
|
|
|
return TRUE;
|
2004-09-20 11:29:06 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 15:53:47 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|