UID 发表于 2009-2-3 11:04:32

求如何用CreateProcess打开指定程序

因为AU3自己带的SHELLEXECUTE有局限性,要写一个程序来批量运行程序,有的程序要隐藏窗口运行,有的程序要等待它结束后再执行下一个,所以如果要写,就要写四个语句,两个是SHELLEXECUTE,两个是SHELLEXECUTEWAIT,非常的麻烦,用WINAPI的SHELLEXECUTE的返回值也不能用在WaitForSingleObject,所以要改其它方法,谁会的写一下谢谢

UID 发表于 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
       
EndFunc

[ 本帖最后由 UID 于 2009-2-3 16:54 编辑 ]

UID 发表于 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
       
EndFunc

顽固不化 发表于 2009-2-3 23:18:57

向您学习~~
页: [1]
查看完整版本: 求如何用CreateProcess打开指定程序