本帖最后由 xunis 于 2012-12-26 18:35:21 编辑

解决方案 »

  1.   


    没太理解是什么意思,我说的是ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&)代码的返回值是0,根据Err.LastDllError=6的结果查询出来的错误代码是 ERROR_INVALID_HANDLE 应该说的是hReadPipe
      

  2.   

    查看API文档CreateProcess的最后一个参数的解释是“PROCESS_INFORMATION,该结构用于容纳新进程的进程和线程标识符。大多数情况下,一旦这个函数返回,父应用程序都会关闭两个句柄。”,其中有关闭句柄的字眼,会不会根这个有关系?还有纠正下笔误:
    CreateProcess(sCmdline, "", sa, sa, True, 0, 0, vbNullString, start, proc)
    应该是ret = CreateProcess(sCmdline, "", sa, sa, True, 0, 0, vbNullString, start, proc)
    写错了,该处的返回值是非0
      

  3.   

    是哪一行出错?创建进程应该这样写:
    Dim sa As SECURITY_ATTRIBUTES, si As STARTUPINFO, pi As PROCESS_INFORMATION
    sa.nLength = Len(sa)
    sa.bInheritHandle = True
    si.cb = Len(si)
    Res = CreateProcess(ApplicationName, CommandLine, sa, sa, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
    If Res = 0 Then
       information = MsgBox("不能创建子进程!", vbOKOnly + vbExclamation + vbSystemModal, "创建进程提示")
    End If
    '''''''''''''''''''''''
    CloseHandle pi.hProcess
    CloseHandle pi.hThread
      

  4.   

    CloseHandle hWritePipe 语句放错位置了,还没开始传输就把管道关闭了,什么逻辑?
    移到 CloseHandle proc.hThread 语句后面。
      

  5.   


    你好,我刚试了下放到后面也是一样的。
    我想了想,这个写是管道的写,写的过程是不是在创建进程的时候已经完成了呢?但是我发现个问题,就是当创建进程已经结束后,甚至程序都走完了,dos窗口才弹出来。
      

  6.   


    你好,按照您的方法该了下,也是一样的
    出错的地方在         MsgBox Err.LastDllError   '结果=0
             MsgBox GetLastError       '结果=0
            ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&)
             MsgBox Err.LastDllError   '结果=6
    我查API帮助文档看到6代表的意思是 ERROR_INVALID_HANDLE   应该说的是hReadPipe
      

  7.   


    是 ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&) 有问题~ 但是我debug看到hReadPipe是有值的
      

  8.   

    这是 API-Guide 中的例子,可以正确获得 ping 的命令行输出。
    调用你的外部 exe 试试。
    'Redirects output from console program to textbox.
    'Requires two textboxes and one command button.
    'Set MultiLine property of Text2 to true.
    '
    'Original bcx version of this program was made by
    ' dl <[email protected]>
    'VB port was made by Jernej Simoncic <[email protected]>
    'Visit Jernejs site at http://www2.arnes.si/~sopjsimo/
    '
    'Note: don't run plain DOS programs with this example
    'under Windows 95,98 and ME, as the program freezes when
    'execution of program is finnished.Option Explicit
    Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Type SECURITY_ATTRIBUTES
      nLength As Long
      lpSecurityDescriptor As Long
      bInheritHandle As Long
    End TypePrivate Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessId As Long
      dwThreadId As Long
    End TypePrivate Type STARTUPINFO
      cb As Long
      lpReserved As Long
      lpDesktop As Long
      lpTitle As Long
      dwX As Long
      dwY As Long
      dwXSize As Long
      dwYSize As Long
      dwXCountChars As Long
      dwYCountChars As Long
      dwFillAttribute As Long
      dwFlags As Long
      wShowWindow As Integer
      cbReserved2 As Integer
      lpReserved2 As Byte
      hStdInput As Long
      hStdOutput As Long
      hStdError As Long
    End TypePrivate Type OVERLAPPED
        ternal As Long
        ternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
    End TypePrivate Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const SW_HIDE = 0
    Private Const EM_SETSEL = &HB1
    Private Const EM_REPLACESEL = &HC2Private Sub Command1_Click()
      Command1.Enabled = False
      Redirect Text1.Text, Text2
      Command1.Enabled = True
    End Sub
    Private Sub Form_Load()
        Text1.Text = "ping"
    End Sub
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
      If Command1.Enabled = False Then Cancel = True
    End SubSub Redirect(cmdLine As String, objTarget As Object)
      Dim i%, t$
      Dim pa As SECURITY_ATTRIBUTES
      Dim pra As SECURITY_ATTRIBUTES
      Dim tra As SECURITY_ATTRIBUTES
      Dim pi As PROCESS_INFORMATION
      Dim sui As STARTUPINFO
      Dim hRead As Long
      Dim hWrite As Long
      Dim bRead As Long
      Dim lpBuffer(1024) As Byte
      pa.nLength = Len(pa)
      pa.lpSecurityDescriptor = 0
      pa.bInheritHandle = True
      
      pra.nLength = Len(pra)
      tra.nLength = Len(tra)  If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
        sui.cb = Len(sui)
        GetStartupInfo sui
        sui.hStdOutput = hWrite
        sui.hStdError = hWrite
        sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
        sui.wShowWindow = SW_HIDE
        If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
          SetWindowText objTarget.hwnd, ""
          Do
            Erase lpBuffer()
            If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
              SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
              SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
              DoEvents
            Else
              CloseHandle pi.hThread
              CloseHandle pi.hProcess
              Exit Do
            End If
            CloseHandle hWrite
          Loop
          CloseHandle hRead
        End If
      End If
    End Sub
      

  9.   


    好的 我先试试例子我该大概试了下 createprocess 返回值是0 抓取到错误代码 87 对应错误信息ERROR_INVALID_PARAMETER 我先找找原因 谢谢您啊先
      

  10.   

    你是想抓取控制台信息,参考下面这个5楼、6楼的代码:
    http://bbs.csdn.net/topics/210031042
      

  11.   

     额,,,您的这段代码可以调用了!!成功了!!! 哇塞~~~~~~~~~~~~~~~~~~~麻烦您能不能告诉我这种代码段的例子是从哪里找呢?我现在参考的是http://www.vbgood.com/api.html
    但是没有代码段,谢谢谢谢了。麻烦您回复下http://bbs.csdn.net/topics/390326659这个帖子把分给你,同时也谢谢chenjl1031,谢谢谢谢谢谢!高兴啊~~~~~~~~~
      

  12.   

    http://allapi.mentalis.org/agnet/appdown.shtml
    这里的 API-Guide 和 ApiViewer 是必备工具。