Question

Stop Windows XP Shutdown, perform action, then restart shutdown.

Asked by: MusicBuddha

Hi,

Firstly I am using Borland C ++ Builder 5, so any code would need to be in Builder or easily portable.

I need to interrupt the shutdown of windows xp, hopefully by trapping the WM_QUERYENDSESSION message, and also trap the type of shutdown: Reboot or Restart etc.

Then I need to perform some cleanup stuff, which includes launching another app, so the shutdown process needs to be stopped not just halted.

Then after the cleanup is done I'd like to restart the shutdown process using ExitWindowsEx, and also use whichever of the following flags was trapped in the initial shutodwn that was stopped.

EWX_REBOOT
EWX_SHUTDOWN      
EWX_POWEROFF      

Can you please supply full code example of how to do this from within a simple Builder Dialog App?

Thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-01-22 at 06:25:48ID21705682
Tags

shutdown

,

windows

,

xp

Topic

Operating Systems Miscellaneous

Participating Experts
5
Points
500
Comments
49

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. How to trap WM_USERCHANGED
    *** Urgent *** Hi, I'm making a services for 95/98 and I need to trap WM_USERCHANGED, I try with PreTranslateMessage(MSG* pMsg) but with out success. Can you tell me how this @#$ message can be trap in my app. Thank you very much. Deesse
  2. Retrive Win messages whitout focus in Borland Builder
    Hi. I'm using Borland C++ Builder and I want to to be able to read a lot of messages from Windows (like WM_KEYDOWN,WM_KEYUP, WM_ENDSESSION ,WM_QUERYENDSESSION) in a program, whitout Windows having focus on that window. I have seen a small program example that make their own...
  3. Trapping Event Windows Shutdown
    How to trap event Windows Shutdown? If you ever use Norton Anti Virus 5, you must know, that before Windows shutdown, it will automatically scan for Virus in floppy disk. Okay, need your help again any Delphi experts. Regards, Daniel
  4. Borland C++ Builder Links
    Hi does anyone now were I can find some cool Borland C++ Builder were I can find sone help and project codes, tutors links Not C++ or VC++ Thanks please send them to Dreamvb@Yahoo.com Thanks
  5. Borland Builder and TrayIcon
    How can i use TrayIcons in my Borland Builder App ? Is there an example code or Builder Component ?
  6. WM_SHUTDOWN: Use within a Systray application
    I have a system tray application. I've noticed that if the application is active, then a LOGOFF or SHUTDOWN won't work. I've been tooling around with WM_SHUTDOWN and WM_QUERYSHUTDOWN and think that I am close, but I am missing something. My code snippet is as follows: int __...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: dcsbeemerPosted on 2006-01-22 at 10:06:29ID: 15761116

I'm no programmer, but you could try running "shutdown -a" in a command prompt. That usually stops a shutdown from happening ...unless you actually initiated shutdown yourself I think.

 

by: sof_combatPosted on 2006-01-22 at 10:12:00ID: 15761146

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4228&lngWId=3

This code was made with Microsoft Visual C++ 6.0, and it detects a shutdown and asks for the user to confirm if he wants to shutdown or not. I did not check if it uses specific msvs libraries but you can tweak this code to perform what you want.

as for dcsbeemer's suggestion.

once you detect a shutdown you would do:

system("shutdown -a");

windows.h directory would need to be included.
---------------------------------------------------------------------------------------------------------
Also, you may want to put a 20 point pointer in the Programming C++ section if you find difficulties later on.
---------------------------------------------------------------------------------------------------------

If you need help modifying the code i could try to help

Good Luck!
-Sof

 

by: sof_combatPosted on 2006-01-22 at 10:14:18ID: 15761158

By The Way, Welcome To Experts Exchange!

 

by: sof_combatPosted on 2006-01-22 at 10:23:05ID: 15761203

