1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
36 #include <tools/prewin.h>
38 #include <tools/postwin.h>
40 #include <tools/errcode.hxx>
41 #include <vos/process.hxx>
42 #include <basic/sbxcore.hxx>
43 #include <tools/string.hxx>
44 #include <osl/file.hxx>
46 #ifndef _BASIC_TTRESHLP_HXX
47 #include <basic/ttstrhlp.hxx>
50 //#ifndef _BYTE_STRING_LIST
51 //DECLARE_LIST( ByteStringList, ByteString * );
52 //#define _BYTE_STRING_LIST
55 #include <basic/process.hxx>
58 : pArgumentList( NULL
)
62 , bHasBeenStarted( FALSE
)
68 // delete pArgumentList;
74 BOOL
Process::ImplIsRunning()
76 if ( pProcess
&& bHasBeenStarted
)
78 NAMESPACE_VOS(OProcess::TProcessInfo
) aProcessInfo
;
79 pProcess
->getInfo( NAMESPACE_VOS(OProcess::TData_ExitCode
), &aProcessInfo
);
80 if ( !(aProcessInfo
.Fields
& NAMESPACE_VOS(OProcess::TData_ExitCode
)) )
89 long Process::ImplGetExitCode()
93 NAMESPACE_VOS(OProcess::TProcessInfo
) aProcessInfo
;
94 pProcess
->getInfo( NAMESPACE_VOS(OProcess::TData_ExitCode
), &aProcessInfo
);
95 if ( !(aProcessInfo
.Fields
& NAMESPACE_VOS(OProcess::TData_ExitCode
)) )
96 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT
);
97 return aProcessInfo
.Code
;
100 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT
);
105 ////////////////////////////////////////////////////////////////////////////
107 void Process::SetImage( const String
&aAppPath
, const String
&aAppParams
, const Environment
*pEnv
)
108 { // Set image file of executable
109 if ( pProcess
&& ImplIsRunning() )
110 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT
);
113 delete pArgumentList
; pArgumentList
= NULL
;
114 delete pEnvList
; pEnvList
= NULL
;
115 delete pProcess
; pProcess
= NULL
;
117 xub_StrLen i
, nCount
= aAppParams
.GetQuotedTokenCount( CUniString("\"\"" ), ' ' );
118 ::rtl::OUString
*pParamList
= new ::rtl::OUString
[nCount
];
120 xub_StrLen nParamCount
= 0;
121 for ( i
= 0 ; i
< nCount
; i
++ )
123 ::rtl::OUString aTemp
= ::rtl::OUString(aAppParams
.GetQuotedToken( i
, CUniString("\"\"''" ), ' ' ));
124 if ( aTemp
.getLength() )
126 pParamList
[nParamCount
] = aTemp
;
130 pArgumentList
= new NAMESPACE_VOS(OArgumentList
)( pParamList
, nCount
);
133 ::rtl::OUString
*pEnvArray
= NULL
;
136 pEnvArray
= new ::rtl::OUString
[pEnv
->size()];
138 xub_StrLen nEnvCount
= 0;
139 Environment::const_iterator aIter
= pEnv
->begin();
140 while ( aIter
!= pEnv
->end() )
142 ::rtl::OUString aTemp
= ::rtl::OUString( (*aIter
).first
);
143 aTemp
+= ::rtl::OUString::createFromAscii( "=" );
144 aTemp
+= ::rtl::OUString( (*aIter
).second
);
145 pEnvArray
[nEnvCount
] = aTemp
;
149 pEnvList
= new NAMESPACE_VOS(OEnvironment
)( pEnvArray
, nEnvCount
);
152 ::rtl::OUString aNormalizedAppPath
;
153 osl::FileBase::getFileURLFromSystemPath( ::rtl::OUString(aAppPath
), aNormalizedAppPath
);
154 pProcess
= new NAMESPACE_VOS(OProcess
)( aNormalizedAppPath
);
155 bHasBeenStarted
= FALSE
;
157 delete [] pParamList
;
162 BOOL
Process::Start()
165 if ( pProcess
&& !ImplIsRunning() )
169 // sal_uInt32 nErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
170 sal_uInt32 nErrorMode
= SetErrorMode(SEM_NOOPENFILEERRORBOX
| SEM_NOALIGNMENTFAULTEXCEPT
| SEM_NOGPFAULTERRORBOX
);
176 bSuccess
= pProcess
->execute( (NAMESPACE_VOS(OProcess
)::TProcessOption
)
177 ( NAMESPACE_VOS(OProcess
)::TOption_SearchPath
178 /*| NAMESPACE_VOS(OProcess)::TOption_Detached*/
179 /*| NAMESPACE_VOS(OProcess)::TOption_Wait*/ ),
181 *pEnvList
) == NAMESPACE_VOS(OProcess
)::E_None
;
185 bSuccess
= pProcess
->execute( (NAMESPACE_VOS(OProcess
)::TProcessOption
)
186 ( NAMESPACE_VOS(OProcess
)::TOption_SearchPath
187 /*| NAMESPACE_VOS(OProcess)::TOption_Detached*/
188 /*| NAMESPACE_VOS(OProcess)::TOption_Wait*/ ),
189 *pArgumentList
) == NAMESPACE_VOS(OProcess
)::E_None
;
196 // TODO: Output debug message !!
198 nErrorMode
= SetErrorMode(nErrorMode
);
200 bHasBeenStarted
= bSuccess
;
203 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT
);
207 ULONG
Process::GetExitCode()
208 { // ExitCode of program after execution
209 return ImplGetExitCode();
212 BOOL
Process::IsRunning()
214 return ImplIsRunning();
217 BOOL
Process::WasGPF()
218 { // Did the process fail?
220 return ImplGetExitCode() == 3221225477;
226 BOOL
Process::Terminate()
228 if ( ImplIsRunning() )
229 return pProcess
->terminate() == vos::OProcess::E_None
;