disabling the cmd close button by batch command

2019-07-18 10:13发布

问题:

im kinda new to the batch scripting.i had a problem recently about how to disable the close button of the cmd while the batch file is running.i saw some posts about how to overcome this.but the things stated there was out of my reach..if any1 could point me to a correct direction, it would be really great. it would be really preferred if sum1 can tell me how to do what i mentioned before inside my batch file.so when i use it in some other pc the effect would still be there...

thanks

回答1:

You can't.

You could hide the execution of the batch file by using a VBScript

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("yourbatchfile.bat"), 0, True

That would hide it from the user, but it wouldn't stop them from killing it in task manager.

What you're asking can't really be done.



回答2:

You could create an executable that disables the [X] button for all processes named "cmd", and run that executable the first line of your batch file.

Here is a c# program that does just that:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Remove__X__Button_from_another_process
{

class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("user32.dll")]
    static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);

    const uint SC_CLOSE = 0xF060;
    const uint MF_BYCOMMAND = 0x00000000;

    static void Main(string[] args)
    {
        //Console.Write("Please enter process name:"); // "cmd"
        //String process_name = Console.ReadLine();

        Process[] processes = Process.GetProcessesByName("cmd");
        foreach (Process p in processes)
        {
            IntPtr pFoundWindow = p.MainWindowHandle;

            IntPtr nSysMenu = GetSystemMenu(pFoundWindow, false);
            if (nSysMenu != IntPtr.Zero)
            {
                if (DeleteMenu(nSysMenu, SC_CLOSE, MF_BYCOMMAND))
                {

                }
            }
        }
        Environment.Exit(0);
    }
}