Search code examples
c#installationcustom-action

How to disable Cancel button in the installer dialog


Sometimes i want disable a cancel button when installing my package, I am using visual studio installer. enter image description here

I want to disable this cancel button from code side.


Solution

  • Here is a solution with the WIN32 API:

    • Get the class name of the Window with Spy++ and search it with the function FindWindow
    • Get the Button hwnd by FindWindowEx
    • Disable it by EnableWindow

    Here is my example code:

    Win32 Function declaration:

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

    Code to disable the button:

    IntPtr hwndWindow = FindWindow("MsiDialogCloseClass", "Installer");
    IntPtr hwndButton = FindWindowEx((IntPtr)hwndWindow, IntPtr.Zero, "Button", "Cancel");
    
    if (EnableWindow(hwndButton, false))
    {
        //has been disabled
    }

    Here my test window:

    enter image description here