Respond to the system shutdown process - vb6

I built a VB6 application to run in the background and tell me when my caps lock key gets cycled. It starts when Windows starts, so it's always on, but when I go to shutdown the computer, the app hangs the shutdown process in Windows 8.
Is there a system-wide event to monitor, or some other method of catching the shutdown command I can use to kill my app gracefully?

The standard way to be notified of a system shutdown is via Form_QueryUnload(). When the OS is shutting down, the UnloadMode parameter will be equal to vbAppWindows. For example:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then
' OS is shutting down. Close gracefully.
End If
End Sub
Alternatively, you can subclass your window and listen for the WM_QUERYENDSESSION message.

Related

Is there a way to "release" comm ports after the application quits irregulary?

I am working on vb.net dll, for reading values from a bar code scanner, connected via serial comm port. If the app closes irregularly (i.e.: killed by task manager), the ports stay bound to the app, and i have to restart the whole program.
I would like to know if there is a way to somehow release those ports, so i can only restart the crashed app?
Port is initialized:
Public Sub Init(pPort As String)
port = New SerialPort(pPort, 9600, Parity.None, 8, StopBits.One)
port.Handshake = Handshake.None
port.ReadTimeout = 1000
port.WriteTimeout = 1000
port.RtsEnable = True
port.DtrEnable = True
port.Open()
End Sub
Port is closed after use:
Public Sub Close()
port.Close()
port = Nothing
End Sub
As said before, if the app closes irregularly, it does not close the port.
If I try to initialize again i get the error port denied.
What can I do in this case?

How to terminate an inheritible process gracefully

In order to hoop the stdout of a console application, my process manager start the console application using CreateProcess with bInheritHandles to be true, everything works except that when my process manager was closed but the console application was still alive, if try to restart my process manager, Windows will report error that the port was in used (my process manager open a port to play the service role), and if i use TCPView to check the port status, i found my process manager seems to be still “alive”, the PID was still there but it could not be found in Task Manager, the port was listed in the TCPView but could not be found via NETSTAT command — Windows failed to terminate my process manager due to the inherited handles of the console application, if i need to restart my process manager, i have to kill the console application first.
Is there any way to terminate my process manager gracefully even when the inherited console application still alive?

Shut down a UDP Server receiving requests

I made a UDP server class and my program creates a process (running in the background). It is a command line utility, and so running 'udpserver.exe start' would bind the socket and begin a blocking recvfrom() call inside a for(;;) loop.
What is the best way to safely and 'gracefully' stop the server?
I was thinking about 'udpserver.exe stop' would send a udp msg such as 'stop' and the ongoing process from 'udpserver.exe start' would recognize this msg, break from the loop, and clean up (closesocket/wsacleanup).
Also, is just killing the process not a good idea?
Why are you running the UDP server as an external process instead of as a worker thread inside of your main program? That would make it a lot easier to manage the server. Simple close the socket, which will abort a blocked recvfrom(), thus allowing the thread to terminate itself.
But if you must run the UDP server in an external process, personally I would just kill that process, quick and simple. But if you really want to be graceful about it, you could make the server program handle CTRL-BREAK via SetConsoleCtrlHandler() so it knows when it needs to close its socket, stopping recvfrom(). Then you can have the main program spawn the server program via CreateProcess() with the CREATE_NEW_PROCESS_GROUP flag to get a group ID that can then be used to send CTRL-BREAK to the server process via GenerateConsoleCtrlEvent() when needed.

Qt 5.1 Windows 7 - aboutToQuit() not called on windows logoff

If I exit my application normally, aboutToQuit is called and I can do some cleanup, however, if the user logs off of windows, my application closes immediately and aboutToQuit is never called.
The program functions as an application launcher that logs to a server. Each time an application launches, it sends a message to a server with the name (you launch the application from within my app).
Normally, a user shuts down the application when they are done and I log the event. If they shutdown the computer though, I want to send a application's shutdown log event in aboutToQuit, but that function is never called.
I've searched for options and short of making my application into a Windows service (haven't tried this yet), nothing is working.
Any ideas?
This appears to be fixed in Qt5.2.0 as I now receive a QApplication::commitDataRequest() signal at windows logoff.

VBScript - catching End Task signal

I have a script written in VBS that checks every second if the LAN port has a connection and if so, disables the wireless, or enables the wireless if no LAN connection exists. There's an unfortunate bug in this that leaves the wireless disabled if you shut down the computer while this script is running that I would like to fix. The script runs at logon via a GPO, so it won't run if one logs in off the network.
Is it at all possible to catch the End Task signal and perform cleanup operations before the script ends? I'd like for this to re-enable the wireless as the system shuts down.
Thanks
No i think it is not possible and if so it would be unreliable or slow down your pc i'm afraid. But just as you can have a script running at logon you can also have a script running at logoff, i suggest you take that road.

Resources