をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
#contents
Private Declare Function CreateProcess Lib "kernel32" Al...
(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 lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ...
Private Declare Function ReadFile Lib "kernel32" (ByVal ...
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long,...
lpOverlapped As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel...
(ByVal hHandle As Long, ByVal dwMilliseconds As Long...
Private Declare Function CreatePipe Lib "kernel32" (phRe...
phWritePipe As Long, lpPipeAttributes As SECURITY_AT...
ByVal nSize As Long) As Long
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 Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&
Private Const INFINITE As Long = &HFFFF&
Public Function RunCommand(CommandLine As String) As Str...
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim retval As Long
Dim hRead As Long
Dim hWrite As Long
Dim sBuffer(0 To 63) As Byte
Dim lgSize As Long
Dim sa As SECURITY_ATTRIBUTES
Dim strResult As String
With sa
.nLength = Len(sa)
.bInheritHandle = 1& 'inherit, needed for this t...
.lpSecurityDescriptor = 0&
End With
retval = CreatePipe(hRead, hWrite, sa, 0&)
If retval = 0 Then
Debug.Print "CreatePipe Failed"
RunCommand = ""
Exit Function
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHO...
.wShowWindow = SW_HIDE
.hStdOutput = hWrite 'pass the write end of the ...
End With
retval = CreateProcess(vbNullString, _
CommandLine & vbNullChar, _
sa, _
sa, _
1&, _
NORMAL_PRIORITY_CLASS, _
ByVal 0&, _
vbNullString, _
si, _
pi)
If retval Then
WaitForSingleObject pi.hProcess, INFINITE
Do While ReadFile(hRead, sBuffer(0), 64, lgSize,...
strResult = strResult & StrConv(sBuffer(), v...
Erase sBuffer()
If lgSize <> 64 Then Exit Do
Loop
CloseHandle pi.hProcess
CloseHandle pi.hThread
Else
Debug.Print "CreateProcess Failed" & vbCrLf
End If
CloseHandle hRead
CloseHandle hWrite
RunCommand = Replace(strResult, vbNullChar, "")
End Function
Function test()
Debug.Print RunCommand("ipconfig")
End Function
~
~
終了行:
#contents
Private Declare Function CreateProcess Lib "kernel32" Al...
(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 lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ...
Private Declare Function ReadFile Lib "kernel32" (ByVal ...
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long,...
lpOverlapped As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel...
(ByVal hHandle As Long, ByVal dwMilliseconds As Long...
Private Declare Function CreatePipe Lib "kernel32" (phRe...
phWritePipe As Long, lpPipeAttributes As SECURITY_AT...
ByVal nSize As Long) As Long
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 Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&
Private Const INFINITE As Long = &HFFFF&
Public Function RunCommand(CommandLine As String) As Str...
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim retval As Long
Dim hRead As Long
Dim hWrite As Long
Dim sBuffer(0 To 63) As Byte
Dim lgSize As Long
Dim sa As SECURITY_ATTRIBUTES
Dim strResult As String
With sa
.nLength = Len(sa)
.bInheritHandle = 1& 'inherit, needed for this t...
.lpSecurityDescriptor = 0&
End With
retval = CreatePipe(hRead, hWrite, sa, 0&)
If retval = 0 Then
Debug.Print "CreatePipe Failed"
RunCommand = ""
Exit Function
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHO...
.wShowWindow = SW_HIDE
.hStdOutput = hWrite 'pass the write end of the ...
End With
retval = CreateProcess(vbNullString, _
CommandLine & vbNullChar, _
sa, _
sa, _
1&, _
NORMAL_PRIORITY_CLASS, _
ByVal 0&, _
vbNullString, _
si, _
pi)
If retval Then
WaitForSingleObject pi.hProcess, INFINITE
Do While ReadFile(hRead, sBuffer(0), 64, lgSize,...
strResult = strResult & StrConv(sBuffer(), v...
Erase sBuffer()
If lgSize <> 64 Then Exit Do
Loop
CloseHandle pi.hProcess
CloseHandle pi.hThread
Else
Debug.Print "CreateProcess Failed" & vbCrLf
End If
CloseHandle hRead
CloseHandle hWrite
RunCommand = Replace(strResult, vbNullChar, "")
End Function
Function test()
Debug.Print RunCommand("ipconfig")
End Function
~
~
ページ名: