Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to disable close "X" button in DOS  (Read 16259 times)

0 Members and 1 Guest are viewing this topic.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: How to disable close "X" button in DOS
« Reply #15 on: April 20, 2009, 11:59:49 AM »
Using the API you can programmatically:

Acquire the System menu of the window. (GetSystemMenu(HWND))

Call EnableMenuItem() with the appropriate flags and the SC_CLOSE ID (0x060).

the "X" button as well as the close menu option will be disabled.


Problem being, I don't think this works for command-windows,  they are a system-managed class (even uxtheme.dll can't work with them- ever notice the lack of Luna themes on cmd windows?

Also, they might use a different ID for the menu items.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How to disable close "X" button in DOS
« Reply #16 on: April 20, 2009, 12:11:02 PM »

Hello B.C.
well, what I intended was a suggested him that he writes the entire thing in Visual Basic or visuals see instead of doing it in a batch command.  And if those function calls are available in Visual Basic that would be persuaded to.
On the Microsoft might he can download or near copies of Visual Basic that do not require that.net stuff.
He and, this is done with voice recognition.  To some funny things.   :rofl:

Batcher



    Rookie

    Thanked: 5
    Re: How to disable close "X" button in DOS
    « Reply #17 on: April 20, 2009, 08:49:43 PM »
    You need a command line tool gray.exe. Then call it in your bat.

    Code: [Select]
    @echo off
    rem Call gray.exe to disable "X" button
    start /b gray.exe
    rem Your commands below
    echo This is a test
    rem Kill process gray.exe to enable "X" button after your commands finished
    taskkill /f /im gray.exe>nul
    pause

    Dias de verano

    • Guest
    Re: How to disable close "X" button in DOS
    « Reply #18 on: April 21, 2009, 12:48:39 AM »
    To some funny things.   :rofl:

    To some stupid things  ::)

    Dias de verano

    • Guest
    Re: How to disable close "X" button in DOS
    « Reply #19 on: April 21, 2009, 12:51:12 AM »
    You need a command line tool gray.exe.

    Does it exist?

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: How to disable close "X" button in DOS
      « Reply #20 on: April 21, 2009, 12:56:21 AM »
      @OP, i am not into windows login stuff, but i believe if you are having some sort of ActiveDirectory or LDAP where information of users are stored, there is no need to prompt user for any input. You just need a little bit scripting to get those information from the data store.

      Batcher



        Rookie

        Thanked: 5
        Re: How to disable close "X" button in DOS
        « Reply #21 on: April 22, 2009, 10:06:09 AM »
        Does it exist?

        Yes, it does. I got it from a friend.

        If you have interesting, I can email it to you. It's free : )

        Dias de verano

        • Guest
        Re: How to disable close "X" button in DOS
        « Reply #22 on: April 22, 2009, 10:19:01 AM »
        Yes, it does. I got it from a friend.

        If you have interesting, I can email it to you. It's free : )

        We don't do things that way on here.

        Batcher



          Rookie

          Thanked: 5
          Re: How to disable close "X" button in DOS
          « Reply #23 on: April 22, 2009, 10:21:14 AM »
          We don't do things that way on here.

          Sorry, I am newbee here. I am not familiar with the rules yet.

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: How to disable close "X" button in DOS
          « Reply #24 on: April 22, 2009, 11:01:27 AM »
          lol. I love that "I got it from a friend" bit. way to be conspicuous.
          I was trying to dereference Null Pointers before it was cool.

          Batcher



            Rookie

            Thanked: 5
            Re: How to disable close "X" button in DOS
            « Reply #25 on: April 22, 2009, 08:24:03 PM »
            lol. I love that "I got it from a friend" bit. way to be conspicuous.

            That gray.exe is developed using C++. If you're interested in the source code, I can try ask it from him.

            Reno



              Hopeful
            • Thanked: 32
              Re: How to disable close "X" button in DOS
              « Reply #26 on: April 23, 2009, 08:06:13 AM »
              Using the API you can programmatically:

              Acquire the System menu of the window. (GetSystemMenu(HWND))

              Call EnableMenuItem() with the appropriate flags and the SC_CLOSE ID (0x060).

              the "X" button as well as the close menu option will be disabled.


              pseudocode based on BC_programmer workflow as mentioned above, working vb6 code below:
              Code: [Select]
              Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
              Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
              Private Declare Function RemoveMenu Lib "user32.dll" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
              Private Const SC_CLOSE As Long = &HF060&

              Sub main()
                  hwnd = FindWindow("ConsoleWindowClass", vbNullString)
                  If hwnd = 0 Then MsgBox "Console not running": Exit Sub
                  RemoveMenu GetSystemMenu(hwnd, 0), SC_CLOSE, 0
              End Sub

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: How to disable close "X" button in DOS
              « Reply #27 on: April 23, 2009, 09:02:09 AM »
              a good method to get the handle to the proper window, instead of findwindow (we can never know what it'll find, if there is more then one instance) is to use LINK to change the subsystem to CONSOLE and then use the console functions to determine the hwnd of the console window itself, which can be used as presented here.
              I was trying to dereference Null Pointers before it was cool.

              Reno



                Hopeful
              • Thanked: 32
                Re: How to disable close "X" button in DOS
                « Reply #28 on: April 23, 2009, 09:17:57 AM »
                bc, which api are you refering to get current console hwnd? care to shed some light on me, plz  :P

                you are correct, the above code only for one instance of console.

                BC_Programmer


                  Mastermind
                • Typing is no substitute for thinking.
                • Thanked: 1140
                  • Yes
                  • Yes
                  • BC-Programming.com
                • Certifications: List
                • Computer: Specs
                • Experience: Beginner
                • OS: Windows 11
                Re: How to disable close "X" button in DOS
                « Reply #29 on: April 23, 2009, 09:38:49 AM »
                W2K and up support GetConsoleWindow().

                Code: [Select]
                Private Declare Function GetConsoleWindow Lib "kernel32" () As Long

                otherwise, you'd need to take the tortuous path.

                This is the property from the Console Module I use...

                of course... you could just use the module in a small program to change the system menu...

                Some of the variables are member variables and the API's need to be declared as well. I'd visit the url given in the comments.



                Code: [Select]
                  ' 124103 - HOWTO: Obtain a Console Window Handle (HWND)
                   ' http://support.microsoft.com/default.aspx?scid=KB;en-us;q124103
                   Dim os As OSVERSIONINFO
                   Dim Title As String
                   Dim Unique As String
                   Dim nRet As Long

                   ' Returned cached value, if possible.
                   If m_hWnd = 0 Then
                      ' Determine what operating system this is.
                      os.dwOSVersionInfoSize = Len(os)
                      Call GetVersionEx(os)

                      If os.dwPlatformId = VER_PLATFORM_WIN32_NT _
                         And os.dwMajorVersion >= 5 Then
                         ' This is Windows2000 or later!
                         m_hWnd = GetConsoleWindow()

                      Else ' Take the tortuous path...
                         ' Cache the associated title.
                         Title = Space$(1024)
                         nRet = GetConsoleTitle(Title, Len(Title))
                         If nRet Then
                            Title = Left$(Title, nRet)
                         End If

                         ' Construct unique string to use as new title.
                         Unique = Format$(Now, "yyyymmddhhnnss") & Hex$(GetCurrentProcessId())

                         ' Set new title to use for search.
                         If SetConsoleTitle(Unique) Then
                            ' Find window most likely to be our console.
                            m_hWnd = FindConsole(Unique)
                            ' Restore original title.
                            Call SetConsoleTitle(Title)
                         End If
                      End If
                   End If
                   hWnd = m_hWnd
                I was trying to dereference Null Pointers before it was cool.