找回密码
 加入
搜索
查看: 2400|回复: 3

求如何用CreateProcess打开指定程序

[复制链接]
发表于 2009-2-3 11:04:32 | 显示全部楼层 |阅读模式
因为AU3自己带的SHELLEXECUTE有局限性,要写一个程序来批量运行程序,有的程序要隐藏窗口运行,有的程序要等待它结束后再执行下一个,所以如果要写,就要写四个语句,两个是SHELLEXECUTE,两个是SHELLEXECUTEWAIT,非常的麻烦,用WINAPI的SHELLEXECUTE的返回值也不能用在WaitForSingleObject,所以要改其它方法,谁会的写一下谢谢
 楼主| 发表于 2009-2-3 16:52:24 | 显示全部楼层
自己写了个UDF如下,但程序执行时没反应,不知道是哪儿错了,会不会是PTR的参数不能是""呢?那怎么写上NULL啊
Local Const $tagSTARTUPINFO="int Size;ptr Reserved1;ptr Desktop;ptr Title;int X;int Y;int XSize;int YSize;int XCountChars;" & _
                "int YCountChars;int FillAttribute;int Flags;short ShowWindow;short Reserved2;ptr Reserved3;int StdInput;" & _
                "int StdOutput;int StdError"
Local Const $tagPROCESS_INFORMATION="hwnd hProcess;hwnd hThread;int ProcessID;int ThreadID"
Local Const $SW_SHOW=1

Func _UID_CreateProcess($sCommand,$sDir,$ShowMode=$SW_SHOW)
        ;参数1:命令行;参数2:运行路径;参数3:显示模式,默认是显示
        Local $pAppName, $tAppName, $pCommand, $tCommand, $pDir, $tDir, $aResult
        Local $SI=DllStructCreate($tagSTARTUPINFO)
        Local $PI=DllStructCreate($tagPROCESS_INFORMATION)
        Local $pSI=DllStructGetPtr($SI)                ;获取结构指针
        Local $pPI=DllStructGetPtr($PI)

        Local Const $STARTF_USESHOWWINDOW=1
        DllStructSetData($SI,"Size",DllStructGetSize($SI))                ;必须步骤,初始化结构的大小
        DllStructSetData($SI,"ShowWindow",$ShowMode)                ;显示模式
        DllStructSetData($SI,"Flags",$STARTF_USESHOWWINDOW)                ;使用ShowWindow元素

        $tAppName = DllStructCreate("char Text["&1&"]")                ;CreateProcess的第一个参数为空,长度为1个字节
        $pAppName = DllStructGetPtr($tAppName)                ;CreateProcess的第一个参数的指针

        If $sCommand <> "" Then
                $tCommand = DllStructCreate("char Text[" & StringLen($sCommand) + 1 & "]")
                $pCommand = DllStructGetPtr($tCommand)
                DllStructSetData($tCommand, "Text", $sCommand)
        EndIf
        If $sDir <> "" Then
                $tDir = DllStructCreate("char Text[" & StringLen($sDir) + 1 & "]")
                $pDir = DllStructGetPtr($tDir)
                DllStructSetData($tDir, "Text", $sDir)
        EndIf

        $aResult = DllCall("Kernel32.dll", "int", "CreateProcess", "ptr", $pAppName, "ptr", $pCommand, "ptr", "", "ptr", "", _
                        "int", false, "int", 0, "ptr", "", "ptr", $pDir, "ptr", $pSI, "ptr", $pPI)

        If @error Then Return SetError(@error, 0, False)
        Return $aResult[0] <> 0
        
EndFunc
[ 本帖最后由 UID 于 2009-2-3 16:54 编辑 ]
 楼主| 发表于 2009-2-3 17:16:37 | 显示全部楼层
成功解决了
Local Const $tagSTARTUPINFO="int Size;ptr Reserved1;ptr Desktop;ptr Title;int X;int Y;int XSize;int YSize;int XCountChars;" & _
                "int YCountChars;int FillAttribute;int Flags;short ShowWindow;short Reserved2;ptr Reserved3;int StdInput;" & _
                "int StdOutput;int StdError"
Local Const $tagPROCESS_INFORMATION="hwnd hProcess;hwnd hThread;int ProcessID;int ThreadID"
Local Const $SW_SHOW=1

Func _UID_CreateProcess($sCommand,$sDir,$ShowMode=$SW_SHOW)
        ;参数1:命令行;参数2:运行路径;参数3:显示模式,默认是显示
        Local $pCommand, $tCommand, $pDir, $tDir, $aResult
        Local $SI=DllStructCreate($tagSTARTUPINFO)
        Local $PI=DllStructCreate($tagPROCESS_INFORMATION)
        Local $pSI=DllStructGetPtr($SI)                ;获取结构指针
        Local $pPI=DllStructGetPtr($PI)

        Local Const $STARTF_USESHOWWINDOW=1
        DllStructSetData($SI,"Size",DllStructGetSize($SI))                ;必须步骤,初始化结构的大小
        DllStructSetData($SI,"ShowWindow",$ShowMode)                ;显示模式
        DllStructSetData($SI,"Flags",$STARTF_USESHOWWINDOW)                ;使用ShowWindow元素

        If $sCommand <> "" Then
                $tCommand = DllStructCreate("char Text[" & StringLen($sCommand) + 1 & "]")
                $pCommand = DllStructGetPtr($tCommand)
                DllStructSetData($tCommand, "Text", $sCommand)
        EndIf
        If $sDir <> "" Then
                $tDir = DllStructCreate("char Text[" & StringLen($sDir) + 1 & "]")
                $pDir = DllStructGetPtr($tDir)
                DllStructSetData($tDir, "Text", $sDir)
        EndIf

;        $aResult = DllCall("Kernel32.dll", "int", "CreateProcess", "ptr", $pAppName, "ptr", $pCommand, "ptr", 0, "ptr", 0, _
;                        "int", false, "int", 0, "ptr", 0, "ptr", $pDir, "ptr", $pSI, "ptr", $pPI)
        $aResult = DllCall("Kernel32.dll", "int", "CreateProcess", "ptr", 0, "ptr", $pCommand, "ptr", 0, "ptr", 0, _
                        "int", false, "int", 0, "ptr", 0, "ptr", $pDir, "ptr", $pSI, "ptr", $pPI)

        If @error Then Return SetError(@error, 0, False)
        Return $aResult[0] <> 0
        
EndFunc
发表于 2009-2-3 23:18:57 | 显示全部楼层
向您学习~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-23 07:27 , Processed in 0.073392 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表