Results 1 to 6 of 6

Thread: Help! Call VB app from a bat, vb app return string to bat

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Winston Salem, NC
    Posts
    13
    Can someone please help me. I need to make a small vb app that I can call from a batch or cmd file. The vb app would then pop up a form for some user input and return a string to the batch or cmd file in a variable. Is there any easy way to do this? I know how to do everything I need it to do except return the string to the batch file in a variable.

    Any help would be greatly appreciated,
    Thanks,
    Richard

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    I don't know how to get the returning variable back to the batch file.

    However, I know how to make a VB program generate another batch file with the returning variable? In other words, you can run your batch file and it will run the VB program and then the program will write another batch file our and execute it.
    Chemically Formulated As:
    Dr. Nitro

  3. #3
    Member
    Join Date
    Dec 1999
    Posts
    41
    Well, I just tried out something...see if u like this approach...
    1) Write to a Text File , say TESTFILE from VB program.
    2) Assume that it contains text "This is a Test"
    3)Use SETX utility (comes free with Windows NT resource kit)as follows
    SETX var1 -f TESTFILE -a 0,0 .....returns "This" in var1
    SETX var2 -f TESTFILE -a 0,1 .....returns "is" in var2
    SETX var3 -f TESTFILE -a 0,3 .....returns "Test" in var3

    Let me know if it works...
    My other ID is [email protected]

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Also you can try this code. I got it from Megatron. Just drop it into a module.

    Code:
    Option Explicit
    
    'File: Console.bas
    'Summary: Demonstrates how it is possible to create and
    'manipulate a console box in Visual Basic.
    'Classes: None
    'Functions: Main, ConsolePrint, ConsoleRead
    'Origin: Visual Basic 6.0 For Windows
    '--------------------------------------------------------
    '(C) Copyright 1999 by Scott Lloyd. All Rights Reserved.
    'Scott Lloyd
    '===========================================================+
    
    
    Private Declare Function AllocConsole Lib "kernel32" () As Long
    Private Declare Function FreeConsole Lib "kernel32" () As Long
    Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
    Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
    Private Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleOutput As Long, dwMode As Long) As Long
    Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
    Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
    Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
    
    ''''C O N S T A N T S'''''''''''''''''''''''''''''''''''''
    'I/O handlers for the console window. These are much like the
    'hWnd handlers to form windows.
    
    Private Const STD_INPUT_HANDLE = -10&
    Private Const STD_OUTPUT_HANDLE = -11&
    Private Const STD_ERROR_HANDLE = -12&
    
    'Color values for SetConsoleTextAttribute.
    Private Const FOREGROUND_BLUE = &H1
    Private Const FOREGROUND_GREEN = &H2
    Private Const FOREGROUND_RED = &H4
    Private Const FOREGROUND_INTENSITY = &H8
    Private Const BACKGROUND_BLUE = &H10
    Private Const BACKGROUND_GREEN = &H20
    Private Const BACKGROUND_RED = &H40
    Private Const BACKGROUND_INTENSITY = &H80
    
    'For SetConsoleMode (input)
    Private Const ENABLE_LINE_INPUT = &H2
    Private Const ENABLE_ECHO_INPUT = &H4
    Private Const ENABLE_MOUSE_INPUT = &H10
    Private Const ENABLE_PROCESSED_INPUT = &H1
    Private Const ENABLE_WINDOW_INPUT = &H8
    
    'For SetConsoleMode (output)
    Private Const ENABLE_PROCESSED_OUTPUT = &H1
    Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2
    
    '''''G L O B A L S'''''''''''''''''''''''''''''''''''
    Private hConsoleIn As Long 'The console's input handle
    Private hConsoleOut As Long 'The console's output handle
    Private hConsoleErr As Long 'The console's error handle
    
    
    '''''M A I N'''''''''''''''''''''''''''''''''''''''''
    
    Private Sub Main()
        Dim szUserInput As String
        AllocConsole 'Create a console instance
        SetConsoleTitle "VB Console Example" 'Set the title on the console window
        
        'Get the console's handle
        hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
        hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
        hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
        
        'Print the prompt to the user. Use the vbCrLf to get to a new line.
        SetConsoleTextAttribute hConsoleOut, _
        FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_BLUE Or FOREGROUND_INTENSITY Or BACKGROUND_BLUE
        ConsolePrint "VB Console Example" & vbCrLf
        SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_BLUE
        ConsolePrint "Enter your name--> "
        
        'Get the user's name
        szUserInput = ConsoleRead()
        If Not szUserInput = vbNullString Then
          ConsolePrint "Hello, " & szUserInput & "!" & vbCrLf
        Else
          ConsolePrint "Hello, whoever you are!" & vbCrLf
        End If
        
        'End the program
        ConsolePrint "Press enter to exit"
        Call ConsoleRead
        FreeConsole 'Destroy the console
    End Sub
    
    '''''F U N C T I O N S''''''''''''''''''''''''''''''''''
    
    'Function: ConsolePrint
    'Summary: Prints the output of a string
    'Args: String ConsolePrint
    'The string to be printed to the console's ouput buffer.
    'Returns: None
    Private Sub ConsolePrint(szOut As String)
      WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
    End Sub
    
    'Function: ConsoleRead
    'Summary: Gets a line of input from the user.
    'Args: None
    'Returns: String ConsoleRead
    'The line of input from the user.
    Private Function ConsoleRead() As String
      Dim sUserInput As String * 256
      Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
      'Trim off the NULL charactors and the CRLF.
      ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
    End Function
    Chemically Formulated As:
    Dr. Nitro

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Drop these codes into a form!

    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 WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Const INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000
    
    Private Sub Form_Load()
      Me.WindowState = vbMaximized
      Me.Font = "Courier"
      Me.FontBold = False
      ChDir "C:\"
    End Sub
    Private Sub Form_Click()
      'Print f_DOS_Result("Net View")
      Print f_DOS_Result("dir /o:g /o:n")
    End Sub
    
    Function f_DOS_Result(ByVal sCommand As String) As String
    
        On Error Resume Next
        Dim str_BatchFile As String
        Dim str_ResultFile As String
        str_BatchFile = "Temp.Bat"
        str_ResultFile = "Results.txt"
        
        'PURPOSE: Create the batch file & place the
        '         results in this file - str_ResultFile
        Dim iFile As Integer
        iFile = FreeFile
        Open str_BatchFile For Output As iFile
          Print #iFile, "REM Place results into this file -" & str_ResultFile
          Print #iFile, sCommand & " > " & str_ResultFile
        Close iFile
        
        'PURPOSE: Run the batch file in hidden
        '         mode and wait until DOS is done.
        Dim lng_Shell As Long
        lng_Shell = Shell(str_BatchFile, vbHide)
        lng_Shell = OpenProcess(SYNCHRONIZE, False, lng_Shell)
        Call WaitForSingleObject(lng_Shell, INFINITE) 'WaitGlobalObject() API
        Call CloseHandle(lng_Shell)
        
        If Dir(str_ResultFile) = str_ResultFile Then
          'PURPOSE: Place the entire results into the string f_DOS_Result
          iFile = FreeFile
          Open str_ResultFile For Input As iFile
          f_DOS_Result = Input(LOF(iFile), iFile)
          Close iFile
        End If
        
        'PURPOSE: Don't need these files
        Kill str_BatchFile
        Kill str_ResultFile
    End Function
    Chemically Formulated As:
    Dr. Nitro

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Winston Salem, NC
    Posts
    13
    Thanks for the replies. Gave me a lot of good info. I will try some of these out in the morning.

    Thanks Again,
    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width