source: trunk/src/helpers/apps.c@ 133

Last change on this file since 133 was 133, checked in by umoeller, 24 years ago

program plus other fixes

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 53.0 KB
Line 
1
2/*
3 *@@sourcefile apps.c:
4 * contains program helpers (environments, application start).
5 *
6 * This file is new with V0.9.12 and contains functions
7 * previously in winh.c and dosh2.c.
8 *
9 * Note: Version numbering in this file relates to XWorkplace version
10 * numbering.
11 *
12 *@@header "helpers\apps.h"
13 *@@added V0.9.12 (2001-05-26) [umoeller]
14 */
15
16/*
17 * Copyright (C) 1997-2001 Ulrich M”ller.
18 * This file is part of the "XWorkplace helpers" source package.
19 * This is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published
21 * by the Free Software Foundation, in version 2 as it comes in the
22 * "COPYING" file of the XWorkplace main distribution.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 */
28
29#define OS2EMX_PLAIN_CHAR
30 // this is needed for "os2emx.h"; if this is defined,
31 // emx will define PSZ as _signed_ char, otherwise
32 // as unsigned char
33
34#define INCL_DOSPROCESS
35#define INCL_DOSMODULEMGR
36#define INCL_DOSSESMGR
37#define INCL_DOSERRORS
38
39#define INCL_WINPROGRAMLIST // needed for PROGDETAILS, wppgm.h
40#define INCL_WINERRORS
41#define INCL_SHLERRORS
42#include <os2.h>
43
44#include <stdio.h>
45
46#include "setup.h" // code generation and debugging options
47
48#include "helpers\apps.h"
49#include "helpers\dosh.h"
50#include "helpers\prfh.h"
51#include "helpers\standards.h" // some standard macros
52#include "helpers\stringh.h"
53#include "helpers\winh.h"
54#include "helpers\xstring.h"
55
56/*
57 *@@category: Helpers\PM helpers\Application helpers
58 */
59
60/* ******************************************************************
61 *
62 * Environment helpers
63 *
64 ********************************************************************/
65
66/*
67 *@@ appQueryEnvironmentLen:
68 * returns the total length of the passed in environment
69 * string buffer, including the terminating two null bytes.
70 *
71 *@@added V0.9.16 (2002-01-09) [umoeller]
72 */
73
74ULONG appQueryEnvironmentLen(PCSZ pcszEnvironment)
75{
76 ULONG cbEnvironment = 0;
77 if (pcszEnvironment)
78 {
79 PCSZ pVarThis = pcszEnvironment;
80 // go thru the environment strings; last one has two null bytes
81 while (*pVarThis)
82 {
83 ULONG ulLenThis = strlen(pVarThis) + 1;
84 cbEnvironment += ulLenThis;
85 pVarThis += ulLenThis;
86 }
87
88 cbEnvironment++; // last null byte
89 }
90
91 return (cbEnvironment);
92}
93
94/*
95 *@@ appParseEnvironment:
96 * this takes one of those ugly environment strings
97 * as used by DosStartSession and WinStartApp (with
98 * lots of zero-terminated strings one after another
99 * and a duplicate zero byte as a terminator) as
100 * input and splits it into an array of separate
101 * strings in pEnv.
102 *
103 * The newly allocated strings are stored in in
104 * pEnv->papszVars. The array count is stored in
105 * pEnv->cVars.
106 *
107 * Each environment variable will be copied into
108 * one newly allocated string in the array. Use
109 * appFreeEnvironment to free the memory allocated
110 * by this function.
111 *
112 * Use the following code to browse thru the array:
113 +
114 + DOSENVIRONMENT Env = {0};
115 + if (appParseEnvironment(pszEnv,
116 + &Env)
117 + == NO_ERROR)
118 + {
119 + if (Env.papszVars)
120 + {
121 + PSZ *ppszThis = Env.papszVars;
122 + for (ul = 0;
123 + ul < Env.cVars;
124 + ul++)
125 + {
126 + PSZ pszThis = *ppszThis;
127 + // pszThis now has something like PATH=C:\TEMP
128 + // ...
129 + // next environment string
130 + ppszThis++;
131 + }
132 + }
133 + appFreeEnvironment(&Env);
134 + }
135 *
136 *@@added V0.9.4 (2000-08-02) [umoeller]
137 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
138 */
139
140APIRET appParseEnvironment(const char *pcszEnv,
141 PDOSENVIRONMENT pEnv) // out: new environment
142{
143 APIRET arc = NO_ERROR;
144 if (!pcszEnv)
145 arc = ERROR_INVALID_PARAMETER;
146 else
147 {
148 PSZ pszVarThis = (PSZ)pcszEnv;
149 ULONG cVars = 0;
150 // count strings
151 while (*pszVarThis)
152 {
153 cVars++;
154 pszVarThis += strlen(pszVarThis) + 1;
155 }
156
157 pEnv->cVars = 0;
158 pEnv->papszVars = 0;
159
160 if (cVars)
161 {
162 ULONG cbArray = sizeof(PSZ) * cVars;
163 PSZ *papsz;
164 if (!(papsz = (PSZ*)malloc(cbArray)))
165 arc = ERROR_NOT_ENOUGH_MEMORY;
166 else
167 {
168 PSZ *ppszTarget = papsz;
169 memset(papsz, 0, cbArray);
170 pszVarThis = (PSZ)pcszEnv;
171 while (*pszVarThis)
172 {
173 ULONG ulThisLen;
174 if (!(*ppszTarget = strhdup(pszVarThis, &ulThisLen)))
175 {
176 arc = ERROR_NOT_ENOUGH_MEMORY;
177 break;
178 }
179 (pEnv->cVars)++;
180 ppszTarget++;
181 pszVarThis += ulThisLen + 1;
182 }
183
184 pEnv->papszVars = papsz;
185 }
186 }
187 }
188
189 return (arc);
190}
191
192/*
193 *@@ appGetEnvironment:
194 * calls appParseEnvironment for the current
195 * process environment, which is retrieved from
196 * the info blocks.
197 *
198 * Returns:
199 *
200 * -- NO_ERROR:
201 *
202 * -- ERROR_INVALID_PARAMETER
203 *
204 * -- ERROR_BAD_ENVIRONMENT: no environment found in
205 * info blocks.
206 *
207 *@@added V0.9.4 (2000-07-19) [umoeller]
208 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
209 */
210
211APIRET appGetEnvironment(PDOSENVIRONMENT pEnv)
212{
213 APIRET arc = NO_ERROR;
214 if (!pEnv)
215 arc = ERROR_INVALID_PARAMETER;
216 else
217 {
218 PTIB ptib = 0;
219 PPIB ppib = 0;
220 arc = DosGetInfoBlocks(&ptib, &ppib);
221 if (arc == NO_ERROR)
222 {
223 PSZ pszEnv;
224 if (pszEnv = ppib->pib_pchenv)
225 arc = appParseEnvironment(pszEnv, pEnv);
226 else
227 arc = ERROR_BAD_ENVIRONMENT;
228 }
229 }
230
231 return (arc);
232}
233
234/*
235 *@@ appFindEnvironmentVar:
236 * returns the PSZ* in the pEnv->papszVars array
237 * which specifies the environment variable in pszVarName.
238 *
239 * With pszVarName, you can either specify the variable
240 * name only ("VARNAME") or a full environment string
241 * ("VARNAME=BLAH"). In any case, only the variable name
242 * is compared.
243 *
244 * Returns NULL if no such variable name was found in
245 * the array.
246 *
247 *@@added V0.9.4 (2000-07-19) [umoeller]
248 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
249 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
250 *@@changed V0.9.16 (2002-01-01) [umoeller]: removed extra heap allocation
251 */
252
253PSZ* appFindEnvironmentVar(PDOSENVIRONMENT pEnv,
254 PSZ pszVarName)
255{
256 PSZ *ppszRet = 0;
257
258 if ( (pEnv)
259 && (pEnv->papszVars)
260 && (pszVarName)
261 )
262 {
263 ULONG ul = 0;
264 ULONG ulVarNameLen = 0;
265
266 PSZ pFirstEqual;
267 // rewrote all the following for speed V0.9.16 (2002-01-01) [umoeller]
268 if (pFirstEqual = strchr(pszVarName, '='))
269 // VAR=VALUE
270 // ^ pFirstEqual
271 ulVarNameLen = pFirstEqual - pszVarName;
272 else
273 ulVarNameLen = strlen(pszVarName);
274
275 for (ul = 0;
276 ul < pEnv->cVars;
277 ul++)
278 {
279 PSZ pszThis = pEnv->papszVars[ul];
280 if (pFirstEqual = strchr(pszThis, '='))
281 {
282 ULONG ulLenThis = pFirstEqual - pszThis;
283 if ( (ulLenThis == ulVarNameLen)
284 && (!memicmp(pszThis,
285 pszVarName,
286 ulVarNameLen))
287 )
288 {
289 ppszRet = &pEnv->papszVars[ul];
290 break;
291 }
292 }
293 }
294 }
295
296 return (ppszRet);
297}
298
299/*
300 *@@ appSetEnvironmentVar:
301 * sets an environment variable in the specified
302 * environment, which must have been initialized
303 * using appGetEnvironment first.
304 *
305 * pszNewEnv must be a full environment string
306 * in the form "VARNAME=VALUE".
307 *
308 * If "VARNAME" has already been set to something
309 * in the string array in pEnv, that array item
310 * is replaced.
311 *
312 * OTOH, if "VARNAME" has not been set yet, a new
313 * item is added to the array, and pEnv->cVars is
314 * raised by one. In that case, fAddFirst determines
315 * whether the new array item is added to the front
316 * or the tail of the environment list.
317 *
318 *@@added V0.9.4 (2000-07-19) [umoeller]
319 *@@changed V0.9.7 (2000-12-17) [umoeller]: added fAddFirst
320 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
321 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed crash if !fAddFirst
322 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
323 */
324
325APIRET appSetEnvironmentVar(PDOSENVIRONMENT pEnv,
326 PSZ pszNewEnv,
327 BOOL fAddFirst)
328{
329 APIRET arc = NO_ERROR;
330 if ((!pEnv) || (!pszNewEnv))
331 arc = ERROR_INVALID_PARAMETER;
332 else
333 {
334 if (!pEnv->papszVars)
335 {
336 // no variables set yet:
337 pEnv->papszVars = (PSZ*)malloc(sizeof(PSZ));
338 pEnv->cVars = 1;
339
340 *(pEnv->papszVars) = strdup(pszNewEnv);
341 }
342 else
343 {
344 PSZ *ppszEnvLine;
345 if (ppszEnvLine = appFindEnvironmentVar(pEnv, pszNewEnv))
346 // was set already: replace
347 arc = strhStore(ppszEnvLine,
348 pszNewEnv,
349 NULL);
350 else
351 {
352 // not set already:
353 PSZ *ppszNew = NULL;
354
355 // allocate new array, with one new entry
356 // fixed V0.9.12 (2001-05-26) [umoeller], this crashed
357 PSZ *papszNew;
358
359 if (!(papszNew = (PSZ*)malloc(sizeof(PSZ) * (pEnv->cVars + 1))))
360 arc = ERROR_NOT_ENOUGH_MEMORY;
361 else
362 {
363 if (fAddFirst)
364 {
365 // add as first entry:
366 // overwrite first entry
367 ppszNew = papszNew;
368 // copy old entries
369 memcpy(papszNew + 1, // second new entry
370 pEnv->papszVars, // first old entry
371 sizeof(PSZ) * pEnv->cVars);
372 }
373 else
374 {
375 // append at the tail:
376 // overwrite last entry
377 ppszNew = papszNew + pEnv->cVars;
378 // copy old entries
379 memcpy(papszNew, // first new entry
380 pEnv->papszVars, // first old entry
381 sizeof(PSZ) * pEnv->cVars);
382 }
383
384 free(pEnv->papszVars); // was missing V0.9.12 (2001-05-21) [umoeller]
385 pEnv->papszVars = papszNew;
386 pEnv->cVars++;
387 *ppszNew = strdup(pszNewEnv);
388 }
389 }
390 }
391 }
392
393 return (arc);
394}
395
396/*
397 *@@ appConvertEnvironment:
398 * converts an environment initialized by appGetEnvironment
399 * to the string format required by WinStartApp and DosExecPgm,
400 * that is, one memory block is allocated in *ppszEnv and all
401 * strings in pEnv->papszVars are copied to that block. Each
402 * string is terminated with a null character; the last string
403 * is terminated with two null characters.
404 *
405 * Use free() to free the memory block allocated by this
406 * function in *ppszEnv.
407 *
408 *@@added V0.9.4 (2000-07-19) [umoeller]
409 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
410 */
411
412APIRET appConvertEnvironment(PDOSENVIRONMENT pEnv,
413 PSZ *ppszEnv, // out: environment string
414 PULONG pulSize) // out: size of block allocated in *ppszEnv; ptr can be NULL
415{
416 APIRET arc = NO_ERROR;
417 if ( (!pEnv)
418 || (!pEnv->papszVars)
419 )
420 arc = ERROR_INVALID_PARAMETER;
421 else
422 {
423 // count memory needed for all strings
424 ULONG cbNeeded = 0,
425 ul = 0;
426 PSZ *ppszThis = pEnv->papszVars;
427
428 for (ul = 0;
429 ul < pEnv->cVars;
430 ul++)
431 {
432 cbNeeded += strlen(*ppszThis) + 1; // length of string plus null terminator
433
434 // next environment string
435 ppszThis++;
436 }
437
438 cbNeeded++; // for another null terminator
439
440 if (!(*ppszEnv = (PSZ)malloc(cbNeeded)))
441 arc = ERROR_NOT_ENOUGH_MEMORY;
442 else
443 {
444 PSZ pTarget = *ppszEnv;
445 if (pulSize)
446 *pulSize = cbNeeded;
447 ppszThis = pEnv->papszVars;
448
449 // now copy each string
450 for (ul = 0;
451 ul < pEnv->cVars;
452 ul++)
453 {
454 PSZ pSource = *ppszThis;
455
456 while ((*pTarget++ = *pSource++))
457 ;
458
459 // *pTarget++ = 0; // append null terminator per string
460
461 // next environment string
462 ppszThis++;
463 }
464
465 *pTarget++ = 0; // append second null terminator
466 }
467 }
468
469 return (arc);
470}
471
472/*
473 *@@ appFreeEnvironment:
474 * frees memory allocated by appGetEnvironment.
475 *
476 *@@added V0.9.4 (2000-07-19) [umoeller]
477 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
478 */
479
480APIRET appFreeEnvironment(PDOSENVIRONMENT pEnv)
481{
482 APIRET arc = NO_ERROR;
483 if ( (!pEnv)
484 || (!pEnv->papszVars)
485 )
486 arc = ERROR_INVALID_PARAMETER;
487 else
488 {
489 PSZ *ppszThis = pEnv->papszVars;
490 PSZ pszThis;
491 ULONG ul = 0;
492
493 for (ul = 0;
494 ul < pEnv->cVars;
495 ul++)
496 {
497 pszThis = *ppszThis;
498 free(pszThis);
499 // *ppszThis = NULL;
500 // next environment string
501 ppszThis++;
502 }
503
504 free(pEnv->papszVars);
505 pEnv->cVars = 0;
506 }
507
508 return (arc);
509}
510
511/* ******************************************************************
512 *
513 * Application information
514 *
515 ********************************************************************/
516
517/*
518 *@@ appQueryAppType:
519 * returns the Control Program (Dos) and
520 * Win* PROG_* application types for the
521 * specified executable. Essentially, this
522 * is a wrapper around DosQueryAppType.
523 *
524 * pcszExecutable must be fully qualified.
525 * You can use doshFindExecutable to qualify
526 * it.
527 *
528 * This returns the APIRET of DosQueryAppType.
529 * If this is NO_ERROR; *pulDosAppType receives
530 * the app type of DosQueryAppType. In addition,
531 * *pulWinAppType is set to one of the following:
532 *
533 * -- PROG_FULLSCREEN
534 *
535 * -- PROG_PDD
536 *
537 * -- PROG_VDD
538 *
539 * -- PROG_DLL
540 *
541 * -- PROG_WINDOWEDVDM
542 *
543 * -- PROG_PM
544 *
545 * -- PROG_31_ENHSEAMLESSCOMMON
546 *
547 * -- PROG_WINDOWABLEVIO
548 *
549 * -- PROG_DEFAULT
550 *
551 *@@added V0.9.9 (2001-03-07) [umoeller]
552 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
553 *@@changed V0.9.14 (2001-08-07) [pr]: use FAPPTYP_* constants
554 *@@changed V0.9.16 (2001-12-08) [umoeller]: added checks for batch files, other optimizations
555 */
556
557APIRET appQueryAppType(const char *pcszExecutable,
558 PULONG pulDosAppType,
559 PULONG pulWinAppType)
560{
561 APIRET arc;
562
563/*
564 #define FAPPTYP_NOTSPEC 0x0000
565 #define FAPPTYP_NOTWINDOWCOMPAT 0x0001
566 #define FAPPTYP_WINDOWCOMPAT 0x0002
567 #define FAPPTYP_WINDOWAPI 0x0003
568 #define FAPPTYP_BOUND 0x0008
569 #define FAPPTYP_DLL 0x0010
570 #define FAPPTYP_DOS 0x0020
571 #define FAPPTYP_PHYSDRV 0x0040 // physical device driver
572 #define FAPPTYP_VIRTDRV 0x0080 // virtual device driver
573 #define FAPPTYP_PROTDLL 0x0100 // 'protected memory' dll
574 #define FAPPTYP_WINDOWSREAL 0x0200 // Windows real mode app
575 #define FAPPTYP_WINDOWSPROT 0x0400 // Windows protect mode app
576 #define FAPPTYP_WINDOWSPROT31 0x1000 // Windows 3.1 protect mode app
577 #define FAPPTYP_32BIT 0x4000
578*/
579
580 ULONG ulWinAppType = PROG_DEFAULT;
581
582 if (!(arc = DosQueryAppType((PSZ)pcszExecutable, pulDosAppType)))
583 {
584 // clear the 32-bit flag
585 // V0.9.16 (2001-12-08) [umoeller]
586 ULONG ulDosAppType = (*pulDosAppType) & ~FAPPTYP_32BIT,
587 ulLoAppType = ulDosAppType & 0xFFFF;
588
589 if (ulDosAppType & FAPPTYP_PHYSDRV) // 0x40
590 ulWinAppType = PROG_PDD;
591 else if (ulDosAppType & FAPPTYP_VIRTDRV) // 0x80
592 ulWinAppType = PROG_VDD;
593 else if ((ulDosAppType & 0xF0) == FAPPTYP_DLL) // 0x10
594 // DLL bit set
595 ulWinAppType = PROG_DLL;
596 else if (ulDosAppType & FAPPTYP_DOS) // 0x20
597 // DOS bit set?
598 ulWinAppType = PROG_WINDOWEDVDM;
599 else if ((ulDosAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) // 0x0003)
600 // "Window-API" == PM
601 ulWinAppType = PROG_PM;
602 else if (ulLoAppType == FAPPTYP_WINDOWSREAL)
603 ulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // @@todo really?
604 else if ( (ulLoAppType == FAPPTYP_WINDOWSPROT31) // 0x1000) // windows program (?!?)
605 || (ulLoAppType == FAPPTYP_WINDOWSPROT) // ) // windows program (?!?)
606 )
607 ulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // PROG_31_ENH;
608 else if ((ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_WINDOWCOMPAT) // 0x02)
609 ulWinAppType = PROG_WINDOWABLEVIO;
610 else if ((ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_NOTWINDOWCOMPAT) // 0x01)
611 ulWinAppType = PROG_FULLSCREEN;
612 }
613
614 if (ulWinAppType == PROG_DEFAULT)
615 {
616 // added checks for batch files V0.9.16 (2001-12-08) [umoeller]
617 PCSZ pcszExt;
618 if (pcszExt = doshGetExtension(pcszExecutable))
619 {
620 if (!stricmp(pcszExt, "BAT"))
621 {
622 ulWinAppType = PROG_WINDOWEDVDM;
623 arc = NO_ERROR;
624 }
625 else if (!stricmp(pcszExt, "CMD"))
626 {
627 ulWinAppType = PROG_WINDOWABLEVIO;
628 arc = NO_ERROR;
629 }
630 }
631 }
632
633 *pulWinAppType = ulWinAppType;
634
635 return (arc);
636}
637
638/*
639 *@@ PROGTYPESTRING:
640 *
641 *@@added V0.9.16 (2002-01-13) [umoeller]
642 */
643
644typedef struct _PROGTYPESTRING
645{
646 PROGCATEGORY progc;
647 PCSZ pcsz;
648} PROGTYPESTRING, *PPROGTYPESTRING;
649
650PROGTYPESTRING G_aProgTypes[] =
651 {
652 PROG_DEFAULT, "PROG_DEFAULT",
653 PROG_FULLSCREEN, "PROG_FULLSCREEN",
654 PROG_WINDOWABLEVIO, "PROG_WINDOWABLEVIO",
655 PROG_PM, "PROG_PM",
656 PROG_GROUP, "PROG_GROUP",
657 PROG_VDM, "PROG_VDM",
658 // same as PROG_REAL, "PROG_REAL",
659 PROG_WINDOWEDVDM, "PROG_WINDOWEDVDM",
660 PROG_DLL, "PROG_DLL",
661 PROG_PDD, "PROG_PDD",
662 PROG_VDD, "PROG_VDD",
663 PROG_WINDOW_REAL, "PROG_WINDOW_REAL",
664 PROG_30_STD, "PROG_30_STD",
665 // same as PROG_WINDOW_PROT, "PROG_WINDOW_PROT",
666 PROG_WINDOW_AUTO, "PROG_WINDOW_AUTO",
667 PROG_30_STDSEAMLESSVDM, "PROG_30_STDSEAMLESSVDM",
668 // same as PROG_SEAMLESSVDM, "PROG_SEAMLESSVDM",
669 PROG_30_STDSEAMLESSCOMMON, "PROG_30_STDSEAMLESSCOMMON",
670 // same as PROG_SEAMLESSCOMMON, "PROG_SEAMLESSCOMMON",
671 PROG_31_STDSEAMLESSVDM, "PROG_31_STDSEAMLESSVDM",
672 PROG_31_STDSEAMLESSCOMMON, "PROG_31_STDSEAMLESSCOMMON",
673 PROG_31_ENHSEAMLESSVDM, "PROG_31_ENHSEAMLESSVDM",
674 PROG_31_ENHSEAMLESSCOMMON, "PROG_31_ENHSEAMLESSCOMMON",
675 PROG_31_ENH, "PROG_31_ENH",
676 PROG_31_STD, "PROG_31_STD",
677
678// Warp 4 toolkit defines, whatever these were designed for...
679#ifndef PROG_DOS_GAME
680 #define PROG_DOS_GAME (PROGCATEGORY)21
681#endif
682#ifndef PROG_WIN_GAME
683 #define PROG_WIN_GAME (PROGCATEGORY)22
684#endif
685#ifndef PROG_DOS_MODE
686 #define PROG_DOS_MODE (PROGCATEGORY)23
687#endif
688
689 PROG_DOS_GAME, "PROG_DOS_GAME",
690 PROG_WIN_GAME, "PROG_WIN_GAME",
691 PROG_DOS_MODE, "PROG_DOS_MODE",
692
693 // added this V0.9.16 (2001-12-08) [umoeller]
694 PROG_WIN32, "PROG_WIN32"
695 };
696
697/*
698 *@@ appDescribeAppType:
699 * returns a "PROG_*" string for the given
700 * program type. Useful for WPProgram setup
701 * strings and such.
702 *
703 *@@added V0.9.16 (2001-10-06)
704 */
705
706PCSZ appDescribeAppType(PROGCATEGORY progc) // in: from PROGDETAILS.progc
707{
708 ULONG ul;
709 for (ul = 0;
710 ul < ARRAYITEMCOUNT(G_aProgTypes);
711 ul++)
712 {
713 if (G_aProgTypes[ul].progc == progc)
714 return (G_aProgTypes[ul].pcsz);
715 }
716
717 return NULL;
718}
719
720/*
721 *@@ appIsWindowsApp:
722 * checks the specified program category
723 * (PROGDETAILS.progt.progc) for whether
724 * it represents a Win-OS/2 application.
725 *
726 * Returns:
727 *
728 * -- 0: no windows app (it's VIO, OS/2
729 * or DOS fullscreen, or PM).
730 *
731 * -- 1: Win-OS/2 standard app.
732 *
733 * -- 2: Win-OS/2 enhanced-mode app.
734 *
735 *@@added V0.9.12 (2001-05-26) [umoeller]
736 */
737
738ULONG appIsWindowsApp(ULONG ulProgCategory)
739{
740 switch (ulProgCategory)
741 {
742 case PROG_31_ENHSEAMLESSVDM: // 17
743 case PROG_31_ENHSEAMLESSCOMMON: // 18
744 case PROG_31_ENH: // 19
745 return (2);
746
747#ifndef PROG_30_STD
748 #define PROG_30_STD (PROGCATEGORY)11
749#endif
750
751#ifndef PROG_30_STDSEAMLESSVDM
752 #define PROG_30_STDSEAMLESSVDM (PROGCATEGORY)13
753#endif
754
755 case PROG_WINDOW_REAL: // 10
756 case PROG_30_STD: // 11
757 case PROG_WINDOW_AUTO: // 12
758 case PROG_30_STDSEAMLESSVDM: // 13
759 case PROG_30_STDSEAMLESSCOMMON: // 14
760 case PROG_31_STDSEAMLESSVDM: // 15
761 case PROG_31_STDSEAMLESSCOMMON: // 16
762 case PROG_31_STD: // 20
763 return (1);
764 }
765
766 return (0);
767}
768
769/* ******************************************************************
770 *
771 * Application start
772 *
773 ********************************************************************/
774
775/*
776 *@@ CallBatchCorrectly:
777 * fixes the specified PROGDETAILS for
778 * command files in the executable part
779 * by inserting /C XXX into the parameters
780 * and setting the executable according
781 * to an environment variable.
782 *
783 *@@added V0.9.6 (2000-10-16) [umoeller]
784 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
785 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
786 */
787
788VOID CallBatchCorrectly(PPROGDETAILS pProgDetails,
789 PXSTRING pstrParams, // in/out: modified parameters (reallocated)
790 const char *pcszEnvVar, // in: env var spec'g command proc
791 // (e.g. "OS2_SHELL"); can be NULL
792 const char *pcszDefProc) // in: def't command proc (e.g. "CMD.EXE")
793{
794 // XXX.CMD file as executable:
795 // fix args to /C XXX.CMD
796
797 PSZ pszOldParams = NULL;
798 ULONG ulOldParamsLength = pstrParams->ulLength;
799 if (ulOldParamsLength)
800 // we have parameters already:
801 // make a backup... we'll append that later
802 pszOldParams = strdup(pstrParams->psz);
803
804 // set new params to "/C filename.cmd"
805 xstrcpy(pstrParams, "/C ", 0);
806 xstrcat(pstrParams,
807 pProgDetails->pszExecutable,
808 0);
809
810 if (pszOldParams)
811 {
812 // .cmd had params:
813 // append space and old params
814 xstrcatc(pstrParams, ' ');
815 xstrcat(pstrParams,
816 pszOldParams,
817 ulOldParamsLength);
818 free(pszOldParams);
819 }
820
821 // set executable to $(OS2_SHELL)
822 pProgDetails->pszExecutable = NULL;
823 if (pcszEnvVar)
824 pProgDetails->pszExecutable = getenv(pcszEnvVar);
825 if (!pProgDetails->pszExecutable)
826 pProgDetails->pszExecutable = (PSZ)pcszDefProc;
827 // should be on PATH
828}
829
830/*
831 *@@ appQueryDefaultWin31Environment:
832 * returns the default Win-OS/2 3.1 environment
833 * from OS2.INI, which you can then merge with
834 * your process environment to be able to
835 * start Win-OS/2 sessions properly with
836 * appStartApp.
837 *
838 * Caller must free() the return value.
839 *
840 *@@added V0.9.12 (2001-05-26) [umoeller]
841 */
842
843PSZ appQueryDefaultWin31Environment(VOID)
844{
845 PSZ pszReturn = NULL;
846 ULONG ulSize = 0;
847 // get default environment (from Win-OS/2 settings object)
848 // from OS2.INI
849 PSZ pszDefEnv = prfhQueryProfileData(HINI_USER,
850 "WINOS2",
851 "PM_GlobalWindows31Settings",
852 &ulSize);
853 if (pszDefEnv)
854 {
855 if (pszReturn = (PSZ)malloc(ulSize + 2))
856 {
857 PSZ p;
858 memset(pszReturn, 0, ulSize + 2);
859 memcpy(pszReturn, pszDefEnv, ulSize);
860
861 for (p = pszReturn;
862 p < pszReturn + ulSize;
863 p++)
864 if (*p == ';')
865 *p = 0;
866
867 // okay.... now we got an OS/2-style environment
868 // with 0, 0, 00 strings
869 }
870
871 free(pszDefEnv);
872 }
873
874 return (pszReturn);
875}
876
877/*
878 *@@ appStartApp:
879 * wrapper around WinStartApp which fixes the
880 * specified PROGDETAILS to (hopefully) work
881 * work with all executable types.
882 *
883 * This fixes the executable info to support:
884 *
885 * -- starting "*" executables (command prompts
886 * for OS/2, DOS, Win-OS/2);
887 *
888 * -- starting ".CMD" and ".BAT" files as
889 * PROGDETAILS.pszExecutable;
890 *
891 * -- starting apps which are not fully qualified
892 * and therefore assumed to be on the PATH.
893 *
894 * Unless it is "*", PROGDETAILS.pszExecutable must
895 * be a proper file name. The full path may be omitted
896 * if it is on the PATH, but the extension (.EXE etc.)
897 * must be given. You can use doshFindExecutable to
898 * find executables if you don't know the extension.
899 *
900 * This also handles and merges special and default
901 * environments for the app to be started. The
902 * following should be respected:
903 *
904 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
905 * is NULL, the new app inherits a default environment
906 * from the shell.
907 *
908 * -- However, if you specify an environment, you _must_
909 * specify a complete environment. This function
910 * will not merge environments. Use
911 * appSetEnvironmentVar to change environment
912 * variables in a complete environment set.
913 *
914 * -- If PROGDETAILS specifies a Win-OS/2 session
915 * and PROGDETAILS.pszEnvironment is empty,
916 * this uses the default Win-OS/2 environment.
917 * See appQueryDefaultWin31Environment.
918 *
919 * Even though this isn't clearly said in PMREF,
920 * PROGDETAILS.swpInitial is important:
921 *
922 * -- To start a session minimized, set SWP_MINIMIZE.
923 *
924 * -- To start a VIO session with auto-close disabled,
925 * set the half-documented SWP_NOAUTOCLOSE flag (0x8000)
926 * This flag is now in the newer toolkit headers.
927 *
928 * In addition, this supports the following session
929 * flags with ulFlags if PROG_DEFAULT is specified:
930 *
931 * -- APP_RUN_FULLSCREEN
932 *
933 * -- APP_RUN_ENHANCED
934 *
935 * -- APP_RUN_STANDARD
936 *
937 * -- APP_RUN_SEPARATE
938 *
939 * Since this calls WinStartApp in turn, this
940 * requires a message queue on the calling thread.
941 *
942 * Note that this also does minimal checking on
943 * the specified parameters so it can return something
944 * more meaningful than FALSE like WinStartApp.
945 * As a result, you get a DOS error code now (V0.9.16).
946 *
947 * Most importantly:
948 *
949 * -- ERROR_INVALID_THREADID: not running on thread 1.
950 * Since this uses WinStartApp internally and
951 * WinStartApp completely hangs the session manager
952 * if a Win-OS/2 full-screen session is started from
953 * a thread that is NOT thread 1, this will now fail
954 * with this error for safety (V0.9.16).
955 *
956 * -- ERROR_INVALID_PARAMETER: pcProgDetails or
957 * phapp is NULL; or PROGDETAILS.pszExecutable is NULL.
958 *
959 * -- ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND:
960 * PROGDETAILS.pszExecutable and/or PROGDETAILS.pszStartupDir
961 * are invalid.
962 * A NULL PROGDETAILS.pszStartupDir is supported though.
963 *
964 * -- ERROR_NOT_ENOUGH_MEMORY
965 *
966 *@@added V0.9.6 (2000-10-16) [umoeller]
967 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
968 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
969 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
970 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
971 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
972 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
973 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
974 *@@changed V0.9.16 (2001-10-19) [umoeller]: added prototype to return APIRET
975 *@@changed V0.9.16 (2001-10-19) [umoeller]: added thread-1 check
976 *@@changed V0.9.16 (2001-12-06) [umoeller]: now using doshSearchPath for finding pszExecutable if not qualified
977 *@@changed V0.9.16 (2002-01-04) [umoeller]: removed error report if startup directory was drive letter only
978 *@@changed V0.9.16 (2002-01-04) [umoeller]: added more detailed error reports and *FailingName params
979 */
980
981APIRET appStartApp(HWND hwndNotify, // in: notify window or NULLHANDLE
982 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
983 ULONG ulFlags, // in: APP_RUN_* flags
984 HAPP *phapp, // out: application handle if NO_ERROR is returned
985 ULONG cbFailingName,
986 PSZ pszFailingName)
987{
988 APIRET arc = NO_ERROR;
989 PROGDETAILS ProgDetails;
990
991 if (pszFailingName)
992 *pszFailingName = '\0';
993
994 if (!pcProgDetails || !phapp)
995 return (ERROR_INVALID_PARAMETER);
996
997 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
998 // pointers still point into old prog details buffer
999 ProgDetails.Length = sizeof(PROGDETAILS);
1000 ProgDetails.progt.fbVisible = SHE_VISIBLE;
1001
1002 // all this only makes sense if this contains something...
1003 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
1004 if ( (!ProgDetails.pszExecutable)
1005 || (!(*(ProgDetails.pszExecutable)))
1006 )
1007 arc = ERROR_INVALID_PARAMETER;
1008 else if (doshMyTID() != 1) // V0.9.16 (2001-10-19) [umoeller]
1009 arc = ERROR_INVALID_THREADID;
1010 else
1011 {
1012 ULONG ulIsWinApp;
1013
1014 CHAR szFQExecutable[CCHMAXPATH];
1015
1016 XSTRING strParamsPatched;
1017 PSZ pszWinOS2Env = 0;
1018
1019 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
1020 // this wasn't a good idea... WPProgram stores stuff
1021 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
1022
1023 // duplicate parameters...
1024 // we need this for string manipulations below...
1025 if (ProgDetails.pszParameters)
1026 xstrInitCopy(&strParamsPatched,
1027 ProgDetails.pszParameters,
1028 100);
1029 else
1030 // no old params:
1031 xstrInit(&strParamsPatched, 100);
1032
1033 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
1034 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
1035 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
1036
1037 // program type fixups
1038 switch (ProgDetails.progt.progc) // that's a ULONG
1039 {
1040 case ((ULONG)-1): // we get that sometimes...
1041 case PROG_DEFAULT:
1042 {
1043 // V0.9.12 (2001-05-26) [umoeller]
1044 ULONG ulDosAppType;
1045 appQueryAppType(ProgDetails.pszExecutable,
1046 &ulDosAppType,
1047 &ProgDetails.progt.progc);
1048 }
1049 break;
1050 }
1051
1052 // set session type from option flags
1053 if (ulFlags & APP_RUN_FULLSCREEN)
1054 {
1055 if (ProgDetails.progt.progc == PROG_WINDOWABLEVIO)
1056 ProgDetails.progt.progc = PROG_FULLSCREEN;
1057
1058 if (ProgDetails.progt.progc == PROG_WINDOWEDVDM)
1059 ProgDetails.progt.progc = PROG_VDM;
1060 }
1061
1062 if (ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc))
1063 {
1064 if (ulFlags & APP_RUN_FULLSCREEN)
1065 ProgDetails.progt.progc = (ulFlags & APP_RUN_ENHANCED)
1066 ? PROG_31_ENH
1067 : PROG_31_STD;
1068 else
1069 {
1070 if (ulFlags & APP_RUN_STANDARD)
1071 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1072 ? PROG_31_STDSEAMLESSVDM
1073 : PROG_31_STDSEAMLESSCOMMON;
1074
1075 if (ulFlags & APP_RUN_ENHANCED)
1076 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1077 ? PROG_31_ENHSEAMLESSVDM
1078 : PROG_31_ENHSEAMLESSCOMMON;
1079 }
1080
1081 // re-run V0.9.16 (2001-10-19) [umoeller]
1082 ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc);
1083 }
1084
1085 /*
1086 * command lines fixups:
1087 *
1088 */
1089
1090 if (!strcmp(ProgDetails.pszExecutable, "*"))
1091 {
1092 /*
1093 * "*" for command sessions:
1094 *
1095 */
1096
1097 if (ulIsWinApp == 2)
1098 {
1099 // enhanced Win-OS/2 session:
1100 PSZ psz = NULL;
1101 if (strParamsPatched.ulLength)
1102 // "/3 " + existing params
1103 psz = strdup(strParamsPatched.psz);
1104
1105 xstrcpy(&strParamsPatched, "/3 ", 0);
1106
1107 if (psz)
1108 {
1109 xstrcat(&strParamsPatched, psz, 0);
1110 free(psz);
1111 }
1112 }
1113
1114 if (ulIsWinApp)
1115 {
1116 // cheat: WinStartApp doesn't support NULL
1117 // for Win-OS2 sessions, so manually start winos2.com
1118 ProgDetails.pszExecutable = "WINOS2.COM";
1119 // this is a DOS app, so fix this to DOS fullscreen
1120 ProgDetails.progt.progc = PROG_VDM;
1121 }
1122 else
1123 // for all other executable types
1124 // (including OS/2 and DOS sessions),
1125 // set pszExecutable to NULL; this will
1126 // have WinStartApp start a cmd shell
1127 ProgDetails.pszExecutable = NULL;
1128
1129 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
1130 else
1131 {
1132 // check if the executable is fully qualified; if so,
1133 // check if the executable file exists
1134 if ( (ProgDetails.pszExecutable[1] == ':')
1135 && (strchr(ProgDetails.pszExecutable, '\\'))
1136 )
1137 {
1138 ULONG ulAttr;
1139 if (!(arc = doshQueryPathAttr(ProgDetails.pszExecutable,
1140 &ulAttr)))
1141 {
1142 // make sure startup dir is really a directory
1143 if (ProgDetails.pszStartupDir)
1144 {
1145 // it is valid to specify a startup dir of "C:"
1146 if ( (strlen(ProgDetails.pszStartupDir) > 2)
1147 && (!(arc = doshQueryPathAttr(ProgDetails.pszStartupDir,
1148 &ulAttr)))
1149 && (!(ulAttr & FILE_DIRECTORY))
1150 )
1151 arc = ERROR_PATH_NOT_FOUND;
1152 }
1153 }
1154 }
1155 else
1156 {
1157 // _not_ fully qualified: look it up on the PATH then
1158 // V0.9.16 (2001-12-06) [umoeller]
1159 if (!(arc = doshSearchPath("PATH",
1160 ProgDetails.pszExecutable,
1161 szFQExecutable,
1162 sizeof(szFQExecutable))))
1163 // alright, found it:
1164 ProgDetails.pszExecutable = szFQExecutable;
1165 }
1166
1167 if (!arc)
1168 {
1169 PSZ pszExtension;
1170 switch (ProgDetails.progt.progc)
1171 {
1172 /*
1173 * .CMD files fixups
1174 *
1175 */
1176
1177 case PROG_FULLSCREEN: // OS/2 fullscreen
1178 case PROG_WINDOWABLEVIO: // OS/2 window
1179 {
1180 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1181 && (!stricmp(pszExtension, "CMD"))
1182 )
1183 {
1184 CallBatchCorrectly(&ProgDetails,
1185 &strParamsPatched,
1186 "OS2_SHELL",
1187 "CMD.EXE");
1188 }
1189 break; }
1190
1191 case PROG_VDM: // DOS fullscreen
1192 case PROG_WINDOWEDVDM: // DOS window
1193 {
1194 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1195 && (!stricmp(pszExtension, "BAT"))
1196 )
1197 {
1198 CallBatchCorrectly(&ProgDetails,
1199 &strParamsPatched,
1200 NULL,
1201 "COMMAND.COM");
1202 }
1203 break; }
1204 } // end switch (ProgDetails.progt.progc)
1205 }
1206 }
1207
1208 if (!arc)
1209 {
1210 if ( (ulIsWinApp)
1211 && ( (ProgDetails.pszEnvironment == NULL)
1212 || (!strlen(ProgDetails.pszEnvironment))
1213 )
1214 )
1215 {
1216 // this is a windoze app, and caller didn't bother
1217 // to give us an environment:
1218 // we MUST set one then, or we'll get the strangest
1219 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
1220
1221 DOSENVIRONMENT Env = {0};
1222
1223 // get standard WIN-OS/2 environment
1224 PSZ pszTemp = appQueryDefaultWin31Environment();
1225
1226 if (!(arc = appParseEnvironment(pszTemp,
1227 &Env)))
1228 {
1229 // now override KBD_CTRL_BYPASS=CTRL_ESC
1230 if ( (!(arc = appSetEnvironmentVar(&Env,
1231 "KBD_CTRL_BYPASS=CTRL_ESC",
1232 FALSE))) // add last
1233 && (!(arc = appConvertEnvironment(&Env,
1234 &pszWinOS2Env, // freed at bottom
1235 NULL)))
1236 )
1237 ProgDetails.pszEnvironment = pszWinOS2Env;
1238
1239 appFreeEnvironment(&Env);
1240 }
1241
1242 free(pszTemp);
1243 }
1244
1245 if (!arc)
1246 {
1247 if (!ProgDetails.pszTitle)
1248 ProgDetails.pszTitle = ProgDetails.pszExecutable;
1249
1250 ProgDetails.pszParameters = strParamsPatched.psz;
1251
1252 _Pmpf((__FUNCTION__ ": progt.progc: %d", ProgDetails.progt.progc));
1253 _Pmpf((" progt.fbVisible: 0x%lX", ProgDetails.progt.fbVisible));
1254 _Pmpf((" progt.pszTitle: \"%s\"", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : "NULL"));
1255 _Pmpf((" exec: \"%s\"", (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL"));
1256 _Pmpf((" params: \"%s\"", (ProgDetails.pszParameters) ? ProgDetails.pszParameters : "NULL"));
1257 _Pmpf((" startup: \"%s\"", (ProgDetails.pszStartupDir) ? ProgDetails.pszStartupDir : "NULL"));
1258 _Pmpf((" pszIcon: \"%s\"", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : "NULL"));
1259 /* _Pmpf((" environment: "));
1260 {
1261 PSZ pszThis = ProgDetails.pszEnvironment;
1262 while (pszThis && *pszThis)
1263 {
1264 _Pmpf((" \"%s\"", pszThis));
1265 pszThis += strlen(pszThis) + 1;
1266 }
1267 }
1268 */
1269
1270 _Pmpf((" swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
1271 ProgDetails.swpInitial.fl,
1272 ProgDetails.swpInitial.x,
1273 ProgDetails.swpInitial.y,
1274 ProgDetails.swpInitial.cx,
1275 ProgDetails.swpInitial.cy));
1276 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1277 ProgDetails.swpInitial.hwndInsertBehind,
1278 ProgDetails.swpInitial.hwnd,
1279 ProgDetails.swpInitial.ulReserved1,
1280 ProgDetails.swpInitial.ulReserved2));
1281
1282 if (pszFailingName)
1283 strhncpy0(pszFailingName, ProgDetails.pszExecutable, cbFailingName);
1284
1285 /* if (WinMessageBox(HWND_DESKTOP,
1286 NULLHANDLE,
1287 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
1288 "Start?",
1289 0,
1290 MB_YESNO | MB_MOVEABLE)
1291 != MBID_YES)
1292 arc = ERROR_INTERRUPT;
1293 else */
1294 {
1295 if (!(*phapp = WinStartApp(hwndNotify,
1296 // receives WM_APPTERMINATENOTIFY
1297 &ProgDetails,
1298 strParamsPatched.psz,
1299 NULL, // "reserved", PMREF says...
1300 SAF_INSTALLEDCMDLINE)))
1301 // we MUST use SAF_INSTALLEDCMDLINE
1302 // or no Win-OS/2 session will start...
1303 // whatever is going on here... Warp 4 FP11
1304
1305 // do not use SAF_STARTCHILDAPP, or the
1306 // app will be terminated automatically
1307 // when the WPS terminates!
1308 {
1309 // cannot start app:
1310 _Pmpf((__FUNCTION__ ": WinStartApp failed"));
1311 arc = ERROR_FILE_NOT_FOUND;
1312 // unfortunately WinStartApp doesn't
1313 // return meaningful codes like DosStartSession, so
1314 // try to see what happened
1315 /*
1316 switch (ERRORIDERROR(WinGetLastError(0)))
1317 {
1318 case PMERR_DOS_ERROR: // (0x1200)
1319 {
1320 arc = ERROR_FILE_NOT_FOUND;
1321
1322 // this is probably the case where the module
1323 // couldn't be loaded, so try DosStartSession
1324 // to get a meaningful return code... note that
1325 // this cannot handle hwndNotify then
1326 /* RESULTCODES result;
1327 arc = DosExecPgm(pszFailingName,
1328 cbFailingName,
1329 EXEC_ASYNC,
1330 NULL, // ProgDetails.pszParameters,
1331 NULL, // ProgDetails.pszEnvironment,
1332 &result,
1333 ProgDetails.pszExecutable);
1334 */
1335 /* ULONG sid, pid;
1336 STARTDATA SData;
1337 SData.Length = sizeof(STARTDATA);
1338 SData.Related = SSF_RELATED_CHILD; //INDEPENDENT;
1339 SData.FgBg = SSF_FGBG_FORE;
1340 SData.TraceOpt = SSF_TRACEOPT_NONE;
1341
1342 SData.PgmTitle = ProgDetails.pszTitle;
1343 SData.PgmName = ProgDetails.pszExecutable;
1344 SData.PgmInputs = ProgDetails.pszParameters;
1345
1346 SData.TermQ = NULL;
1347 SData.Environment = ProgDetails.pszEnvironment;
1348 SData.InheritOpt = SSF_INHERTOPT_PARENT; // ignored
1349 SData.SessionType = SSF_TYPE_DEFAULT;
1350 SData.IconFile = 0;
1351 SData.PgmHandle = 0;
1352
1353 SData.PgmControl = SSF_CONTROL_VISIBLE;
1354
1355 SData.InitXPos = 30;
1356 SData.InitYPos = 40;
1357 SData.InitXSize = 200;
1358 SData.InitYSize = 140;
1359 SData.Reserved = 0;
1360 SData.ObjectBuffer = pszFailingName;
1361 SData.ObjectBuffLen = cbFailingName;
1362
1363 arc = DosStartSession(&SData, &sid, &pid);
1364 }
1365 break;
1366
1367 case PMERR_INVALID_APPL: // (0x1530)
1368 // Attempted to start an application whose type is not
1369 // recognized by OS/2.
1370 arc = ERROR_INVALID_EXE_SIGNATURE;
1371 break;
1372
1373 case PMERR_INVALID_PARAMETERS: // (0x1208)
1374 // An application parameter value is invalid for
1375 // its converted PM type. For example: a 4-byte
1376 // value outside the range -32 768 to +32 767 cannot be
1377 // converted to a SHORT, and a negative number cannot
1378 // be converted to a ULONG or USHORT.
1379 arc = ERROR_INVALID_DATA;
1380 break;
1381
1382 case PMERR_STARTED_IN_BACKGROUND: // (0x1532)
1383 // The application started a new session in the
1384 // background.
1385 arc = ERROR_SMG_START_IN_BACKGROUND;
1386 break;
1387
1388 case PMERR_INVALID_WINDOW: // (0x1206)
1389 // The window specified with a Window List call
1390 // is not a valid frame window.
1391
1392 default:
1393 arc = ERROR_BAD_FORMAT;
1394 break;
1395 }
1396 */
1397 }
1398 }
1399 }
1400 }
1401
1402 xstrClear(&strParamsPatched);
1403 if (pszWinOS2Env)
1404 free(pszWinOS2Env);
1405 } // end if (ProgDetails.pszExecutable)
1406
1407 _Pmpf((__FUNCTION__ ": returning %d", arc));
1408
1409 return (arc);
1410}
1411
1412/*
1413 *@@ appWaitForApp:
1414 * waits for the specified application to terminate
1415 * and returns its exit code.
1416 *
1417 *@@added V0.9.9 (2001-03-07) [umoeller]
1418 */
1419
1420BOOL appWaitForApp(HWND hwndNotify, // in: notify window
1421 HAPP happ, // in: app to wait for
1422 PULONG pulExitCode) // out: exit code (ptr can be NULL)
1423{
1424 BOOL brc = FALSE;
1425
1426 if (happ)
1427 {
1428 // app started:
1429 // enter a modal message loop until we get the
1430 // WM_APPTERMINATENOTIFY for happ. Then we
1431 // know the app is done.
1432 HAB hab = WinQueryAnchorBlock(hwndNotify);
1433 QMSG qmsg;
1434 // ULONG ulXFixReturnCode = 0;
1435 while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
1436 {
1437 if ( (qmsg.msg == WM_APPTERMINATENOTIFY)
1438 && (qmsg.hwnd == hwndNotify)
1439 && (qmsg.mp1 == (MPARAM)happ)
1440 )
1441 {
1442 // xfix has terminated:
1443 // get xfix return code from mp2... this is:
1444 // -- 0: everything's OK, continue.
1445 // -- 1: handle section was rewritten, restart Desktop
1446 // now.
1447 if (pulExitCode)
1448 *pulExitCode = (ULONG)qmsg.mp2;
1449 brc = TRUE;
1450 // do not dispatch this
1451 break;
1452 }
1453
1454 WinDispatchMsg(hab, &qmsg);
1455 }
1456 }
1457
1458 return (brc);
1459}
1460
1461/*
1462 *@@ appQuickStartApp:
1463 * shortcut for simply starting an app and
1464 * waiting until it's finished.
1465 *
1466 * On errors, NULLHANDLE is returned.
1467 *
1468 * If pulReturnCode != NULL, it receives the
1469 * return code of the app.
1470 *
1471 *@@added V0.9.16 (2001-10-19) [umoeller]
1472 */
1473
1474HAPP appQuickStartApp(const char *pcszFile,
1475 ULONG ulProgType, // e.g. PROG_PM
1476 const char *pcszArgs,
1477 PULONG pulExitCode)
1478{
1479 PROGDETAILS pd = {0};
1480 HAPP happ,
1481 happReturn = NULLHANDLE;
1482 CHAR szDir[CCHMAXPATH] = "";
1483 PCSZ p;
1484 HWND hwndObject;
1485
1486 pd.Length = sizeof(pd);
1487 pd.progt.progc = ulProgType;
1488 pd.progt.fbVisible = SHE_VISIBLE;
1489 pd.pszExecutable = (PSZ)pcszFile;
1490 pd.pszParameters = (PSZ)pcszArgs;
1491 if (p = strrchr(pcszFile, '\\'))
1492 {
1493 strhncpy0(szDir,
1494 pcszFile,
1495 p - pcszFile);
1496 pd.pszStartupDir = szDir;
1497 }
1498
1499 if ( (hwndObject = winhCreateObjectWindow(WC_STATIC, NULL))
1500 && (!appStartApp(hwndObject,
1501 &pd,
1502 0,
1503 &happ,
1504 0,
1505 NULL))
1506 )
1507 {
1508 if (appWaitForApp(hwndObject,
1509 happ,
1510 pulExitCode))
1511 happReturn = happ;
1512 }
1513
1514 return (happReturn);
1515}
Note: See TracBrowser for help on using the repository browser.