WMEndSession Doubt

Hi Experts. For a reason i really don't know, my app can't close or terminate when i shut down or i restart windows. So, i use WMEndSession to force to close my app. It works, but it appears that windows stops shutdown or restart. Maybe i'm forgetting something. This is my code


class TMyForm: public TForm
{
  private:
      void __fastcall WMEndSession(TWMEndSession &Msg);
  public:
   BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER( WM_ENDSESSION, TWMEndSession, WMEndSession )
    END_MESSAGE_MAP( TForm )
};

void __fastcall TMyForm::WMEndSession(TWMEndSession &Msg)
{
    Application->Terminate();         //Or Close(); both close my app
    TForm::WMEndSession(Msg);  //This line generates an error
}
//---------------------------------------------------------------------------

Last line generates this error :
[C++ Error] MyFormClass.cpp(287): E2451 Undefined symbol 'WMEndSession'

What should i do?
lmmedinaAsked:
Who is Participating?
 
George TokasConnect With a Mentor Commented:
OnIdle is a function the application call when there is no other message or function executed... Of course this function must be declared at the header.. If not ignore it...
Anyway since you are using threads at your application probably some thread locks the execution and don't finnish so the application hangs...
It will be better if you post some more more details... the headers at least...

gtokas.
0
 
GreybirdCommented:
you sould do :
TForm::Dispatch(&Msg);
0
 
lmmedinaAuthor Commented:
Thanks so much, but it doesn't work. What could be happen? What should be Msg values?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
George TokasCommented:
You can't call the function from within the function...
Compiler treat that function as local parameter..
Replace:
>>TForm::WMEndSession(Msg);  //This line generates an error
with:
Dispatch(&Msg);

That will work...

gtokas.
0
 
George TokasCommented:
Addition:
Your app is closing when you shut down windows.... If not there is something else wrong there... Incorect handler source, OnIdle routine or something else....

gtokas.
0
 
lmmedinaAuthor Commented:
Hi, Incorrect Handler... how could happend this?... OnIdle routine means threads or something like that??? Please could u give more information. I'musing threads on my app. Could it be the reason why my code doesn't work?
0
 
lmmedinaAuthor Commented:
Hi experts, i had been looking for a lost thread or a process that were on background in my app. But it seems all ok. My app is still not closing when i try to restar or shut down windows. So, i'm trying to force it to close. But i want to know if there is a way to get if i selected "restart" or "shut down" when i catch WMEndSession message to force and use the right flag on ExitWindowsEx();

I'm doing something like this.

int __fastcall MyForm::WMEndSession(TMessage  &Msg)
{
 HANDLE token;
  TOKEN_PRIVILEGES tok,old;
  LUID priv;
  DWORD dwSize = sizeof (TOKEN_PRIVILEGES);

  PostMessage(Application->Handle,WM_CLOSE,0,0);
  DefWindowProc(Application->Handle, Msg.Msg, Msg.WParam, Msg.LParam);
IntToStr(ENDSESSION_LOGOFF));
  if(Msg.LParam==ENDSESSION_LOGOFF)
    ExitWindowsEx(EWX_LOGOFF|EWX_FORCE ,0);
  else
  { if(!OpenProcessToken(GetCurrentProcess(),
          TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&token))
      return(0);
    if(!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&priv))
      return(0);
    ZeroMemory(&tok,sizeof(tok));
    tok.PrivilegeCount=1;
    tok.Privileges[0].Luid=priv;
    tok.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    if(!AdjustTokenPrivileges(token,false,&tok,
        sizeof(TOKEN_PRIVILEGES),&old,&dwSize))
      return(0);
    ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE ,0);
  }

  return(0);
}
//---------------------------------------------------------------------------
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.