DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: disabling close option in command window while executing batch file

  1. #1
    Join Date
    Jul 2008
    Posts
    1

    disabling close option in command window while executing batch file

    Hi Friens,

    I m new to VB and new member of this forum.

    I am executing a batch file using the following command in VB
    rc = shell(App.Path & "\deletepdf.bat " & fname).
    It works fine.

    For executing the batch file, command window opens. I want to disable the close option(right upper corner) in the command window bcos I dont want the user to terminate batch job manually.

    Could anyone please help regarding this.
    Is there any option in the shell command itself to disable the close option of command window. Let me know.

    Thanks,
    Vishnuvardhan

  2. #2
    Join Date
    Mar 2008
    Location
    Lancashire, UK
    Posts
    156
    Welcome to DevX



    I've used a console window in the past as a 'console' for my program. Here's how i disabled the close button:

    Declarations:
    Code:
    'Console Functions
    Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
    Private Declare Function AllocConsole Lib "kernel32" () As Long
    Private Declare Function FreeConsole Lib "kernel32" () As Long
    
    'Menu Functions
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    Private Declare Function RemoveMenu Lib "user32" (ByVal hmenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hmenu As Long) As Long
    Private Const MF_BYPOSITION = &H400&
    
    'Other Functions
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Procedure:
    Code:
    Const sTitle = "Consonle McGraw"
    Dim hSysMenu As Long, MenuCount As Long
    Dim ConsolehWnd As Long, Tick As Long
    
    
    AllocConsole
    SetConsoleTitle sTitle
    
    
    '---
    'Code omitted where i obtained handles for input/output etc
    '---
    
    
    ConsolehWnd = 0
    Tick = GetTickCount()
    
    Do
        ConsolehWnd = FindWindow(vbNullString, sTitle)
        
    Loop While (Tick + 1000) > GetTickCount() Or ConsolehWnd = 0
    
    
    If ConsolehWnd Then
        hSysMenu = GetSystemMenu(ConsolehWnd, 0)
        MenuCount = GetMenuItemCount(hSysMenu)
        RemoveMenu hSysMenu, MenuCount - 4, MF_BYPOSITION
        'You'll notice that the close menu is fourth from the bottom in the list
        'You could use MF_BYCOMMAND if you wanted
    Else
        MsgBox "Couldn't Find the Console Window"
    End If
    
    FreeConsole

    If you research, you can do all sort of things with a console, including printing on it where ever you want, set up a callback, and so on.

    AllAPI has some good tutorials on the console.




    Edit: Just realised you don't use alloc console. The FindWindow stuff will still work, you'd just need to know the window's title, either by setting it in the batch file (e.g. "Title Test") or you could allocate the console before shelling the batch file, then the batch file will process in the console you've already allocated. I think
    Last edited by CptNeutral; 07-11-2008 at 11:24 AM.

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663

    Thumbs up

    Very slick!

  4. #4
    Join Date
    Mar 2008
    Location
    Lancashire, UK
    Posts
    156
    Tar

    Or, if you want to stick with your method, you could 'convert' the pID (returned from Shell) into a hWnd. A bit like this:


    Code:
    Private Const GW_HWNDNEXT = 2
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    'Return the window handle for an instance handle.
    Private Function pIDTohWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long
    Dim test_pid As Long
    Dim test_thread_id As Long
    
    'Get the first window handle.
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    
    'Loop until we find the target or we run out
    'of windows.
    Do While test_hwnd <> 0
        'See if this window has a parent. If not,
        'it is a top-level window.
        If GetParent(test_hwnd) = 0 Then
            'This is a top-level window. See if
            'it has the target instance handle.
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
            
            If test_pid = target_pid Then
                'This is the target.
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        
        'Examine the next window.
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
    
    
    End Function
    
    Sub Test()
    Dim rc As Long, ConsolehWnd As Long
    
    rc = Shell(App.Path & "\deletepdf.bat " & fname)
    'rc = pID
    
    ConsolehWnd = pIDTohWnd(rc)
    'find the console's hWnd/get a handle to it
    
    If ConsolehWnd Then
        hSysMenu = GetSystemMenu(ConsolehWnd, 0)
        MenuCount = GetMenuItemCount(hSysMenu)
        RemoveMenu hSysMenu, MenuCount - 4, MF_BYPOSITION
    Else
        MsgBox "Couldn't Find the Console Window"
    End If
    
    
    End Sub
    I had to nick the pIDTohWnd() function from vbhelper, i couldn't remember how it went.


    ------------


    Just realised, in the code in my first post, one line should be changed from
    Code:
    Loop While (Tick + 1000) > GetTickCount() Or ConsolehWnd = 0
    to
    Code:
    Loop While (Tick + 1000) > GetTickCount() AND ConsolehWnd = 0

  5. #5
    Join Date
    Mar 2008
    Location
    Lancashire, UK
    Posts
    156
    Forgot to mention, if your user presses Ctrl+C or Ctrl+Break (or perhaps some other keystrokes) in the command window, it'll ask them to stop execution of the batch file.

    You can catch these messages by setting callbacks up, but i think this might be going a bit too far.

    Just thought i'd say in case it was important. If you want, i can find a link for you to prevent Ctrl+C/Ctrl+Break.

  6. #6
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    BTW - If there is no need for the user to see the command window, then :-

    rc = shell(App.Path & "\deletepdf.bat " & fname, vbHide)

    Greg

  7. #7
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Batch files are usually simple things.

    Why not translate the job done in the batch file in VB?

    You would have complete control and could run the thing without displaying anything if needed.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

Similar Threads

  1. Problem compiling a C++ file in Linux
    By kepler in forum C++
    Replies: 3
    Last Post: 08-26-2007, 12:11 AM
  2. mutual exclusive write to Excel file
    By extreme in forum VB Classic
    Replies: 0
    Last Post: 06-13-2007, 09:51 PM
  3. Replies: 0
    Last Post: 01-05-2001, 03:18 PM
  4. How to execute a batch file from an asp+ script
    By Tom Ashworth in forum ASP.NET
    Replies: 1
    Last Post: 09-08-2000, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center