LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Disable "Close" button of command prompt through LabVIEW using Win32 APIs

Solved!
Go to solution

Hello all,

I am trying to disable the close button of a third party console application that I am invoking through LabVIEW. I tried using GetSystemMenu() and DeleteSystemMenu() from user32.dll, but somewhere I am doing it wrong.

 

Can anyone can suggest a solution to this?

 

Thanks!

 

FraggerFox

-FraggerFox!
Certified LabVIEW Architect, Certified TestStand Developer
"What you think today is what you live tomorrow"
0 Kudos
Message 1 of 11
(9,925 Views)
Solution
Accepted by FraggerFox

Do you have the handle to Window?  Off hand, I don't know how to delete it, but here is some C# code that I used to grey out the close window button in another project:

 

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("user32.dll")]
        private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem,
           uint uEnable);

        private const Int32 SC_CLOSE = 0xF060;
        private const UInt32 MF_BYCOMMAND    =0x00000000;
        private const UInt32 MF_ENABLED = 0x00000000;
        private const UInt32 MF_GRAYED = 0x00000001;
        private const UInt32 MF_DISABLED = 0x00000002;
        private void EnableClose(bool enable)
        {
            IntPtr pSysMenu = GetSystemMenu(Handle, false);
            if (pSysMenu != null)
            {
                EnableMenuItem(pSysMenu, SC_CLOSE, MF_BYCOMMAND | (enable ? MF_ENABLED : MF_DISABLED));
            }
        }

 

This was fairly easy to convert over once I had the handle (this is from a library I picked up somewhere).

 

Hope this helps.

 

 

A

 

 

Download All
Message 2 of 11
(9,911 Views)

If the window has a name (look at titlebar) you can get the handle by window name using a user32.dll find window (I think this is the function, havent used it is a while)

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 3 of 11
(9,895 Views)

Yeah, that is the API I was using from the Library I passed on.

 

 

A

0 Kudos
Message 4 of 11
(9,888 Views)

@A:

The VI you attached works!

But I didn't understand one thing. I used the same logic, but I was providing PID of the command prompt/console application window, and not the reference which is coming out which is coming using the WINLLB function. Now when we say a "Handle" to the window, we mean that we are referring to the PID of that application, right?

Correct me if I am wrong.

 

Thanks!

FraggerFox

-FraggerFox!
Certified LabVIEW Architect, Certified TestStand Developer
"What you think today is what you live tomorrow"
0 Kudos
Message 5 of 11
(9,855 Views)

^ ^

Ok, I got that PID and handle are two different things.

One more thing, if anyone can explain. If user does CTRL+C even when the close button is disabled on the command prompt, it will close the console application/command prompt. How can we disable this?

 

Fragger Fox

-FraggerFox!
Certified LabVIEW Architect, Certified TestStand Developer
"What you think today is what you live tomorrow"
0 Kudos
Message 6 of 11
(9,847 Views)

The Ctrl-C is to be trapped by the console app.  You can't (to my knowledge) trap that signal OUTSIDE of the app and keep it from captureing it UNLESS you spawn it (IIR unix correctly), in which case it is not exaclty outside and the parent could shelter the child from the Ctrl-C.  So you would have to make a console app that would capture and withhold the Ctrl-C and then spawn this other console app you are talking about. IIRC, this can be done in shell script, but if you are using windows, you will probably need to roll your own or get cygwin or other unix emulator.  This is going way off topic from LV though.

 

Alternatively, why not just hide the consloe window?  Probably much easier unless you need the console for something.  Haven't tried, but I think there is a Hide window.vi in that library I gave you.

 

 

A

0 Kudos
Message 7 of 11
(9,834 Views)

It is possible to make the labview window the parent of the console window. I played with this in the past, might cause other problems though.

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 8 of 11
(9,826 Views)

No, not parent window, parent process (which it already is if it is the spawer of the process) and capture the Ctlr-C (which I don't think LV will do).

 

 

A

0 Kudos
Message 9 of 11
(9,821 Views)

Do you have access to the code for the console app?  You could disable the break via the winapi SetConsoleCtrlHandler() as discribed here:

 

http://stackoverflow.com/questions/1243047/how-to-disable-control-c-in-a-windowsxp-python-console-pr...

 

and here:

 

http://msdn.microsoft.com/en-us/library/ms686016%28VS.85%29.aspx

 

 

A

0 Kudos
Message 10 of 11
(9,815 Views)