1. Closing a DOS console in Windows 2000

This is a multi-part message in MIME format.

------=_NextPart_000_002B_01C0EC7C.9C0BE920
	charset="iso-8859-1"

Here is the code for anybody interested in launching a DOS console, waiting
for the DOS process to finish, then closing the console in Windows 2000,
i.e., using all forms of shell functions within Euphoria and win32lib don't
have the desired effect.  This code works in both Win 9x and Windows 2000.

------=_NextPart_000_002B_01C0EC7C.9C0BE920
	name="CreateProcessAPI Example.exw"
	filename="CreateProcessAPI Example.exw"

-- This routine will call a DOS console, suspend euphoria window (code),
-- then revert back to the window once the DOS console is done.
--
-- Of course, euphoria's system() does the same thing, but does not
-- work in Windows 2000: the euphoria program continues running even
-- if the DOS console is still running.  Calling system_exec() works
-- under 2000 but the console is not released when finished.
--
-- Please Note: the "/c" switch in the call to "command.com" will exit
-- the DOS console when finished.
--
-- Have to use the API CreateProcess() because it is the only function
-- that works with WaitForSingleObject(), given the context of the
-- requirement outlined above.
--
-- Jason Nickerson

include win32lib.ew
without warning

global constant TRUE                  =3D  1,
								FALSE                 =3D  0,
								NORMAL_PRIORITY_CLASS =3D 32,
								INFINITE              =3D -1

global constant
  cb                 =3D allot( DWord ),=09
  lpReserved         =3D allot( Ptr   ),=09
  lpDesktop          =3D allot( Ptr   ),	=20
  lpTitle            =3D allot( Ptr   ),=09
  dwX                =3D allot( DWord ),=09
  dwY                =3D allot( DWord ),=09
  dwXSize            =3D allot( DWord ),=09
  dwYSize            =3D allot( DWord ),=09
  dwXCountChars      =3D allot( DWord ),=09
  dwYCountChars      =3D allot( DWord ),
	dwFillAttribute    =3D allot( DWord ),
	dwFlags            =3D allot( DWord ),
	wShowWindow        =3D allot( Word  ),
	cbReserved2        =3D allot( Word  ),
	lpReserved2        =3D allot( Ptr   ),
	hStdInput          =3D allot( Hndl  ),
	hStdOutput         =3D allot( Hndl  ),
	hStdError          =3D allot( Hndl  ),
	SIZEOF_STARTUPINFO =3D allotted_size()=09
 =20
global constant
	hProcess           =3D allot( Hndl  ),
	hThread            =3D allot( Hndl  ),
	dwProcessId        =3D allot( DWord ),=09
  dwThreadId         =3D allot( DWord ),=09
	SIZEOF_PROCESSINFO =3D allotted_size()=09

object rv
global integer createProcess
global integer closeHandle
global integer waitProcess

constant  Win     =3D create ( Window,     "CreateProcess() Example",   =
0,         Default, Default, 255, 100, 0 ),
					btRun   =3D create ( PushButton, "Run Example",             Win,    =
          10,      10, 225,  50, 0 )

----------------------
-- onLoad_Win
----------------------

procedure onLoad_Win()
	atom k32

	k32 =3D open_dll("kernel32.dll")
	if k32 =3D 0 then
		rv =3D message_box(sprintf( "%s", { "FATAL ERROR opening =
\"kernel32.dll\" - Aborting . . ." } ), "CreateProcess", =
MB_ICONERROR+MB_TASKMODAL )
		abort(-1)
	end if

	createProcess =3D define_c_func(k32, "CreateProcessA",      {C_POINTER, =
C_POINTER, C_POINTER, C_POINTER, C_INT,
		C_LONG, C_POINTER, C_POINTER, C_POINTER, C_POINTER}, C_INT)
	closeHandle   =3D define_c_func(k32, "CloseHandle",         {C_LONG}, =
C_INT)
	waitProcess   =3D define_c_func(k32, "WaitForSingleObject", {C_LONG, =
C_LONG}, C_LONG)
end procedure

----------------------
-- onClick_btRun
----------------------

procedure onClick_btRun()

	atom memset1
	atom memset2
	atom memset3
	atom ptProcess
	atom ptStartup
	atom pCommand
	atom pCurrentDir

	memset1 =3D new_memset()
	ptProcess =3D acquire_mem(memset1, SIZEOF_PROCESSINFO )
	memset2 =3D new_memset()
	ptStartup =3D acquire_mem(memset2, SIZEOF_STARTUPINFO )
	memset3 =3D new_memset()
	pCommand    =3D acquire_mem(memset3, "command.com /c dir" )
	pCurrentDir =3D acquire_mem(memset3, "C:\\" )

	if not c_func(createProcess, { NULL, pCommand, NULL, NULL, TRUE, =
NORMAL_PRIORITY_CLASS, NULL, NULL, ptStartup, ptProcess } ) then
		rv =3D message_box(sprintf( "%s", { "Error starting CRC engine . . ." =
} ), "CreateProcess", MB_ICONERROR+MB_TASKMODAL )
		release_mem(memset1)
		release_mem(memset2)
		release_mem(memset3)
		return
	end if
	setVisible(Win, False) -- Don't necessarily need this but if not used, =
the window goes funny
	-- Now, wait for the DOS console to end before continuing on with the =
code
	rv =3D c_func(waitProcess,  { fetch(ptProcess, hProcess), INFINITE})
	rv =3D c_func(closeHandle,  { fetch(ptProcess, hProcess) })
	setVisible(Win, True)  -- Don't necessarily need this but if not used, =
the window goes funny
	release_mem(memset1)
	release_mem(memset2)
	release_mem(memset3)

end procedure

------------------
-- E V E N T S
------------------
onClick  [ btRun  ] =3D routine_id( "onClick_btRun"           )
onOpen   [ Win    ] =3D routine_id( "onLoad_Win"              )

-------------------------
-- START WINDOW
-------------------------
WinMain( Win, Normal )
------=_NextPart_000_002B_01C0EC7C.9C0BE920--

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu