Link to home
Start Free TrialLog in
Avatar of lmmedina
lmmedina

asked on

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?
Avatar of Greybird
Greybird

you sould do :
TForm::Dispatch(&Msg);
Avatar of lmmedina

ASKER

Thanks so much, but it doesn't work. What could be happen? What should be Msg values?
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.
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of George Tokas
George Tokas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
}
//---------------------------------------------------------------------------