LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Need to run a shutdown function in a CVI program before Windows XP shuts down and kills my CVI process.

I need to run a CVI program that will take data over many days. The data buffers get saved periodically within the program but I need to be able to shut the program down gracefully (to turn off power supplies, etc.) if, for whatever reason, Windows XP is going to shut down and kill my program. What/how can my CVI program get a message from Windows that it is shutting down so that I can execute a shutdown function within my CVI program before the process is killed?

Thanks!
libertycvi
0 Kudos
Message 1 of 12
(8,446 Views)
You will need to handle some Windows messages in order to shutdown gracefully. Check out this document for more information. Use the InstallWinMsgCallback function in the Programmers toolbox to install a callback for the particular message. When the callback fires, you can start shutting down your application.

Hope this helps
Bilal Durrani
NI
0 Kudos
Message 2 of 12
(8,421 Views)
I tried to follow your suggestions but with no luck: the system closes without entering my function regardless the callback mode choosen (In-Queue of Intercept) even if trapping WM_ENDSESSION message together with WM_QUERYENDSESSION.

I attach a little program written in CVI 7.0 to test this behaviour. The same functions integrated in an existing program in CVI 6.0 don't work at all.

Any help will be appreciated, since my application must run an endurance test on prototypes and I must be absolutely sure not to leave the equipment in an uncontrolled condition.

BTW, is there any difference between using the shutdown function on start menu or pressing the On/off buttons on the computer (on some machines this can fire the shutdown of the system)?

Running on WinXP machine.

Thanks
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 12
(8,374 Views)
I tried this on an XP system as well. I just registered for the QUERYENDSESSION message and I seem to get a notification for it. I tried this with 7.0 and 7.1. Try registering just for this notification and not WM_ENDSESSION.

err = InstallWinMsgCallback (mainH, WM_QUERYENDSESSION, endCbk, VAL_MODE_INTERCEPT,
NULL, &endHandle);

The newer machines seem to handle shutting down via the On/Off button as a system shutdown, probably because the mother board supports it. Most of the newer systems have ACPI support which provides a way to control this behavior. The OS has to have support for this as well. I think most OS's install ACPI support by default if the motherboard supports it.

You might also consider creating a windows service to handle the state of your devices. Services can remain running on the machine if a user just logs out. You can create services in CVI with Console Apps. You will need a utility called srvany.exe (just google for it), which can take any application (some restrictions apply) and register it as a service on the system. If you go that route, you will need to use the SDK function SetConsoleCtrlHandler() to handle the log off/ log on/exit messages that a console window gets.
Bilal Durrani
NI
0 Kudos
Message 4 of 12
(8,346 Views)
Hi Bilal,
I had to suspend studies on this item for a while. Now I tried again with the same problems: the computers disconnects and stops regardless the use of the program which should trap and reject the message.

I have a supplementary abnormal issue about it: the computer normally manages the disconnect request terminating the session and prompting me with the ordinary Ctrl-Alt-Canc logon screen. However, when this little program is running, the disconnection request drives me always to computer reboot! 😠

Have you some idea about it?


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 12
(8,232 Views)
Forgot to say that situation didn't changed after upgrading to CVI 7.1.1


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 12
(8,226 Views)
Hmmmm, thats wierd. I dont know why InstallWinMsgCallback() isnt catching that message. I'll have to check that out some more.

Anyhoo, we can try another approach. Try overrdie the WndProc of the CVI window and returning FALSE for that message
to notify Windows that you're not ready to shutdown

So get rid of the InstallWinMsgCallback() calls


GetPanelAttribute(mainH,ATTR_SYSTEM_WINDOW_HANDLE,&systemHandle);

//You will need to include windows.h for SetWindowLong
wpOrigEditProc = (WNDPROC) SetWindowLong((HWND)systemHandle,
GWL_WNDPROC, (LONG) EditSubclassProc);

DisplayPanel (mainH);

And then in the WndProc

LRESULT APIENTRY EditSubclassProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
if(uMsg == WM_QUERYENDSESSION)
{
MessagePopup ("Chiusura sistema", "Ricevuto WinMsg.");
return FALSE;
}

return CallWindowProc(wpOrigEditProc, hwnd, uMsg,wParam, lParam);
}


Let me know how you fare with this. This seemed to work every time I tried to restart the XP machine.
Bilal Durrani
NI
0 Kudos
Message 7 of 12
(8,202 Views)
So it looks like InstallWinMsgCallback() does the same thing I just did. I get the message and sutff that I'm looking for (change the callback mode to VAL_MODE_INTERCEPT and you get the message notification as well). But the reason it wasnt working was that it ends up passing the message on to either the CVI WndProc or the windows WindProc, which means its going to shut down anyway. So thats why swallowing that event wasnt working. The Help says that passing 1 will ensure that the message is not processed by the CVI WndProc, but it didnt really mention that it will get passed on to the default windows WndProc. I'll see if I can make this a little more clear in the help.
Bilal Durrani
NI
0 Kudos
Message 8 of 12
(8,177 Views)
Maybe be off the trail, but this we work good.

int main()
{
...
InstallMainCallback (EventFunctionName, 0, 0); //EVENT_END_TASK
RunUserInterface ();
...
return 0;
}
int CVICALLBACK EventFunctionName (int panelOrMenuBarHandle, int controlOrMenuItemID, int event, void *callbackData, int eventData1, int eventData2)
{
if (event==EVENT_END_TASK)
{
if (ConfirmPopup ("Ukončit aplikaci", "")!=1)return -1;
}
return 0;
}

Tested on WindowsXP profesional / CVI7.1.1
PowerButton - OK
Start\shutdow\restart - ok
Killapp -ok

Message Edited by ovrabek/CZ on 06-25-2005 05:04 PM

Message 9 of 12
(8,160 Views)
hi all,

have the same problem and testet the possibilities using "InstallWinMsgCallback()" and also "EditSubclassProc()".

The second runs ok.

But have you found a "CVI-internal" method using "InstallWinMsgCallback" that runs ok ? I mean: without using windows-API ?

best regards

Simon
0 Kudos
Message 10 of 12
(7,163 Views)