PB 11 使用windows api CreateProcess 调用其他程序

本文深入解析了在PB环境中调用后台运行第三方程序的技术细节,包括使用CreateProcess API的方法、实例代码以及如何实现隐藏执行窗口和等待程序执行结束的功能。通过实例演示了如何利用RAR.exe在后台生成压缩文件,提供了实用的代码实现,适用于自动化任务和后台操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from:http://hi.baidu.com/lzycsd/blog/item/60ac860fce5fe0e7ab6457d5.html

 

在网上搜了搜大部分是这个方法

但是我发现在pb9中可以用,在pb11中不可以用

又查了查 原因是字符问题

把下面例子中的CreateProcessA换成 CreateProcessW即可

createprocess,createprocessA,createprocessW的区别http://hi.baidu.com/lzycsd/blog/item/41ef76e7224d3424b93820cd.html

CreateProcess api的用法见

http://hi.baidu.com/lzycsd/blog/item/204f730f388dcc206059f3e6.html

返回错误码见

http://hi.baidu.com/lzycsd/blog/item/54053f95be1119027bf4801c.html

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 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命令,就可以在后台实现很多操作;
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
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.cbreserved2 = 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);
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
三、实用案例
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
if fileexists(as_targetpath+'\'+as_targetfile ) then
   return TRUE
else
   return FALSE // 生成压缩文件失败
end if
else
return FALSE // 调用api返回失败
end if
结合脚本,其实可以做很多很多事情,大家自己发挥吧,例如创建目录,删除目录等。
总结
本文相比别的方法,解决了两大关键问题,一是隐藏执行,不会弹出讨厌的控制台窗口;二是主程序会等待被调用程序执行结束;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值