前往Shuct.Net首页

Shudepb PB反编译专家长时间以来,为业内同类软件事实上的唯一选择.细节,彰显专业.态度,决定品质.

关于PowerBuilder的搜索

PB 11 使用windows api CreateProcess 调用其他程序 (调用cmd不显示窗口)_吕振_百度空间 相册 广场游戏 登录注册 关注此空间 吕振世界上有10种人,一种是看得懂2进制的,另一种是看不懂的. 2009-09-21 15:37 PB 11 使用windows api CreateProcess 调用其他程序 (调用cmd不显示窗口) 在网上搜了搜大部分是这个方法但是我发现在pb9中可以用,在pb11中不可以用又查了查 原因是字符问题把下面例子中的CreateProcessA换成 CreateProcessW即可createprocess,createprocessA,createprocessW的区别见 http://hi.baidu.com/lzycsd/blog/item/41ef76e7224d3424b93820cd.htmlCreateProcess api的用法见http://hi.baidu.com/lzycsd/blog/item/204f730f388dcc206059f3e6.html返回错误码见http://hi.baidu.com/lzycsd/blog/item/54053f95be1119027bf4801c.htmlpb调用其他程序总结 前言在pb中经常需要运行第三方程序,例如打开计算器,记事本等,外部程序调用方法不外乎以下三种:1、直接用run; 优点是使用简单方便,pb自带函数,缺点跟优点一样明显,无法隐藏执行窗口,无法控制程序执行过程等(当然也可以用findwindow等方式判断,总体来说不是很合理);2、调用api,ShellExecute; 把程序执行方式交给操作系统处理,简单来说,例如操作系统里文本文件默认用记事本打开,网址文件用ie打开,运行网址文件,系统会自动调用ie打开网址;3、调用api,CreateProcess; 优点:配合其他api,可以控制程序的执行过程,也可以隐藏执行,可以设置标题等; 缺点:使用复杂,需要定义一大堆api函数; 方法一、方法二网上资料很多,唯独方法三资料比较少,而且错误百出;综合很多网友的资料,查了msdn,反复实践,总算总结出来,下面开始罗列; 正文 一、定义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 typetype 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 sinfoPROCESS_INFORMATION pinfosinfo.cb = 4 * 17sinfo.lpReserved = 0 // NULL sinfo.lpDesktop = 0 // NULL sinfo.lpTitle = 0 // NULL sinfo.dwX = 0sinfo.dwY = 0sinfo.dwXSize = 0sinfo.dwYSize = 0sinfo.dwXCountChars = 0sinfo.dwYCountChars = 0sinfo.dwFillAttribute = 0sinfo.dwFlags = 1IF ab_show THEN sinfo.wShowWindow = SW_NORMALELSE sinfo.wShowWindow = SW_HIDE // 默认隐藏执行窗口END IFsinfo.cbreserved2 = 0sinfo.lpReserved2 = 0sinfo.hStdInput = 0sinfo.hStdOutput = 0sinfo.hStdError = 0Boolean bRetbRet = 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 FalseEND IFULong lpExitCodeDO // 等待结束 WaitForSingleObject(pinfo.hProcess,0) bRet = GetExitCodeProcess ( pinfo.hProcess, lpExitCode ) IF ab_yield THEN Yield() //等待返回循环中,释放cpuLOOP Until ( bRet = True And lpExitCode <> 259 )RETURN True 三、实用案例1、利用rar.exe在后台生成压缩文件// 控制台下命令如下: rar.exe a 2007.rar *.txtString ls_command,ls_comm_para,ls_curr_pathString 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_sourcefileif 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 ifelse return FALSE // 调用api返回失败end if结合脚本,其实可以做很多很多事情,大家自己发挥吧,例如创建目录,删除目录等。 总结本文相比别的方法,解决了两大关键问题,一是隐藏执行,不会弹出讨厌的控制台窗口;二是主程序会等待被调用程序执行结束; <--- END ---> #pb;PowerBuilder 分享到: 举报 浏览(2047) 评论 转载 你可能也喜欢 溪山清远 【502设计】夜猫子来了 学画奔马 愚悟草堂发布的文字 【漆画问答】漆画为什么要大量运用铝箔? 为著名学者 艺术评论家 画家 杨悦浦 先生刻的印章 乐水爱山 杨 悦浦 【转】在java中使用Jawin组件访问Windows下的COM组件 java调用ole 本文最近访客 hx_xiaochao 北极以北5 lao_he xhfire l2ice xt_chaoji catface0511 科洛蒂娅公主 评论 帮助中心 | 空间客服 | 投诉中心 | 空间协议&copy;2014 Baidu