The program that i liked to before is an MFC app... :(

If you don't get much help try making a 20 point pointer question here:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus_Builder/

labeling it "Pointer to 500 point question"
and having your link:http://www.experts-exchange.com/Operating_Systems/Q_21705682.html

 

by: MusicBuddhaPosted on 2006-01-22 at 14:14:20ID: 15762256

dcsbeemer "try running "shutdown -a"" would involve starting a new process. in which case I would get a "process could not start because the workstation is shutting down" message.

 

by: MusicBuddhaPosted on 2006-01-22 at 14:17:29ID: 15762271

sof_combat: AFAICT the link to http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4228&lngWId=3 does not contain any code to determine what type of shutdown was first initiated IE restart, reboot, poweroff etc. Maybe I should be using something other than WM_QUERYENDSESSION . I am sure that I am replying to the suggestions in the right way ??????

 

by: MusicBuddhaPosted on 2006-01-22 at 14:23:02ID: 15762292

sof_combat I have added the 20 point pointer question. How do I assign the 20 points, does the winner of this question get them ??

 

by: dcsbeemerPosted on 2006-01-22 at 16:54:02ID: 15762867

Can't you perhaps create a batch file (*.bat) containing "shutdown -a" and have your application just query the batch file? (I'm not betting a lot on this working)

Again, I'm shooting in the dark here, but you never know when a guess can turn out to be a good guess.


I found this, might be of some help:

http://www.codeproject.com/system/AbortSystemShutdown.asp


and this:

http://www.codeproject.com/macro/winver_macros.asp



Good luck.

 

by: griffin36Posted on 2006-01-22 at 17:01:09ID: 15762883

As far as I can see, there is no way to tell whether the system is shutting down or restarting. This is even explicitly stated on Microsoft's documentation for WM_QUERYENDSESSION: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shutdown/base/wm_queryendsession.asp (look under the description of lParam).

The rest of what you want can be done:
-- Begin Code --

// Put at the top of your source
#define (WM_USER + 1) WM_PRESHUTDOWN

// Put in your window procedure
case WM_QUERYENDSESSION:
PostMessage(hWnd, WM_PRESHUTDOWN, 0, 0);

return 0; // This cancels the shutdown
case WM_PRESHUTDOWN:
// Do the stuff you want here, launch processes, etc.

// Shutdown the computer with ExitWindowsEx, see below
break;

-- End Code --

To shutdown the computer, use the code from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shutdown/base/how_to_shut_down_the_system.asp

As stated earlier, there is no way to tell what kind of shutdown you want to do, however. One way to solve this is to just pick one (but this is not ideal).

The best way to deal with this would be to equip your program to clean up after itself without starting another program. You could put this code inside your WM_DESTROY message and you'd be all set. What is it that you need done by another program?

 

by: griffin36Posted on 2006-01-22 at 17:04:56ID: 15762894

With regards to dcsbeemer's comment, AbortSystemShutdown only works on shutdowns started with InitiateSystemShutdown, which is different from ExitWindowsEx, and won't work after the shutdown has been started.

 

by: sof_combatPosted on 2006-01-22 at 20:47:08ID: 15763642

"AFAICT the link to http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4228&lngWId=3 does not contain any code to determine what type of shutdown was first initiated IE restart, reboot, poweroff etc. Maybe I should be using something other than WM_QUERYENDSESSION ."

As griffin36 also stated:

lParam
    This parameter can be one of the following values.
    Value       Meaning
    0       The system is shutting down or restarting (it is not possible to determine which event is occurring).
- Taken From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shutdown/base/wm_queryendsession.asp

--------------------------------------------------------------------------------------------------------------------------------------
"I am sure that I am replying to the suggestions in the right way ??????"
Yes, there is no wrong response as long as you have a question or comment reguarding the issue.
--------------------------------------------------------------------------------------------------------------------------------------

"I have added the 20 point pointer question. How do I assign the 20 points, does the winner of this question get them ??"

In order for the person who answers this question to recieve points from your pointer: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus_Builder/Q_21705930.html they would need to post a message there and you would need to accept it. I haven't made a pointer myself, so I dont know if you can ask for the pointer to  be deleted. If it can be deleted you would post a message in the Community Support area: http://www.experts-exchange.com/Community_Support/

 

by: sof_combatPosted on 2006-01-22 at 20:48:54ID: 15763646

Oops, forgot one thing, if no one posts in your pointer question, you can delete it yourself.

 

by: MusicBuddhaPosted on 2006-01-22 at 21:10:00ID: 15763715

griffin36

