advapi32: Make sure lpDisplayName is initialized in EnumServicesStatusA.
[wine.git] / programs / winevdm / winevdm.c
blob04f2953808685f919cc9defd80e51902cd113e3a
1 /*
2 * Wine virtual DOS machine
4 * Copyright 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "winuser.h"
32 #include "wincon.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
38 extern void __wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline );
41 /*** PIF file structures ***/
42 #include "pshpack1.h"
44 /* header of a PIF file */
45 typedef struct {
46 BYTE unk1[2]; /* 0x00 */
47 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
48 WORD memmax; /* 0x20 */
49 WORD memmin; /* 0x22 */
50 CHAR program[63]; /* 0x24 seems to be zero terminated */
51 BYTE hdrflags1; /* 0x63 various flags:
52 * 02 286: text mode selected
53 * 10 close window at exit
55 BYTE startdrive; /* 0x64 */
56 char startdir[64]; /* 0x65 */
57 char optparams[64]; /* 0xa5 seems to be zero terminated */
58 BYTE videomode; /* 0xe5 */
59 BYTE unkn2; /* 0xe6 ?*/
60 BYTE irqlow; /* 0xe7 */
61 BYTE irqhigh; /* 0xe8 */
62 BYTE rows; /* 0xe9 */
63 BYTE cols; /* 0xea */
64 BYTE winY; /* 0xeb */
65 BYTE winX; /* 0xec */
66 WORD unkn3; /* 0xed 7??? */
67 CHAR unkn4[64]; /* 0xef */
68 CHAR unkn5[64]; /* 0x12f */
69 BYTE hdrflags2; /* 0x16f */
70 BYTE hdrflags3; /* 0x170 */
71 } pifhead_t;
73 /* record header: present on every record */
74 typedef struct {
75 CHAR recordname[16]; /* zero terminated */
76 WORD posofnextrecord; /* file offset, 0xffff if last */
77 WORD startofdata; /* file offset */
78 WORD sizeofdata; /* data is expected to follow directly */
79 } recordhead_t;
81 /* 386 -enhanced mode- record */
82 typedef struct {
83 WORD memmax; /* memory desired, overrides the pif header*/
84 WORD memmin; /* memory required, overrides the pif header*/
85 WORD prifg; /* foreground priority */
86 WORD pribg; /* background priority */
87 WORD emsmax; /* EMS memory limit */
88 WORD emsmin; /* EMS memory required */
89 WORD xmsmax; /* XMS memory limit */
90 WORD xmsmin; /* XMS memory required */
91 WORD optflags; /* option flags:
92 * 0008 full screen
93 * 0004 exclusive
94 * 0002 background
95 * 0001 close when active
97 WORD memflags; /* various memory flags*/
98 WORD videoflags; /* video flags:
99 * 0010 text
100 * 0020 med. res. graphics
101 * 0040 hi. res. graphics
103 WORD hotkey[9]; /* Hot key info */
104 CHAR optparams[64]; /* optional params, replaces those in the pif header */
105 } pif386rec_t;
107 #include "poppack.h"
109 /***********************************************************************
110 * find_dosbox
112 static char *find_dosbox(void)
114 const char *envpath = getenv( "PATH" );
115 struct stat st;
116 char *path, *p, *buffer, *dir;
117 size_t envpath_len;
119 if (!envpath) return NULL;
121 envpath_len = strlen( envpath );
122 path = HeapAlloc( GetProcessHeap(), 0, envpath_len + 1 );
123 buffer = HeapAlloc( GetProcessHeap(), 0, envpath_len + sizeof("/dosbox") );
124 strcpy( path, envpath );
126 p = path;
127 while (*p)
129 while (*p == ':') p++;
130 if (!*p) break;
131 dir = p;
132 while (*p && *p != ':') p++;
133 if (*p == ':') *p++ = 0;
134 strcpy( buffer, dir );
135 strcat( buffer, "/dosbox" );
136 if (!stat( buffer, &st ))
138 HeapFree( GetProcessHeap(), 0, path );
139 return buffer;
142 HeapFree( GetProcessHeap(), 0, buffer );
143 HeapFree( GetProcessHeap(), 0, path );
144 return NULL;
148 /***********************************************************************
149 * start_dosbox
151 static void start_dosbox( const char *appname, const char *args )
153 static const WCHAR cfgW[] = {'c','f','g',0};
154 const char *config_dir = wine_get_config_dir();
155 WCHAR path[MAX_PATH], config[MAX_PATH];
156 HANDLE file;
157 char *p, *buffer;
158 int i;
159 int ret = 1;
160 DWORD written, drives = GetLogicalDrives();
161 char *dosbox = find_dosbox();
163 if (!dosbox) return;
164 if (tolower(appname[0]) == 'z')
166 WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", appname );
167 WINE_MESSAGE( " because DOSBox doesn't support running from the Z: drive.\n" );
168 ExitProcess(1);
170 if (!GetTempPathW( MAX_PATH, path )) return;
171 if (!GetTempFileNameW( path, cfgW, 0, config )) return;
172 if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
173 file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
174 if (file == INVALID_HANDLE_VALUE) return;
176 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
177 25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
178 4 * strlenW( path ) +
179 6 + strlen( appname ) + strlen( args ) + 20 );
180 p = buffer;
181 p += sprintf( p, "[autoexec]\n" );
182 for (i = 0; i < 25; i++)
183 if (drives & (1 << i))
184 p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
185 p += sprintf( p, "%c:\ncd ", path[0] );
186 p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
187 p += sprintf( p, "\n%s %s\n", appname, args );
188 p += sprintf( p, "exit\n" );
189 if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
191 const char *args[4];
192 char *config_file = wine_get_unix_file_name( config );
193 args[0] = dosbox;
194 args[1] = "-conf";
195 args[2] = config_file;
196 args[3] = NULL;
197 ret = spawnvp( _P_WAIT, args[0], args );
199 CloseHandle( file );
200 DeleteFileW( config );
201 HeapFree( GetProcessHeap(), 0, buffer );
202 ExitProcess( ret );
206 /***********************************************************************
207 * start_dos_exe
209 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
211 MEMORY_BASIC_INFORMATION mem_info;
212 const char *reason;
214 if (VirtualQuery( NULL, &mem_info, sizeof(mem_info) ) && mem_info.State != MEM_FREE)
216 __wine_load_dos_exe( filename, cmdline );
217 if (GetLastError() == ERROR_NOT_SUPPORTED)
218 reason = "because vm86 mode is not supported on this platform";
219 else
220 reason = wine_dbg_sprintf( "It failed with error code %u", GetLastError() );
222 else reason = "because the DOS memory range is unavailable";
224 start_dosbox( filename, cmdline );
226 WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", filename );
227 WINE_MESSAGE( " %s.\n", reason );
228 WINE_MESSAGE( " Try running this application with DOSBox.\n" );
229 ExitProcess(1);
232 /***********************************************************************
233 * read_pif_file
234 *pif386rec_tu
235 * Read a pif file and return the header and possibly the 286 (real mode)
236 * record or 386 (enhanced mode) record. Returns FALSE if the file is
237 * invalid otherwise TRUE.
239 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
240 char *optparams, char *startdir, int *closeonexit, int *textmode)
242 DWORD nread;
243 LARGE_INTEGER filesize;
244 recordhead_t rhead;
245 BOOL found386rec = FALSE;
246 pif386rec_t pif386rec;
247 pifhead_t pifheader;
248 if( !GetFileSizeEx( hFile, &filesize) ||
249 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
250 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
251 return FALSE;
253 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
254 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
255 return FALSE;
256 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
257 wine_dbgstr_a(pifheader.program),
258 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
259 wine_dbgstr_a(pifheader.startdir),
260 wine_dbgstr_a(pifheader.optparams));
261 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
262 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
263 pifheader.videomode);
264 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
265 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
266 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
267 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
268 WINE_ERR("Invalid pif file: magic string not found\n");
269 return FALSE;
271 /* now process the following records */
272 while( 1) {
273 WORD nextrecord = rhead.posofnextrecord;
274 if( (nextrecord & 0x8000) ||
275 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
276 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
277 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
278 return FALSE;
279 if( !rhead.recordname[0]) continue; /* deleted record */
280 WINE_TRACE("reading record %s size %d next 0x%x\n",
281 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
282 rhead.posofnextrecord );
283 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
284 found386rec = TRUE;
285 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
286 WINE_TRACE("386rec: memory req'd %d des'd %d EMS req'd %d des'd %d XMS req'd %d des'd %d\n",
287 pif386rec.memmin, pif386rec.memmax,
288 pif386rec.emsmin, pif386rec.emsmax,
289 pif386rec.xmsmin, pif386rec.xmsmax);
290 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
291 pif386rec.optflags, pif386rec.memflags,
292 pif386rec.videoflags);
293 WINE_TRACE("386rec: optional parameters %s\n",
294 wine_dbgstr_a(pif386rec.optparams));
297 /* prepare the return data */
298 strncpy( progname, pifheader.program, sizeof(pifheader.program));
299 memcpy( title, pifheader.windowtitle, sizeof(pifheader.windowtitle));
300 title[ sizeof(pifheader.windowtitle) ] = '\0';
301 if( found386rec)
302 strncpy( optparams, pif386rec.optparams, sizeof( pif386rec.optparams));
303 else
304 strncpy( optparams, pifheader.optparams, sizeof(pifheader.optparams));
305 strncpy( startdir, pifheader.startdir, sizeof(pifheader.startdir));
306 *closeonexit = pifheader.hdrflags1 & 0x10;
307 *textmode = found386rec ? pif386rec.videoflags & 0x0010
308 : pifheader.hdrflags1 & 0x0002;
309 return TRUE;
312 /***********************************************************************
313 * pif_cmd
315 * execute a pif file.
317 static VOID pif_cmd( char *filename, char *cmdline)
319 HANDLE hFile;
320 char progpath[MAX_PATH];
321 char buf[128];
322 char progname[64];
323 char title[31];
324 char optparams[64];
325 char startdir[64];
326 char *p;
327 int closeonexit;
328 int textmode;
329 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
330 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
332 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
333 return;
335 if( !read_pif_file( hFile, progname, title, optparams, startdir,
336 &closeonexit, &textmode)) {
337 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
338 CloseHandle( hFile);
339 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
340 filename);
341 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
342 SetLastError( ERROR_BAD_FORMAT);
343 return;
345 CloseHandle( hFile);
346 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
347 WINE_FIXME(".bat programs in pif files are not supported.\n");
348 /* first change dir, so the search below can start from there */
349 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
350 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
351 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
352 filename);
353 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
355 /* search for the program */
356 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
357 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
358 filename);
359 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
360 SetLastError( ERROR_FILE_NOT_FOUND);
361 return;
363 if( textmode)
364 if( AllocConsole())
365 SetConsoleTitleA( title) ;
366 /* if no arguments on the commandline, use them from the pif file */
367 if( !cmdline[0] && optparams[0])
368 cmdline = optparams;
369 /* FIXME: do something with:
370 * - close on exit
371 * - graphic modes
372 * - hot key's
373 * - etc.
375 start_dos_exe( progpath, cmdline );
378 /***********************************************************************
379 * build_command_line
381 * Build the command line of a process from the argv array.
382 * Copied from ENV_BuildCommandLine.
384 static char *build_command_line( char **argv )
386 int len;
387 char *p, **arg, *cmd_line;
389 len = 0;
390 for (arg = argv; *arg; arg++)
392 int has_space,bcount;
393 char* a;
395 has_space=0;
396 bcount=0;
397 a=*arg;
398 if( !*a ) has_space=1;
399 while (*a!='\0') {
400 if (*a=='\\') {
401 bcount++;
402 } else {
403 if (*a==' ' || *a=='\t') {
404 has_space=1;
405 } else if (*a=='"') {
406 /* doubling of '\' preceding a '"',
407 * plus escaping of said '"'
409 len+=2*bcount+1;
411 bcount=0;
413 a++;
415 len+=(a-*arg)+1 /* for the separating space */;
416 if (has_space)
417 len+=2; /* for the quotes */
420 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
421 return NULL;
423 p = cmd_line;
424 *p++ = (len < 256) ? len : 255;
425 for (arg = argv; *arg; arg++)
427 int has_space,has_quote;
428 char* a;
430 /* Check for quotes and spaces in this argument */
431 has_space=has_quote=0;
432 a=*arg;
433 if( !*a ) has_space=1;
434 while (*a!='\0') {
435 if (*a==' ' || *a=='\t') {
436 has_space=1;
437 if (has_quote)
438 break;
439 } else if (*a=='"') {
440 has_quote=1;
441 if (has_space)
442 break;
444 a++;
447 /* Now transfer it to the command line */
448 if (has_space)
449 *p++='"';
450 if (has_quote) {
451 int bcount;
452 char* a;
454 bcount=0;
455 a=*arg;
456 while (*a!='\0') {
457 if (*a=='\\') {
458 *p++=*a;
459 bcount++;
460 } else {
461 if (*a=='"') {
462 int i;
464 /* Double all the '\\' preceding this '"', plus one */
465 for (i=0;i<=bcount;i++)
466 *p++='\\';
467 *p++='"';
468 } else {
469 *p++=*a;
471 bcount=0;
473 a++;
475 } else {
476 strcpy(p,*arg);
477 p+=strlen(*arg);
479 if (has_space)
480 *p++='"';
481 *p++=' ';
483 if (len) p--; /* remove last space */
484 *p = '\0';
485 return cmd_line;
489 /***********************************************************************
490 * usage
492 static void usage(void)
494 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
495 ExitProcess(1);
499 /***********************************************************************
500 * main
502 int main( int argc, char *argv[] )
504 DWORD count;
505 HINSTANCE16 instance;
506 LOADPARAMS16 params;
507 WORD showCmd[2];
508 char buffer[MAX_PATH];
509 STARTUPINFOA info;
510 char *cmdline, *appname, **first_arg;
511 char *p;
513 if (!argv[1]) usage();
515 if (!strcmp( argv[1], "--app-name" ))
517 if (!(appname = argv[2])) usage();
518 first_arg = argv + 3;
520 else
522 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
524 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
525 ExitProcess(1);
527 appname = buffer;
528 first_arg = argv + 1;
531 if (*first_arg) first_arg++; /* skip program name */
532 cmdline = build_command_line( first_arg );
534 if (WINE_TRACE_ON(winevdm))
536 int i;
537 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
538 WINE_TRACE( "appname = '%s'\n", appname );
539 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
540 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
543 GetStartupInfoA( &info );
544 showCmd[0] = 2;
545 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
547 params.hEnvironment = 0;
548 params.cmdLine = MapLS( cmdline );
549 params.showCmd = MapLS( showCmd );
550 params.reserved = 0;
552 RestoreThunkLock(1); /* grab the Win16 lock */
554 /* some programs assume mmsystem is always present */
555 LoadLibrary16( "gdi.exe" );
556 LoadLibrary16( "user.exe" );
557 LoadLibrary16( "mmsystem.dll" );
559 if ((instance = LoadModule16( appname, &params )) < 32)
561 if (instance == 11)
563 /* first see if it is a .pif file */
564 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
565 pif_cmd( appname, cmdline + 1);
566 else
568 /* try DOS format */
569 /* loader expects arguments to be regular C strings */
570 start_dos_exe( appname, cmdline + 1 );
572 /* if we get back here it failed */
573 instance = GetLastError();
576 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
577 switch (instance)
579 case 2: WINE_MESSAGE("file not found\n" ); break;
580 case 11: WINE_MESSAGE("invalid program file\n" ); break;
581 default: WINE_MESSAGE("error=%d\n", instance ); break;
583 ExitProcess(instance);
586 /* wait forever; the process will be killed when the last task exits */
587 ReleaseThunkLock( &count );
588 Sleep( INFINITE );
589 return 0;