批处理命令禁用CMD关闭按钮

5

我对批处理脚本不太熟悉。最近我遇到了一个问题,就是在批处理文件运行时如何禁用cmd的关闭按钮。我看到一些帖子关于如何解决这个问题,但那些东西都超出了我的能力范围...如果有人能指引我正确的方向,那就太好了。如果有人能告诉我如何在我的批处理文件中实现之前所提到的功能,那就更好了。这样在其他电脑上使用时,效果仍然存在...

谢谢

3个回答

2

你不能这样做。

你可以使用VBScript隐藏批处理文件的执行。

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

这样做能够对用户隐藏,但是无法阻止他们在任务管理器中将其关闭。

你所要求的并不能真正实现。


谢谢@Bali c...我会在VB中尝试它.. :) - Sp3LLingzz

2

您可以创建一个可执行文件,禁用所有名为“cmd”的进程的[X]按钮,并在批处理文件的第一行运行该可执行文件。

这里是一个能够实现此功能的C#程序:

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);
    }
}

0
在批处理文件夹中创建 name.ps1 文件。
$code = @'
using System;
using System.Runtime.InteropServices;

namespace CloseButtonToggle {

 internal static class WinAPI {
   [DllImport("kernel32.dll")]
   internal static extern IntPtr GetConsoleWindow();

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DeleteMenu(IntPtr hMenu,
                          uint uPosition, uint uFlags);

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DrawMenuBar(IntPtr hWnd);

   [DllImport("user32.dll")]
   internal static extern IntPtr GetSystemMenu(IntPtr hWnd,
              [MarshalAs(UnmanagedType.Bool)]bool bRevert);

   const uint SC_CLOSE     = 0xf060;
   const uint MF_BYCOMMAND = 0;

   internal static void ChangeCurrentState(bool state) {
     IntPtr hMenu = GetSystemMenu(GetConsoleWindow(), state);
     DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
     DrawMenuBar(GetConsoleWindow());
   }
 }

 public static class Status {
   public static void Disable() {
     WinAPI.ChangeCurrentState(false); //its 'true' if need to enable
   }
 }
}
'@

Add-Type $code
[CloseButtonToggle.Status]::Disable()

添加到批处理

Powershell.exe -executionpolicy remotesigned -File name.ps1

源代码


目前你的回答不够清晰。请编辑并添加更多细节,以帮助其他人理解它如何回答所提出的问题。你可以在帮助中心找到有关如何撰写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接