That is such a pain in the butt not being able to determine if a system is restarting or shutting down!! Whilst you can't get it from the WM_QUERYENDSESSION or WindowProc() maybe there is some other way to tell if it is a restart or a shutdown????? I mean, if the system knows, maybe there is some way to query it? This part was kinda crucial cause I was thinking of making this a configurable releasable applcation. Now I know why none of the existing ones are able to offer this functionality :(.

What my external program does is start and stop Winlirc (A program that sends Infra Red signals through a Serial Port and out through an IR emitter) and then send messages to winlirc to turn off or adjust certain Home Theatre components. In between the starting and stopping of Winlirc the app changes Winlirc's comm port registry setting so that I can send the commands down a seperate serial port each time, and this allows me to access different components in the Home Theatre. I can do all the registry and sending of stuff to winlirc from within my program but I am still gonna need to start and stop Winlirc a few times.

And when you say:

// Put in your window procedure
case WM_QUERYENDSESSION:
PostMessage(hWnd, WM_PRESHUTDOWN, 0, 0);

Where do I put that in a New C++ Builder application that has a simple dialog form as a window ? In the OnCreate                                                              event for the form ??

BTW did you come to this question through the message pointer that I left in the C Builder section ??

 

by: dcsbeemerPosted on 2006-01-22 at 21:44:36ID: 15763834

OK, found this on the web:

VB:

Private Sub Form1_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    If UnloadMode = vbAppWindows Then
        MsgBox "Windows is telling me to unload"
    End If

End Sub


If it works, you need to thank a person called 00100b



You can also try this one:

VB:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
 
   If UnloadMode = vbAppWindows Then   'vbAppWindows is 2
      MsgBox "Windows is shutting down"
   End If
   
End Sub


Credits goes to jjStinger72




That's all I could get.

 

by: griffin36Posted on 2006-01-22 at 22:35:58ID: 15763983

MusicBuddha-

The part that I mentioned putting in your window procedure is standard windows API stuff. It goes into the function
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
that you define. If your C++ builder project is not a windows API project, then you trap the message as you put it and execute the code there. If you need help trapping the message, give me an example of what a message trap normally looks like for C++ builder and I'll help you out.

Also, when I was investigating the problem just now, I made a sample program and had no problem starting a process from the WM_QUERYENDSESSION message. The ideal way for you to do this is to use the function system("yourprogram.exe parameters") because this will automatically not return until the process you start exits. That way you know Winlirc has done what it's supposed to before you exit.

When you trap WM_QUERYENDSESSION, be sure to return true.

I found this thread through the OS section, not through your pointer.

If you need anything else, let me know.

 

by: MusicBuddhaPosted on 2006-01-23 at 05:27:59ID: 15765968

griffin36

"""The part that I mentioned putting in your window procedure is standard windows API stuff. It goes into the function
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
that you define. If your C++ builder project is not a windows API project, then you trap the message as you put it and execute the code there. If you need help trapping the message, give me an example of what a message trap normally looks like for C++ builder and I'll help you out."""

I have uploaded a bare, new, non API application created from C Builder 5 to http://www.harmonycentral.com.au/downloads/11.rar.

I am guessing that your code will probably go into WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) inside the project1.cpp file.

 

by: MusicBuddhaPosted on 2006-01-23 at 05:29:48ID: 15765976

dcsbeemer

Thanks for trying to help me out with this!!, but I can't use any VB Code :(. It needs to be in either C or C++.

Thanks again.

 

by: sof_combatPosted on 2006-01-23 at 06:18:19ID: 15766461

I know you stated that "the shutdown process needs to be stopped not just halted. "

Maybe we can eliminate the problem of having to detect if it is a reboot, shutdown etc... by detecting the shutdown and only delaying it until your cleanup is complete? or is that not possible?

 

by: MusicBuddhaPosted on 2006-01-23 at 06:46:17ID: 15766746

sof_combat

"Maybe we can eliminate the problem of having to detect if it is a reboot, shutdown etc... by detecting the shutdown and only delaying it until your cleanup is complete? or is that not possible?"

Eventually you would initially get the "Window Not Responding" Dialog box with a "Close Now" button and eventually this would change to having the "close now" button and a "cancel" button at which time you would have stopped the restart/shutdown completely. It's a good idea though, but I can't think of a tidy way of doing it without running into that dialog box.

 

by: dcsbeemerPosted on 2006-01-23 at 08:52:37ID: 15768003

Ah, sorry about that MusicBuddha

