pb调用其他程序总结
前言
在pb中经常需要运行第三方程序,例如打开计算器,记事本等,外部程序调用方法不外乎以下三种:
1、直接用run;
优点是使用简单方便,pb自带函数,缺点跟优点一样明显,无法隐藏执行窗口,无法控制程序执行过程等(当然也可以用findwindow等方式判断,总体来说不是很合理);
2、调用api,ShellExecute;
把程序执行方式交给操作系统处理,简单来说,例如操作系统里文本文件默认用记事本打开,网址文件用ie打开,运行网址文件,系统会自动调用ie打开网址;
3、调用api,CreateProcess;
优点:配合其他api,可以控制程序的执行过程,也可以隐藏执行,可以设置标题等;
缺点:使用复杂,需要定义一大堆api函数;
方法一、方法二网上资料很多,唯独方法三资料比较少,而且错误百出;综合很多网友的资料,查了msdn,反复实践,总算总结出来,下面开始罗列;
正文
一、定义api
Function BOOLEAN GetExitCodeProcess( LONG hProcess,REF ULONG lpExitCode ) LIBRARY "KERNEL32.DLL"
Function ULONG GetLastError() 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"
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
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
long hprocess
long hthread
unsignedlong dwprocessid
unsignedlong dwthreadid
end type
二、实现代码
注意红色字体代码处,这是经过反复测试才得出来的,使用这些函数,配合一些dos命令,就可以在后台实现很多操作;
public function boolean create_process_wait (string as_executable, string as_commandparm, boolean ab_yield, boolean ab_show);
//====================================================================
// 事件: nvo_ftp.Properties - nvo_ftp inherited from nonvisualobject()
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
// 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
// 事件: nvo_ftp.Properties - nvo_ftp inherited from nonvisualobject()
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
// 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
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 IF
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 IF
sinfo.cbreserved2 = 0
sinfo.lpReserved2 = 0
sinfo.hStdInput = 0
sinfo.hStdOutput = 0
sinfo.hStdError = 0
sinfo.lpReserved2 = 0
sinfo.hStdInput = 0
sinfo.hStdOutput = 0
sinfo.hStdError = 0
Boolean bRet
bRet = CreateProcess(as_executable,as_executable + " " + as_commandparm,0,0,False,32,0,0,sinfo,pinfo)
//bRet = CreateProcess(NULL,cmd,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&TStartupInfo,&TProcessInformation);
bRet = CreateProcess(as_executable,as_executable + " " + as_commandparm,0,0,False,32,0,0,sinfo,pinfo)
//bRet = CreateProcess(NULL,cmd,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&TStartupInfo,&TProcessInformation);
IF Not bRet THEN
MessageBox("警告", "创建子进程错误,错误码:"+String(GetLastError()))
RETURN False
END IF
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 )
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
三、实用案例
1、利用rar.exe在后台生成压缩文件
// 控制台下命令如下: rar.exe a 2007.rar *.txt
String ls_command,ls_comm_para,ls_curr_path
String as_targetfile ,as_sourcefile,as_targetpath
as_targetfile = "test.rar"
as_sourcefile = "*.txt"
as_targetpath = ".\rarfile"
ls_command = "rar.exe " // 这个文件在winrar的安装目录里有,到处都能找到
ls_comm_para= " a " +as_targetpath+'\'+as_targetfile +" "+as_sourcefile
if create_process_wait(ls_command,ls_comm_para,true,FALSE) = TRUE then
ls_comm_para= " a " +as_targetpath+'\'+as_targetfile +" "+as_sourcefile
if create_process_wait(ls_command,ls_comm_para,true,FALSE) = TRUE then
if fileexists(as_targetpath+'\'+as_targetfile ) then
return TRUE
else
return FALSE // 生成压缩文件失败
end if
return TRUE
else
return FALSE // 生成压缩文件失败
end if
else
return FALSE // 调用api返回失败
end if
结合脚本,其实可以做很多很多事情,大家自己发挥吧,例如创建目录,删除目录等。
总结
本文相比别的方法,解决了两大关键问题,一是隐藏执行,不会弹出讨厌的控制台窗口;二是主程序会等待被调用程序执行结束;
<--- END --->