`

vb cls_PipeAccess.cls

    博客分类:
  • vb
阅读更多
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 Function
CreateProcess _
Lib "kernel32" _
Alias "CreateProcessA" (ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes
As SECURITY_ATTRIBUTES, _
lpThreadAttributes
As SECURITY_ATTRIBUTES, _
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
TerminateProcess _
Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long

Private Declare Function
ReadFile _
Lib "kernel32" (ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead
As Long, _
lpOverlapped
As Any) As Long

Private Declare Function
WriteFile _
Lib "kernel32" (ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten
As Long, _
lpOverlapped
As Any) As Long

Private Declare Function
GetFileSize _
Lib "kernel32" (ByVal hFile As Long, _
lpFileSizeHigh
As Long) As Long

Private Declare Function
CloseHandle _
Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function
GetLastError _
Lib "kernel32" () As Long

Private Declare Sub
CopyMemory _
Lib "kernel32" _
Alias "RtlMoveMemory" (ByVal Destination As String, _
ByVal Source As String, _
ByVal Length As Long)

Private Declare Function lstrLen _
Lib "kernel32" _
Alias "lstrlenA" (ByVal lpString As String) As Long

Private
Type SECURITY_ATTRIBUTES
nLength
As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End
Type

Private Type PROCESS_INFORMATION
hProcess
As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End
Type

Private Type STARTUPINFO
cb
As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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

Private Const STARTF_USESTDHANDLES = &H100

Private Const STARTF_USESHOWWINDOW = &H1

Private Const NORMAL_PRIORITY_CLASS = &H20
Dim hReadPipe As Long
Dim
hWritePipe As Long
Dim
hReadFile As Long
Dim
hWriteFile As Long
Dim
pi As PROCESS_INFORMATION

Private Const Pipe_Max_Length As Long = 65536 '64K的空间

Public Function CreateProcessWithPipe(Optional ByVal FileName As String = "cmd.exe") As Boolean
On Error GoTo
ErrHdl
Dim ret&
Dim sa As SECURITY_ATTRIBUTES

With sa
.nLength = Len(sa)
'.bInheritHandle = False
.bInheritHandle = True
.lpSecurityDescriptor = 0
End With

'create two pipe->one for input & output and another for err handle
ret = CreatePipe(hReadPipe, hWriteFile, sa, Pipe_Max_Length): If ret = 0 Then Call RaiseErr
ret = CreatePipe(hReadFile, hWritePipe, sa, Pipe_Max_Length):
If ret = 0 Then Call RaiseErr
'since now , we had create two pipes.
Dim si As STARTUPINFO

'fill start info
With si
.cb = Len(si)
.hStdInput = hReadPipe
.hStdOutput = hWritePipe
.hStdError = hWritePipe
'in fact. both error msg and normal msg r msg, so we can let then in a same handle
.wShowWindow = 0 'hide it
.dwFlags = STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW 'use handles, to make our hstd*** avable. use showwindow, to make our wShowWindow setting avable
End With

'createprocess----normally,it should be cmd.
ret = CreateProcess(vbNullString, FileName, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, App.Path, si, pi): If ret = 0 Then Call RaiseErr
CreateProcessWithPipe =
True
Exit Function
ErrHdl:
Call TerminateProcessAndClosePipe
CreateProcessWithPipe =
False
End Function

Public Function
GetStringFromPipe() As String
On Error GoTo
ErrHdl
Dim ret&
Dim sBuffer As String
Dim
lRead As Long
Dim
sReturn As String
sBuffer = Space$(Pipe_Max_Length)
ret = ReadFile(hReadFile, sBuffer, Len(sBuffer), lRead,
ByVal 0&) 'lRead is bytes that had read actully
sReturn = Space$(lRead)
CopyMemory sReturn, sBuffer, lRead
GetStringFromPipe = sReturn
Exit Function
ErrHdl:
GetStringFromPipe =
""
End Function

Public Function
PipeIsNull() As Boolean
PipeIsNull = (GetFileSize(hReadFile, 0&) <= 0)
End Function

Public Function
PutStringToPipe(ByVal StrToPut As String) As Boolean
On Error GoTo
ErrHdl
'most of time, u need to append a vbCrLf after the string u want to put.
Dim ret&
Dim lWrittenBytes As Long
ret = WriteFile(hWriteFile, StrToPut, lstrLen(StrToPut), lWrittenBytes, ByVal 0&): If ret = 0 Then Call RaiseErr
PutStringToPipe = (lWrittenBytes = Len(StrToPut))
Debug.Print hWriteFile
Exit Function
ErrHdl:
PutStringToPipe =
False
End Function

Public Function
TerminateProcessAndClosePipe() As Boolean
On Error GoTo
ErrHdl
Dim ret&
ret = TerminateProcess(pi.hProcess,
0): If ret = 0 Then Call RaiseErr
ret = CloseHandle(hReadPipe):
If ret = 0 Then Call RaiseErr
ret = CloseHandle(hReadFile):
If ret = 0 Then Call RaiseErr
ret = CloseHandle(hWritePipe):
If ret = 0 Then Call RaiseErr
ret = CloseHandle(hWriteFile):
If ret = 0 Then Call RaiseErr
TerminateProcessAndClosePipe =
True
Exit Function
ErrHdl:
TerminateProcessAndClosePipe =
False
End Function

Private Sub
RaiseErr()
On Error Resume Next
Err.Raise vbObjectError + 1 'raise an error so that to be caught by errhdl
End Sub

 

分享到:
评论

相关推荐

    VB_ICO.zip_vb ico图标下载_vb 图标_vbico_vb用ico_www.vbizcon

    vb 常用icon图标,个性化自己的程序必备

    sc_vb6_ent_cd1.zip 2碟装,这是光盘1,包含了桌面开发软件和工具

    【标题】"VB6企业版光盘1:桌面开发的基石" Visual Basic 6(VB6)是微软在1998年推出的一款强大的可视化编程环境,尤其适用于桌面应用程序的开发。"sc_vb6_ent_cd1.zip" 是VB6企业版的压缩包文件,其中包含的是第...

    VB.rar_ACRptEngineX.o_VB 表格_acrptenginex.ocx_ocx vb_vb 报表

    标题中的“VB.rar_ACRptEngineX.o_VB 表格_acrptenginex.ocx_ocx vb_vb 报表”表明这是一个与Visual Basic(VB)编程相关的压缩包,其中包含一个名为ACRptEngineX.ocx的ActiveX控件,用于处理报表展示。ActiveX控件...

    ezVidCap.ocx.rar_EZVIDCAP. O_VB ocx_VB 摄像头_ezVidCap.o_vb ocx

    标签中的"ezvidcap._o vb_ocx vb_摄像头 ezvidcap.o vb__ocx"进一步强调了这是与VB开发摄像头应用有关的OCX控件,ezvidcap可能是控件的类名或库名。 压缩包内的文件列表: 1. `register.bat`:注册批处理文件,...

    vbpcap_sniff.rar_ vbpc_VBPCAP_Winpcap sniff vb_vb WinPcap_vb wi

    标题中的“vbpcap_sniff.rar_ vbpc_VBPCAP_Winpcap sniff vb_vb WinPcap_vb wi”表明这是一个与VB(Visual Basic)编程语言相关的项目,使用了WinPcap库来实现网络数据包捕获,即网络嗅探功能。WinPcap是一个在...

    MATLAB.dll_test.rar_VB call matlab_mwcomutil.dll _vb MWComutil_v

    在IT行业中,VB(Visual Basic)与MATLAB的联合编程是一种常见的技术,它允许开发者利用MATLAB的强大计算能力,同时利用VB的用户界面设计和系统集成能力。本示例是关于如何通过VB调用MATLAB的COM接口进行交互的一个...

    VB6_CHM_simple.zip_vb6

    【标题】"VB6_CHM_simple.zip_vb6" 是一个与Visual Basic 6相关的压缩文件,其中包含了一些关于Visual Basic 6编程的CHM( Compiled Help Manual)格式的文档。CHM文件是微软开发的帮助文件系统,它将HTML文档集合在...

    vb.netQQ.rar_ vb.netQQ_QQ Vb.NET_VB .net_VB.NET ICQ_vb.net

    【标题】"vb.netQQ.rar_ vb.netQQ_QQ Vb.NET_VB .net_VB.NET ICQ_vb.net" 涉及的核心知识点主要集中在使用VB.NET编程语言开发一个QQ客户端的源代码程序。VB.NET是Visual Basic .NET的简称,是微软公司推出的一种面向...

    wxh vb_project.zip

    【标题】"wxh vb_project.zip" 暗示我们关注的是一个VB(Visual Basic)项目,这个项目可能是一个软件应用程序或系统,被压缩在名为"vb_project.zip"的文件中。VB是一种由Microsoft开发的事件驱动编程语言,主要用于...

    sedwen_cwqe_VB源码_源码.zip

    【标题】"sedwen_cwqe_VB源码_源码.zip" 提供的是一份VB(Visual Basic)编程语言的源代码集合。VB是一种基于Windows环境的事件驱动编程工具,由微软公司开发,它以其易学易用、强大的图形用户界面设计功能而闻名。 ...

    VB_window.rar_vb window.restore_vb 界面_vb界面

    "VB_window.rar_vb window.restore_vb 界面_vb界面"这个标题暗示了我们将在VB环境中讨论如何处理窗口状态,特别是窗口的恢复操作以及创建和设计用户界面。 1. **VB窗口状态管理**:在VB中,窗口有多种状态,包括...

    VB_Test.rar_VB Winpp_VB_test_barcode vb_vb 条码_条码打印机

    标题中的"VB_Test.rar_VB Winpp_VB_test_barcode vb_vb 条码_条码打印机"揭示了这个压缩包文件与Visual Basic(VB)编程语言相关,特别是关于条码打印的应用。VB Winpp可能是指一个特定的VB库或者模块,用于Windows...

    VB6-SirkMini_20190220.exe

    VB6-Sirk Mini 安装版 最新版 包含插件 增强版 包含 Addins

    VB_xml_Class.rar_vb xml_vb xml class_xml

    1. **XMLDOMDocument.cls** - 这可能是一个名为XMLDOMDocument的类文件,它是VB类库的核心,用于封装MSXML中的DOM(文档对象模型)接口,允许开发者通过对象方法和属性来操作XML文档。 2. **工程1.exe** - 这是编译...

    vb_js_example.rar_VB CallByName JS_VB控制_[JS_example.js]_vb WebBr

    VB代码与WebBrowser控制中的HTML代码,互相控制的例子。 点VB中的按钮,改变网页的显示样式...点网页中的按钮,修改VB中控件的属性,或调用VB中的函数。这只是一个如何实现的示例,没有什么强大功能,代码有中文说明。

    S7_200_PPI.dll

    用VB调用S7_200_PPI这个动态链接库与PLC200通讯

    baidufanyi.rar_VB网页_vb 百度翻译_www. baidufanyi.com_百度翻译_翻译

    【标签】中的"vb网页 vb_百度翻译 www._baidufanyi.com 百度翻译 翻译" 是对该程序特性的关键词标注,其中: - "vb网页" 表明是使用VB(Visual Basic)开发的Web相关应用。 - "vb_百度翻译" 指出这个VB程序与百度...

    VB_XML.rar_ VB_XML_VB speeaksdk xml_vb xml_xml_xml vb

    标题中的"VB_XML.rar_ VB_XML_VB speeaksdk xml_vb xml_xml_xml vb"表明这个压缩包包含了与Visual Basic(VB)编程语言和XML处理相关的代码示例。XML,全称Extensible Markup Language,是一种用于存储和传输数据的...

    IISBACK.VB_,IISEXT.VB_,IISAPP.VB_,IISCFG.DL_,IISCLEX4.DL_,IISCNFG.VB_,IISDG.CH_,IISEXT.DL_

    1. **IISBACK.VB_**:这个文件名暗示可能是一个备份相关的VB(Visual Basic)脚本。它可能用于备份IIS的配置或网站数据,确保在系统故障或更新后能够恢复。 2. **IISEXT.VB_**:这可能涉及到IIS的扩展功能,可能是...

    VB_automatically_fill_form_submit_single.rar_.submit vb_Single a

    标题中的"VB_automatically_fill_form_submit_single.rar_.submit vb_Single a"表明这是一个关于使用Visual Basic(VB)编程语言实现自动填充表单并提交的示例代码。在这个实例中,我们将探讨如何利用VB来自动化网页...

Global site tag (gtag.js) - Google Analytics