I don't know programming, but even I know VB & C++ isn't the same thing.

I'll keep looking though.

 

by: moh10lyPosted on 2006-01-23 at 09:55:51ID: 15768682

Hello MusicBuddha,,,

Maybe it will help you out if you post a new question to the cummunity support so they can move this question to the programming Topic Area so most programmers can see your question, or you can open a new question with 20 points in the programming T.A directed with this one 2.

Good Luck
Moh10ly

 

by: griffin36Posted on 2006-01-23 at 10:24:16ID: 15768969

MusicBuddha-

The part that I mentioned actually doesn't go into WinMain, the place it should go is not there, things seem to be handled differently in your application.

From what I can gather, your application uses something called Visual Component Library (VCL) that is usually used with Delphi, a completely different programming language. In fact, I couldn't find any information at all on using VCL with C++, however, I did find a helpful article using Delphi that hopefully should help: http://www.howtodothings.com/viewarticle.aspx?article=424

From the article, it appears you need a private method HookProc in your TForm (the name HookProc isn't important, you can change it if necessary). I've posted below my best translation of the Delphi into C++.

bool YourMainForm::HookProc(TMessage Message) { // Change YourMainForm to the actual name
if (Message.Msg == WM_QueryEndSession) { // If WM_QueryEndSession doesn't work try WM_QUERYENDSESSION
/*
Do what you want with winlirc here by using system() to run it how you want. It will still run because Windows hasn't begun shutting down.
*/
}
}

void YourMainForm::FormCreate(TObject Sender) {
  Application.HookMainWindow(HookProc);
}

Be sure to place the definition of HookProc into the private section of your TForm, and I guess FormCreate goes into the public section.

I am not a Delphi developer, nor a VCL developer, but I've done my best to adapt the article's suggestions.

By the way, how long does it take to run your winlirc commands? It sounds like it takes several seconds, by your description.

 

by: cwwkiePosted on 2006-01-23 at 10:26:01ID: 15768990

> // Put in your window procedure
> case WM_QUERYENDSESSION:
> PostMessage(hWnd, WM_PRESHUTDOWN, 0, 0);
>
> Where do I put that in a New C++ Builder application that has a simple dialog form as a window ?

You have two options;

1. override WndProd in your mainform. In the .h:

class TMainForm : public TForm
{
    ...
  public:
    typedef TForm inherited;
    virtual void __fastcall WndProc(TMessage &Msg);

in your cpp:

void __fastcall TMainForm::WndProc(TMessage &Msg)
{
    switch ( Msg.Msg )
    {
        case WM_QUERYENDSESSION:
            ...
            return;

        // you can also trap other messages here:
        case WM_ENDSESSION:
            ...
            return;// or break;

        default:
            break;
    }
    inherited::WndProc( Msg);
}

2. use a message map;

in the .h:

protected:
  BEGIN_MESSAGE_MAP
     VCL_MESSAGE_HANDLER(WM_QUERYENDSESSION, TMessage, OnQueryEndSession)
  END_MESSAGE_MAP(TForm)
  void __fastcall OnQueryEndSession(TMessage & Msg);

in the .cpp:

void __fastcall TForm1::OnQueryEndSession(TMessage &Msg)
{
    ....
 
    TForm::Dispatch(&Msg);
}

 

by: griffin36Posted on 2006-01-23 at 10:31:11ID: 15769033

It looks like cwwkie is more familiar with C++ VLC than I am. Try his suggestions.

Also, I noticed that my HookProc function doesn't return a value (but should). Have it return true inside the if statement and return false outside.

 

by: cwwkiePosted on 2006-01-23 at 10:54:08ID: 15769219

> From the article, it appears you need a private method HookProc in your TForm (the name HookProc isn't
> important, you can change it if necessary). I've posted below my best translation of the Delphi into C++.

I do not often use Delphi, but afaik it is much simpler in delphi:

  ....
private
  procedure WMQueryEndSession (var Msg : TWMQueryEndSession); message WM_QueryEndSession;
end;

Implementation

procedure TForm1.WMQueryEndSession (var Msg : TWMQueryEndSession);
begin
  if MessageDlg('Close Windows now?',
                            mtConfirmation,
                            [mbYes,mbNo], 0) = mrNo then
      Msg.Result := 0
   else
      Msg.Result := 1;
end;

> It looks like cwwkie is more familiar with C++ VLC than I am. Try his suggestions.
I came here through the pointer in the c++builder ta, but on the other hand, I have never done anything with the QueryEndSession message before. But together we can help MusicBuddha...

 

by: MusicBuddhaPosted on 2006-01-23 at 16:03:37ID: 15771770

cwwkie

Where in option 1 and 2 do I perform my own functions ?.

 

by: MusicBuddhaPosted on 2006-01-23 at 16:05:22ID: 15771779

To Everybody.

It looks to me like I am gonna have to split the points between griffin36 and cwwkie with 250 points each, and once I accept that as the solution I will raise the points on the pointer message that sof_combat suggested to 250 (which he can claim by replying to that message after I accept the solution in here)  because not only was his suggestion of a pointer message something that I would never have thought of, but it turns out that without it cwwkie may never have given his contribution to the solution.

Is everybody happy with that splitup of the points?

I would also like to thank everybody for their contributions and can only say what an excellent place this is!

 

by: MusicBuddhaPosted on 2006-01-23 at 16:07:34ID: 15771793

cwwkie,

I forgot to ask in my last post

You wouldn't happen to know of a way to determine if a system is shutting down or restarting would you?

SOF contributed this:

  0      The system is shutting down or restarting (it is not possible to determine which event is occurring).
- Taken From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shutdown/base/wm_queryendsession.asp

 

by: griffin36Posted on 2006-01-23 at 17:55:33ID: 15772361

I'm happy with that decision, MusicBuddha. Did we solve your problem, though? I'm guessing we did (because you're giving out the points) but I just want to be sure.

Good luck with your program!

 

by: MusicBuddhaPosted on 2006-01-23 at 20:20:34ID: 15772931

griffin36,

Yeah the problem is solved although I haven't had time to actually check that the code works, but what cwwkie came up with looks pretty close to some examples I found in the help files, I just didn't know how to implement it in the project.

I was gonna hold off on accepting the solution until cwwkie has answered my last question which was:

Where abouts in his Exmaple 1) and 2) do I put my own code to do the processing I need to do. I will probably use Example 1) because it looks the most familiar to me.

