首页 Soft PlugIn RAN乱 Dev开发 Info资料 English WAP 留言 登陆 注册
-
Posted by Yippee | 评论(0) | 引用(0) | 阅读1545次
VB写的管道重定向CVS4IIS里面的代码

Option Explicit
'Capture the outputs of a DOS command
'Author: Marco Pipino
'marcopipino at libero dot it
'28/02/2002
'Modified: Jason Stracner
'19/08/2003

'The CreatePipe function creates an anonymous pipe,
'and returns handles to the read and write ends of the pipe.
Private Declare Function CreatePipe Lib "kernel32" ( _
          phReadPipe As Long, _
          phWritePipe As Long, _
          lpPipeAttributes As Any, _
          ByVal nSize As Long) As Long

'Used to read the the pipe filled by the process create
'with the CretaProcessA function
Private Declare Function ReadFile Lib "kernel32" ( _
          ByVal hFile As Long, _
          ByVal lpBuffer As String, _
          ByVal nNumberOfBytesToRead As Long, _
          lpNumberOfBytesRead As Long, _
          ByVal lpOverlapped As Any) As Long

'Structure used by the CreateProcessA function
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

'Structure used by the CreateProcessA function
Private 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 Type

'Structure used by the CreateProcessA function
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

'This function launch the the commend and return the relative process
'into the PRECESS_INFORMATION structure
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

'Close opened handle
Private Declare Function CloseHandle Lib "kernel32" ( _
          ByVal hHandle As Long) As Long

'Consts for the above functions
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1


Private m_sCommand As String          'Private variable for the CommandLine property
Private m_sOutputs As String          'Private variable for the ReadOnly Outputs property

'Event that notify the temporary buffer to the object
Public Event ReceiveOutputs(CommandOutputs As String)

'This property set and get the DOS command line
'It's possible to set this property directly from the
'parameter of the ExecuteCommand method
Public Property Let CommandLine(DOSCommand As String)
    m_sCommand = DOSCommand
End Property

Public Property Get CommandLine() As String
    CommandLine = m_sCommand
End Property

'This property ReadOnly get the complete output after
'a command execution
Public Property Get Outputs()
    Outputs = m_sOutputs
End Property

Public Function ExecuteCommand(Optional CommandLine As String) As String
    Dim typeProcess_Information As PROCESS_INFORMATION    'Process info filled by CreateProcessA
    Dim iReturn As Long                                   'long variable for get the return value of the
                                                          'API functions
    Dim typeStartupInformation As STARTUPINFO             'StartUp Info passed to the CreateProceeeA
                                                          'function
    Dim typeSecurityAttributes As SECURITY_ATTRIBUTES     'Security Attributes passeed to the
                                                          'CreateProcessA function
    Dim iReadPipeHandle As Long                           'Read Pipe handle created by CreatePipe
    Dim iWritePipeHandle As Long                          'Write Pite handle created by CreatePipe
    Dim iBytesReadFromReadPipe As Long                    'Amount of byte read from the Read Pipe handle
    Dim sReadPipeBuffer As String * 256                   'String buffer reading the Pipe

    'if the parameter is not empty update the CommandLine property
    Debug.Print CommandLine
    If Len(CommandLine) > 0 Then
        m_sCommand = CommandLine
    End If
   
    'if the command line is empty then exit whit a error message
    If Len(m_sCommand) = 0 Then
        m_sOutputs = "Command Line empty (1)"
        ExecuteCommand = m_sOutputs
        Exit Function
    End If
   
    'Create the Pipe
    typeSecurityAttributes.nLength = Len(typeSecurityAttributes)
    typeSecurityAttributes.bInheritHandle = 1&
    typeSecurityAttributes.lpSecurityDescriptor = 0&
    iReturn = CreatePipe(iReadPipeHandle, _
              iWritePipeHandle, _
              typeSecurityAttributes, _
              0)
   
    If iReturn = 0 Then
        'If an error occur during the Pipe creation exit
        m_sOutputs = "CreatePipe failed. Error: " & Err.LastDllError & " (2)"
        ExecuteCommand = m_sOutputs
        Exit Function
    End If
   
    'Launch the command line application
    typeStartupInformation.cb = Len(typeStartupInformation)
    typeStartupInformation.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    'set the StdOutput and the StdError output to the same Write Pipe handle
    typeStartupInformation.hStdOutput = iWritePipeHandle
    typeStartupInformation.hStdError = iWritePipeHandle
    'Execute the command
    iReturn& = CreateProcessA(0&, _
              m_sCommand, _
              typeSecurityAttributes, _
              typeSecurityAttributes, _
              1&, _
              NORMAL_PRIORITY_CLASS, _
              0&, _
              0&, _
              typeStartupInformation, _
              typeProcess_Information)
       
    If iReturn <> 1 Then
        'if the command is not found ....
        m_sOutputs = "File or command not found (3)"
        ExecuteCommand = m_sOutputs
        Exit Function
    End If
   
    'Now We can ... must close the iWritePipeHandle
    iReturn = CloseHandle(iWritePipeHandle)
    m_sOutputs = ""
   
    'Read the ReadPipe handle
    Do
        iReturn = ReadFile(iReadPipeHandle, _
                  sReadPipeBuffer, _
                  256, _
                  iBytesReadFromReadPipe, _
                  0&)
        m_sOutputs = m_sOutputs & Left(sReadPipeBuffer, iBytesReadFromReadPipe)
        'Send data to the object via ReceiveOutputs event
        RaiseEvent ReceiveOutputs(Left(sReadPipeBuffer, iBytesReadFromReadPipe))
    Loop While iReturn <> 0
   
    'Close the opened handles
    iReturn = CloseHandle(typeProcess_Information.hProcess)
    iReturn = CloseHandle(typeProcess_Information.hThread)
    iReturn = CloseHandle(iReadPipeHandle)
   
    'Return the Outputs property with the entire DOS output
    ExecuteCommand = m_sOutputs
End Function


字体:

Permanant URI永久地址 http://www.shengfang.org/blog/p/cvs4iisvbpipe.php
Trackback URI引用地址 http://www.shengfang.org/blog/tb.php?tb_id=1120790363

2005年7月8日10:39星期五  [Info资料] 追踪此文的RSS
提示:
此文还没有评论。

称呼:    登陆   注册
   不注册,但记住我的信息
邮件:
(非必须)
评论: [UBB代码帮助]
粗体 斜体 下划线 链接 水平线 引用



验证码: 请输入你看见的数字
关闭UBB      提交时自动将内容复制到剪贴板

公告
Fire and Motion!

统计信息
[Yippee]||[统计]||日志:1858
在线: 11||用户: 2577 [列表]
今日:406||到访:2886470
Rss:897749||评论:1605

最新日志

最新评论

友情链接

日历
2008 - 08
     12
3456789
10111213141516
17181920212223
24252627282930
31      

最新引用

搜索

归档

杂项
Get RSS Feed (Version 2.0)
Get Atom Feed (Version 0.3)
编码:  UTF-8