Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Slanje komandi shell-u

[es] :: Visual Basic 6 :: Slanje komandi shell-u

[ Pregleda: 2812 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

dava
Banja Luka

Član broj: 27208
Poruke: 893



+384 Profil

icon Slanje komandi shell-u07.04.2008. u 13:39 - pre 195 meseci
Imam dva pitanja vezano za shell.
Kod slanja neke komande shell-u, recimo:

Code:

   strKomanda = "netstat -a"
   Shell "cmd /k " & strKomanda


1. kako da mi code saceka dok se ta komanda zavrsi?

2. kako da se rezultat te komande direktno vrati u moj vb code.
Kazem direktno jer sam uspio ovo da dobijem tako sto sam dodao jos redirekciju u txt fajl pa sam iz njega citao rezultat komande.

Code:

   Shell "cmd /k " & strKomanda  & " > d:\1.txt"


SELECT * FROM หน่วยงานหลัก WHERE ยสันติ LIKE 'โดย%'
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Slanje komandi shell-u07.04.2008. u 14:31 - pre 195 meseci
pogledaj API Guide tamo imas primere za obe stvari koje tebe muce
 
Odgovor na temu

dava
Banja Luka

Član broj: 27208
Poruke: 893



+384 Profil

icon Re: Slanje komandi shell-u07.04.2008. u 14:46 - pre 195 meseci
Moze li mi reci koji API.
SELECT * FROM หน่วยงานหลัก WHERE ยสันติ LIKE 'โดย%'
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Slanje komandi shell-u07.04.2008. u 16:51 - pre 195 meseci
pogledaj WaitForSingleObject za cekanje da se program zavrsi, a da preusmeris text sa konzole u string promenljivu koristi CreatePipe
 
Odgovor na temu

dava
Banja Luka

Član broj: 27208
Poruke: 893



+384 Profil

icon Re: Slanje komandi shell-u08.04.2008. u 07:42 - pre 195 meseci
Hvala na pomoci. Evo da to podjelim sa vama ako nekome zatreba.

U modul:
Code:

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 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 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 Type

Private Type OVERLAPPED
    ternal As Long
    ternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2

Public Sub 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


Poziv iz forme:

Code:

Redirect Text1.Text, Text2


text2 podesiti da je Multiline i ScrollBars vertikal.

WaitForSingleObject jos nisam proucio, ali mi i ne treba jer pomocu CreatePipe u stvari mogu cekati povratnu informaciju i na taj nacin cekati kraj komande.
SELECT * FROM หน่วยงานหลัก WHERE ยสันติ LIKE 'โดย%'
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Slanje komandi shell-u08.04.2008. u 15:11 - pre 195 meseci
sto se tice WaitForSingleObject evo primera na koji sam naleteo na netu: http://www.vb-helper.com/howto_shell_wait.html
 
Odgovor na temu

dava
Banja Luka

Član broj: 27208
Poruke: 893



+384 Profil

icon Re: Slanje komandi shell-u10.04.2008. u 08:47 - pre 195 meseci
Nisam uspio sa tim kodom jer on ceka da se ugasi proces, u ovom slucaju cmd.exe, i tek tada nastavlja dalje. E sad, recimo kad mu saljemo komandu "cmd /k tree c:\ /a > c:\1.txt", cmd.exe proces ostaje otvoren. Iz nekog razloga ne benda multiline komandu: "cmd /k tree c:\ /a > c:\1.txt&exit" koja bi trebala da zatvori prozor.

Ali uspio sam sa ovim kodom:

Code:

Option Explicit

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400

Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
    
    Dim lInst As Long
    Dim lStart As Long
    Dim lTimeToQuit As Long
    Dim sExeName As String
    Dim lProcessId As Long
    Dim lExitCode As Long
    Dim bPastMidnight As Boolean
    
    On Error GoTo ErrorHandler

    lStart = CLng(Timer)
    sExeName = ExeFullPath

    'Deal with timeout being reset at Midnight
    If TimeOutValue > 0 Then
        If lStart + TimeOutValue < 86400 Then
            lTimeToQuit = lStart + TimeOutValue
        Else
            lTimeToQuit = (lStart - 86400) + TimeOutValue
            bPastMidnight = True
        End If
    End If

    lInst = Shell(ExeFullPath, vbMinimizedNoFocus)
    
lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)

    Do
        Call GetExitCodeProcess(lProcessId, lExitCode)
        DoEvents
        If TimeOutValue And Timer > lTimeToQuit Then
            If bPastMidnight Then
                 If Timer < lStart Then Exit Do
            Else
                 Exit Do
            End If
    End If
    Loop While lExitCode = STATUS_PENDING
    
    ShellandWait = True
   
ErrorHandler:
ShellandWait = False
Exit Function
End Function

Private Sub Form_Load()
ShellandWait ("cmd /k tree c:\ /a > c:\1.txt&exit"), 0
End Sub




SELECT * FROM หน่วยงานหลัก WHERE ยสันติ LIKE 'โดย%'
 
Odgovor na temu

[es] :: Visual Basic 6 :: Slanje komandi shell-u

[ Pregleda: 2812 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.