Do I put my code in the snippet below before the return? And should I return 0; to stop the shutdown process?

case WM_QUERYENDSESSION:
            ...
            return;

griffin36:

Maybe you know the answer anyway ???

Also is it possible to get you two guys email addresses (griffin36 and cwwkie)  in case I stumble over an unforseen problem after I set this one as solved ?? Or can you contact a member directly through the experts system system ??  It's very likely that I won't get time to try the code out until next week sometime and I'm sure that everybody would be happy to see this one solved and points collected :D

 

by: griffin36Posted on 2006-01-23 at 20:37:44ID: 15772986

For cwwkie's example one, you need to put your code right after

case WM_QUERYENDSESSION:

and before the return. The function there has return type void so you can't return anything but you can do everything you want without stopping the machine restart.

I'd be happy to give you my email, but I'd rather not post it on a popular public forum. I have a contact page on my site (http://www.enkod.com/contact.php) with an email form, and if you email me there I'll reply immediately so we have each other's email addresses. If this doesn't sound like something you want to do, I'll post my email address here; I was just hoping to avoid that.

 

by: MusicBuddhaPosted on 2006-01-23 at 20:43:17ID: 15773001

griffin36

"For cwwkie's example one, you need to put your code right after
case WM_QUERYENDSESSION:
and before the return. The function there has return type void so you can't return anything but you can do everything you want without stopping the machine restart."

Can you see a way to change the code so that I can stop the shutdown, my process may take longer than 30 seconds which will initiate the "window not responding" dialog box .. I think ?

 

by: griffin36Posted on 2006-01-23 at 21:10:08ID: 15773116

The only way I can think of for you to stop the shutdown like a return would in Windows API is if maybe TMessage has a method that lets you set what the return value should be. I really have no idea if this is the case, I'm just guessing.

Maybe cwwkie can help on this one.

 

by: MusicBuddhaPosted on 2006-01-23 at 21:27:13ID: 15773173

griffin36

*****The only way I can think of for you to stop the shutdown like a return would in Windows API is if maybe TMessage has a method that lets you set what the return value should be. I really have no idea if this is the case, I'm just guessing.*****

No problem, I'll wait for cwwkie and then hopefully I can close this off and send all the points to everybody.

 

by: cwwkiePosted on 2006-01-23 at 23:41:31ID: 15773685

> Where in option 1 and 2 do I perform my own functions ?.
Where I have put the dots, for example

void __fastcall TForm1::OnQueryEndSession(TMessage &Msg)
{
    Your code goes here
    If you still want to perform default processing, call dispatch:
      TForm::Dispatch(&Msg);
}

> You wouldn't happen to know of a way to determine if a system is shutting down or restarting would you?
The only thing I could think of is hooking functions like ExitWindowsEx.

> I will raise the points on the pointer message that sof_combat suggested to 250 (which he can
> claim by replying to that message after I accept the solution in here)
I understand why you want to do that, but it is probably not allowed. The maximum points of a question is 500 points, you would give away 750 points this way.

 

by: MusicBuddhaPosted on 2006-01-24 at 00:14:06ID: 15773799

cwwkie,

Can you show me me how to stop the shutdown after catching the WM_QUERYENDSESSION message?

""The only thing I could think of is hooking functions like ExitWindowsEx.""

Can I open up a seperate 500 point question so that you can give me detailed information (including code examples) on how to perform that ?? AFAICT it is a completely different question from the one that I initially asked in here.

""I understand why you want to do that, but it is probably not allowed. The maximum points of a question is 500 points, you would give away 750 points this way.""

Technically they are two seperate questions, however if it is not allowed, I feel obligated to split the points 3 ways (if possible) because sof_combat contributed more than just the pointer message, there was also the stuff from the microsoft library (which was useful) and we've yet to hear his position on this.

How about 200 points each for cwwkie and griffin36 and 100 points for sof_combat? How does everybody feel about that??

I really want to be fair about this cause you have all helped so much and I really wouldn't have gotten the complete solution until you showed up from the pointer message.

 

by: MusicBuddhaPosted on 2006-01-24 at 03:43:32ID: 15774503

cwwkie,

The line "typedef TForm1 inherited;" in the .h file raises an exception class EAccessViolation blah blah blah at runtime.

I am about to try option 2 to see if it works any better.

 

by: MusicBuddhaPosted on 2006-01-24 at 04:07:54ID: 15774618

cwwkie,

I just commented out the line above that was causing the problem and both option 1) and 2) trap the message properly. However, one of the things that my program needs to do is to start another program, and since the shutodwn has not been stopped, it errors with "The application failed to initialize because the window station is shutting down".

