怎么样才能把一个DOS程序窗口化呢?
要用到什么API?

解决方案 »

  1.   

    Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
    Private Declare Function ReadFileA Lib "kernel32" Alias "ReadFile" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
    Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle 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 Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End TypePrivate Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessID As Long
        dwThreadID As Long
    End TypePublic Function ExecuteCommand(CommandLine As String) As String
    '管道方式执行DOS命令并返回执行结果
        Dim proc As PROCESS_INFORMATION
        Dim Ret As Long
        Dim start As STARTUPINFO
        Dim sa As SECURITY_ATTRIBUTES
        Dim hReadPipe As Long, hWritePipe As Long, lngBytesread As Long
        Dim strBuff(32767) As Byte, mOutputs As String
        
        sa.nLength = Len(sa)
        sa.bInheritHandle = 1&
        sa.lpSecurityDescriptor = 0&
        Ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
        
        If Ret = 0 Then ExecuteCommand = "CreatePipe failed. error: " & Err.LastDllError: Exit Function
        
        start.cb = Len(start)
        start.dwFlags = &H100 Or 1
        start.hStdOutput = hWritePipe
        start.hStdError = hWritePipe
        Ret& = CreateProcessA(0&, CommandLine, sa, sa, 1, &H20, 0, 0, start, proc)
        If Ret <> 1 Then ExecuteCommand = "error: '" & CommandLine & "' 不是可运行的程序": Exit Function
        Ret = CloseHandle(hWritePipe)
        
        Do
            Ret = ReadFile(hReadPipe, VarPtr(strBuff(0)), 32767, lngBytesread, 0&)
            If Ret = 0 Then Exit Do
            ReDim strBuff2(lngBytesread - 1) As Byte
            CopyMemory VarPtr(strBuff2(0)), VarPtr(strBuff(0)), lngBytesread
            mOutputs = mOutputs & StrConv(strBuff2, vbUnicode)
        Loop
        
        Ret = CloseHandle(proc.hProcess)
        Ret = CloseHandle(proc.hThread)
        Ret = CloseHandle(hReadPipe)
        
        ExecuteCommand = mOutputs
    End Function