- 浏览: 668612 次
- 性别:
- 来自: 中山
文章分类
最新评论
-
wuhuizhong:
jFinal支持Rest风格吗?可以想spring mvc那样 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
在jfinal中应如何获取前端ajax提交的Json数据?ht ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
jfinal如何处理json请求的数据:问题: 在某些api接 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
Ubuntu14.04 安装 Oracle 11g R2 Ex ...
Oracle 11g release 2 XE on Ubuntu 14.04 -
alanljj:
这个很实用,已成功更新,谢过了!
odoo薪酬管理模块l10n_cn_hr_payroll
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 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
结合脚本,其实可以做很多很多事情,大家自己发挥吧,例如创建目录,删除目录等。
总结
本文相比别的方法,解决了两大关键问题,一是隐藏执行,不会弹出讨厌的控制台窗口;二是主程序会等待被调用程序执行结束;
发表评论
-
Talend ESB Runtime查看服务的部署情况
2016-05-04 11:05 699在 Talend Runtime 中: Hit ' ... -
talend的常用控件使用教程
2016-05-04 06:53 24831、建立作业,然后在 ... -
精彩RSS源推荐
2016-04-12 11:21 10156科技类 36氪 http://www.36kr.co ... -
Anypoint Studio 社区版
2016-03-09 23:24 4464Anypoint Studio is just one o ... -
中山市住房公积金提取
2015-06-23 15:41 310中山市住房公积金提取 ================== ... -
用Base64把圖片轉成文字編碼
2014-12-24 09:59 832• 編碼工具: http://www.base64-imag ... -
应用Sphinx + LaTeX + reStructuredText制作PDF文件
2014-05-28 17:24 1679参考: 用Sphinx制作中文pdf http: ... -
開啟Eclipse問題處理一例
2012-03-21 08:27 881開啟Eclipse時報錯並提示查看.metadata\.log ... -
公司内网用eclipse自动更新
2012-03-20 13:27 1371新增eclipse.ini代理设置 -Dorg.eclips ... -
Git常用操作命令
2012-03-08 10:09 810Git常用操作命令收集: ... -
一个成功的Git分支模型
2012-03-04 15:30 380一个成功的Git分支模型. http://nvie.com/ ... -
Using KDiff3 as Git GUI Merge Tool on Windows XP and Windows 7
2012-03-03 05:45 3149Add the KDiff3 directory to yo ... -
使用DocBook
2011-12-22 04:11 1500参考: http://addcn.blogbus.co ... -
在windows上运行的SSH服务器-OpenSSH for Windows和Copssh
2011-11-02 08:31 29234下面介绍两款在windows上 ... -
How to install OpenSSH sshd server and sftp server on a Windows
2011-11-02 08:20 2599http://pigtail.net/LRP/printsrv ... -
敏捷开发修炼之道 - 读后感
2011-10-08 05:47 907高效程序员的45个习惯 敏捷开发修炼之道http://www. ... -
汇率数据
2011-10-06 09:24 2182外汇-人民币即时报价 WEB 服务http://webs ... -
VirtualBox for Windows下安装linux的网络设置
2011-01-24 01:21 3873举例: 主机(windows)设网络桥IP:192.168. ... -
linux单网卡上配置双IP
2010-10-10 19:38 7139/sysconfig/network-scripts在ne ... -
CentOS Linux修改IP地址, DNS, 网关
2010-10-10 19:31 3523一、CentOS 修改IP地址 修改对应网卡的IP地址的配置 ...
相关推荐
用PB控制运行其他程序并进行监控(源码) PB版本:9.0
API_HOOK_CreateProcess知道你运行哪些程序.zip
Delphi XE10 调用外部程序并等待其运行结束
通过使用APIhook拦截CreateProcess函数实现应用程序拦截。
用CreateProcess创建进程后,得到主窗口句柄。
Windows所创建的每个进程都从调用CreateProcess() API函数开始,该函数的任务是在对象管理器子系统内初始化进程对象。每一进程都以调用ExitProcess() 或TerminateProcess() API函数终止。通常应用程序的框架负责调用...
一个运用CREATEPROCESS()打开一个程序的实例
写入了两种打开程序的方法,代码里修改要打开的程序路径即可测试
通过进程打开另一个进程的三种方法:CreateProcess,WinExec,ShellExecute ,给出了调用的实现代码,源码中有CreateProcess,ShellExecute的函数说明,包含一个可执行的演示程序和源码 用VC2008编写的
通过写代码,attach程序中创建的其他进程,将其加入VS的Debugger,以方便调试
这是本人用C++ 在VS2019IDE 用控制台写的一个可以在程序启动前注入dll 和Hook系统api的函数。Hook的是knernelbase的CreateProcessInternalW函数,希望对大家有帮助
以CreateProcess为例运行系统程序并输出结果 以CreateProcess为例运行系统程序并输出结果 以CreateProcess为例运行系统程序并输出结果
MFC中用CreateProcess调用外部程序
① 掌握Windows进程的创建和销毁API的调用方法;编程代码,在程序中创建和销毁一个Word进程; ② 能够挂起和激活被创建进程的主线程; ③ 通过Windows进程管理器查看系统进程列表的变化。
CreateProcess函数 详细使用说明
VC++ 远程桌面控制、抓图源程序 visual c++ vc用win32 api实现 远程控制(socket tcp,抓屏截屏保存成文件,CreateProcess运行程序,操作注册表...
2.保证无毒 3.简单,方便,实用 4.实例可以自行改用 5.如有非法,本人无法律责任,由改动代码人负责! 6.需要更多本人作品,查找标签“朱建强” 7.请下载,杀毒后再使用!
用CreateProcess创建进程 用CreateProcess创建进程 用CreateProcess创建进程 用CreateProcess创建进程
windows下eclipse跑junit报错:CreateProcess error=206后面跟着乱码。 把这个jar替换掉就好了,放在:eclipse/plugins/下面。 参考:https://bugs.eclipse.org/bugs/show_bug.cgi?id=327193
使用CreateProcess()来创建进程 使用WaitForSingleObject()在"mytime”命令和新创建的进程之间同步 调用GetSystemTime()来获取时间 在Linux下实现: 使用fork()/exec()来创建进程运行程序 使用wait()等待新创建的...