I need the code to be able to trap the WM_QUERYENDSESSION but also be able to cancel the shutdown! I will restart it after I am finished my processing.

This is also why I would like to find out if it was a shutdown or a restart before I cancel it. This way when I restart it I will know which one to use. However we can resolve that issue in another question, since the scope of this one does not really cover it.

I think the splitup of the points is fair, but I need to get the above issues cleared up before I can accept this solution.

Hopefully you can get back to me on this soon cwwkie.

 

by: cwwkiePosted on 2006-01-24 at 10:16:34ID: 15778227

Hi, I am back.

>> I need the code to be able to trap the WM_QUERYENDSESSION but also be able to cancel the shutdown!

I think setting result to 1 will do that;
void __fastcall TForm1::OnQueryEndSession(TMessage &Msg)
{
     Msg.Result=0;
}

>> However, one of the things that my program needs to do is to start another program, and since the shutodwn has not been stopped, it errors with "The application failed to initialize because the window station is shutting down".

I think you have to wait until windows has processed the result of the OnQueryEndSession. You can post a message to yourself, when it gets executed, you should be able to start another program. I will look for an example and post it here.

 

by: cwwkiePosted on 2006-01-24 at 10:23:04ID: 15778294

> You can post a message to yourself,

Add another message handler in the message map:

  BEGIN_MESSAGE_MAP
     VCL_MESSAGE_HANDLER(WM_USER+100, TMessage, OnWMUSER100)
     VCL_MESSAGE_HANDLER(WM_QUERYENDSESSION, TMessage, OnQueryEndSession)
  END_MESSAGE_MAP(TForm)
  void __fastcall OnQueryEndSession(TMessage & Msg);
  void __fastcall OnWMUSER100(TMessage& msg);

