当前位置: 代码迷 >> PB >> 配合DOS命令实现后台执行,但是提示有异常,请大家帮助
  详细解决方案

配合DOS命令实现后台执行,但是提示有异常,请大家帮助

热度:12   发布时间:2016-04-29 08:15:38.0
配合DOS命令实现后台执行,但是提示有错误,请大家帮助!
一、定义apiFunction BOOLEAN GetExitCodeProcess( LONG hProcess,REF ULONG lpExitCode ) LIBRARY "KERNEL32.DLL" 
Function ULONG GetLastError() Library "KERNEL32.DLL"Function BOOLEAN CreateProcess ( STRING lpApplicationName, & 
STRING lpCommandLine, & 
ULONG lpProcessAttributes, & 
ULONG lpThreadAttributes, & 
BOOLEAN bInheritHandles, & 
ULONG dwCreationFlags, & 
ULONG lpEnvironment, & 
ULONG lpCurrentDirectory, & 
REF STARTUPINFO lpStartupInfo, & 
REF PROCESS_INFORMATION lpProcessInformation ) & 
LIBRARY "KERNEL32.DLL" Alias for "CreateProcessA" 
FUNCTION ulong WaitForSingleObject(ulong hHandle,ulong dwMilliseconds) LIBRARY "kernel32.dll"

二、结构定义如下:

type startupinfo from structure
unsignedlong cb 
unsignedlong lpreserved 
unsignedlong lpdesktop 
unsignedlong lptitle 
unsignedlong dwx 
unsignedlong dwy 
unsignedlong dwxsize 
unsignedlong dwysize 
unsignedlong dwxcountchars 
unsignedlong dwycountchars 
unsignedlong dwfillattribute 
unsignedlong dwflags 
unsignedlong wshowwindow
unsignedlong cbreserved2 
unsignedlong lpreserved2 
unsignedlong hstdinput 
unsignedlong hstdoutput 
unsignedlong hstderror 
end type

type process_information from structure
long hprocess 
long hthread 
unsignedlong dwprocessid 
unsignedlong dwthreadid 
end type

三、实现代码

配合一些dos命令,就可以在后台实现很多操作

function boolean create_process_wait()

//====================================================================
// 描述:
//--------------------------------
// 参数:
// value string as_executable 执行命令
// value string as_commandparm 命令参数
// value boolean ab_yield 等待返回时,是否释放cpu
// value boolean ab_show 是否显示执行窗口 
//--------------------------------
// 举例:
// create_process_wait(ls_command,ls_comm_para,TRUE,FALSE)
//--------------------------------
// 返回: (none)
//--------------------------------
// 作者: Joshua Zou 日期: 2008年02月18日
//--------------------------------
// Copyright (c) 2002-2007 , All rights reserved.
//--------------------------------
// 修改历史:
//
//====================================================================
STARTUPINFO sinfo
PROCESS_INFORMATION pinfo

Constant Long SW_HIDE =0
Constant Long SW_NORMAL =1


sinfo.cb = 4 * 17
sinfo.lpReserved = 0 // NULL 
sinfo.lpDesktop = 0 // NULL 
sinfo.lpTitle = 0 // NULL 
sinfo.dwX = 0
sinfo.dwY = 0
sinfo.dwXSize = 0
sinfo.dwYSize = 0
sinfo.dwXCountChars = 0
sinfo.dwYCountChars = 0
sinfo.dwFillAttribute = 0
sinfo.dwFlags = 1
IF ab_show THEN
sinfo.wShowWindow = SW_NORMAL
ELSE
sinfo.wShowWindow = SW_HIDE // 默认隐藏执行窗口
END IFsinfo.cbreserved2 = 0
sinfo.lpReserved2 = 0
sinfo.hStdInput = 0
sinfo.hStdOutput = 0
sinfo.hStdError = 0Boolean bRet
bRet = CreateProcess(as_executable,as_executable + " " + as_commandparm,0,0,False,32,0,0,sinfo,pinfo)
IF Not bRet THEN
MessageBox("警告", "创建子进程错误,错误码:"+String(GetLastError()))
RETURN False
END IF
ULong lpExitCode
DO
// 等待结束
WaitForSingleObject(pinfo.hProcess,0)
bRet = GetExitCodeProcess ( pinfo.hProcess, lpExitCode )
IF ab_yield THEN Yield() //等待返回循环中,释放cpu
LOOP Until ( bRet = True And lpExitCode <> 259 )RETURN True
  相关解决方案