in the OnQueryEndSession post a message to your own window:

void __fastcall TForm1::OnQueryEndSession(TMessage &Msg)
{
     Msg.Result=0;
     PostMessage(Handle,WM_USER+100,0,0);
}

In the OnWMUSER100 do whatever you want to execute:

void __fastcall TForm1::OnWMUSER100(TMessage& msg)
{
     // start your application here
}

 

by: MusicBuddhaPosted on 2006-01-24 at 18:02:07ID: 15782926

cwwkie,

Thanks that is excellent, works perfectly. I am getting the following wanring when I compile though. I am happy to close this off, but it would be nice to get rid of that warning message :D

[C++ Warning] Unit1.h(18): W8027 Functions containing switch are not expanded inline

and it points to the BEGIN_MESSAGE_MAP

I have also setup the question of how to tell if windows is shutting down or restarting.

http://www.experts-exchange.com/Operating_Systems/WinXP/Q_21709076.html

 

by: sof_combatPosted on 2006-01-24 at 19:51:47ID: 15783333

As Far As I Know You Can Give more than 500 points to related questions, but not the same question. You cannot give out points for the sake of giving points either. You must have a reason. (This is from EE rules) So I feel that for what I have done 100 points is MORE than enough points.

I am sorry that I wasn't able to help you further than I did, luckily griffin36 & cwwkie came in.

 

by: MusicBuddhaPosted on 2006-01-24 at 20:30:13ID: 15783482

cwwkie,

My original post on the restart or reboot question has been moved to http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21709076.html


sof_combat,

If it is allowed, I believe that I should raise the points in the pointer message to at LEAST 200 and split the 500 points between griffin36 200 & cwwkie 300. I think cwwkie deserves a little more because whilst I am sure that griffin36 and I would have got there is the end, cwwkie really produced the complete final solution. I hope you are ok with that griffin36.

Apart from your initial contribution from the MS website on the reboot shutdown stuff, cwwkie may never have arrived here had it not been for your suggestion about the pointer message, and since he supplied the final solution it might never have arrived had it not been for your pointer message. So in reality it could be argued that your pointer message actually (although indirectly)  provided the solution.

Maybe we could have an Administrator rule on what is and what is not allowed with regard to adding points to the pointer message in this situation ??.






 

by: sof_combatPosted on 2006-01-24 at 20:52:07ID: 15783571

Sure, when its always best to check with the authorities.

 

by: MusicBuddhaPosted on 2006-01-24 at 21:56:24ID: 15783778

griffin36, cwwkie and sof_combat,

I've spoken to the management and whilst they haven't completely ruled out upping the points on the pointer question, it didn't sound like they were too keen on it either.

So okay then ...... this is how I have decided to divvy up the points. sof_combat = 100; griffin36 = 150; cwwkie = 250; I have given this a lot of thought and I don't think I can do it any fairer without splitting hairs.

So as soon as cwwkie can get back to me about weather he can get rid of that warning message or not (with code if the answer is yes :D) I will accept the solution as stated above.

I sincerely would like to again thank everybody invloved in solving this problem for me, you have all been great, and for me this really is a superb place!

Best Regards,

MusicBuddha

 

by: MusicBuddhaPosted on 2006-01-24 at 23:45:48ID: 15784078

cwwkie,

I have gotten rid of the wanring message, so thankyou all very much again and I will consider this question answered :D

 

by: cwwkiePosted on 2006-01-24 at 23:49:23ID: 15784090

> but it would be nice to get rid of that warning message :D

go to compiler options (Project->Options[Shit+Ctrl+F11]->Compiler tab), click Warnings button, in the warnings list find "W8027 Functions containing..." and uncheck it.

Or put in the sourcecode the following line before the BEGIN_MESSAGE_MAP
#pragma warn -8027
You can enable the warning again after the END_MESSAGE_MAP using
#pragma warn +8027

 

by: cwwkiePosted on 2006-01-24 at 23:50:03ID: 15784093

oops, did not update before posting

 

by: cwwkiePosted on 2006-01-25 at 10:04:36ID: 15788629

> My original post on the restart or reboot question has been moved to
> http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21709076.html

As you might have noticed, both the original and the new url are working. But it is even easier if you enter http:Q_21709076.html (just like that, it is automatically converted to the correct url)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...