Link to home
Start Free TrialLog in
Avatar of andrewsmith
andrewsmith

asked on

Forcing a shutdown/logoff

I am trying to create an application which can shutdown the computer or logoff the user (win95/98).  I am currently using this code:

exitwindowsex(ewx_logoff + ewx_force,0) //logoff

exitwindowsex(ewx_poweroff + ewx_force,0)//shutdown

But this is not working properly.  Weird things happen.  The logon box appears, but explorer and all other program (including my app) continue running.

What am I doing wrong?

Thankyou
Andrew
Avatar of DrDelphi
DrDelphi

Try this:

ExitwindowsEx(EWX_LOGOFF OR EWX_FORCE,0) //logoff

ExitWindowsEx(EWX_POWEROFF OR EWX_FORCE,0)//shutdown



Good luck!!

try  using or instead of +

ExitWindowsEx(EWX_LOGOFF or EWX_FORCE,0);
ExitWindowsEx(EWX_REBOOT or EWX_FORCE,0);
ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE,0);  
snap :)
Avatar of andrewsmith

ASKER

I have replaced '+' with 'or' but it still does not work. The screen just flashes, displayes the logon box, and everything is still running.

Any other ideas?
Found this on the M$ Knowledgebase:

INFO: ExitWindowsEx() Does Not Shut Down or Restart Win 95/98/Me (Q220706)

--------------------------------------------------------------------------------
The information in this article applies to:


Microsoft Win32 Application Programming Interface (API), used with:
the operating system: Microsoft Windows 95
the operating system: Microsoft Windows 98
the operating system: Microsoft Windows Millennium Edition


--------------------------------------------------------------------------------


SUMMARY
When you use the ExitWindowsEx() API on Microsoft Windows 95, Microsoft Windows 98, and Microsoft Windows Millennium Edition (Me) to shut down or log off the computer, the action fails intermittently on some rare hardware or driver configurations.



MORE INFORMATION
This behavior is caused by the design of the hardware or driver.

In most cases, a cooperative (user-initiated) log off or shutdown is recommended for Windows 95, Windows 98, and Windows Me.

In cases where the application must initiate shutdown or log off, it is recommended that you use the ExitWindowsEx() API to implement a non-forced programmatic log off or shutdown. A non-forced log off or shutdown allows users to save their work and settings.



REFERENCES
For more information about ExitWindowsEx(), please see the Win32 Platform SDK documentation.

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

Q216638 HOWTO: Programmatically Force Windows 95, Windows 98, and Windows Me to Log Off



PS. Q216638 refred to above has some C++ sample code... here's that too if you can use it. I don't have the time now to translate it for you. sorry.

/*++

  Copyright (c) 2001 Microsoft Corporation

  Description:
     This sample illustrates how to force a log off from Windows 95,
     Windows 98, and Windows Me.

--*/
#include<windows.h>
#include <tlhelp32.h>

BOOL WINAPI ExitWindows9x(DWORD dwFlags)
{
   if (dwFlags & EWX_FORCE)
   {
   // Enumerate and terminate all processes named Explorer. This code calls
   // the TerminateProcess API, which is usually not recommended. We must  
   // use this here, however, because Explorer's auto-restart will cause       // logoff problems.

      HINSTANCE      hInstLib;
      HANDLE         hSnapShot;
      PROCESSENTRY32 procentry;
      BOOL           bFlag;

      // ToolHelp function pointers.
      HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD);
      BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32);
      BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32);


      hInstLib = LoadLibraryA( "Kernel32.DLL" );
      if( hInstLib == NULL )
       return FALSE;

      // Get procedure addresses.
      // We are linking to these functions of Kernel32
      // explicitly, because otherwise a module using
      // this code would fail to load under Windows NT,
      // which does not have the Toolhelp32
      // functions in the Kernel 32.
      lpfCreateToolhelp32Snapshot=
                     (HANDLE(WINAPI *)(DWORD,DWORD))
                     GetProcAddress( hInstLib,
                     "CreateToolhelp32Snapshot" );
      lpfProcess32First=
                     (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
                     GetProcAddress( hInstLib, "Process32First" );
      lpfProcess32Next=
                     (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
                     GetProcAddress( hInstLib, "Process32Next" );

      if( lpfProcess32Next == NULL ||
          lpfProcess32First == NULL ||
          lpfCreateToolhelp32Snapshot == NULL )
      {
         FreeLibrary( hInstLib );
         return FALSE;
      }

      // Get a handle to a ToolHelp snapshot of the systems
      // processes.
      hSnapShot = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 );
      if( hSnapShot == INVALID_HANDLE_VALUE )
      {
         FreeLibrary( hInstLib );
         return FALSE;
      }

      // Get the first process's information.
      procentry.dwSize = sizeof(PROCESSENTRY32);
      bFlag = lpfProcess32First( hSnapShot, &procentry );

      // While there are processes, keep looping.
      while( bFlag )
      {
         {  // We need to see if the app is named Explorer.exe.
            HANDLE hProcess;
            INT nPos;

            nPos = lstrlen(procentry.szExeFile);

            if (nPos)
            {
               while (procentry.szExeFile[--nPos] != '\\' );
               if(!lstrcmpi("explorer.exe",&(procentry.szExeFile[nPos+1])))
               {
                  // Terminate the process.
                  hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,
                                                 procentry.th32ProcessID);
                  TerminateProcess(hProcess, 1);
                  CloseHandle(hProcess);
               }

            }
          }

         procentry.dwSize = sizeof(PROCESSENTRY32) ;
         bFlag = lpfProcess32Next( hSnapShot, &procentry );
      }

      CloseHandle(hSnapShot);

      // Free the library.
      FreeLibrary( hInstLib ) ;
   }

   return ExitWindowsEx(dwFlags,0);
}


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
   ExitWindows9x(EWX_LOGOFF|EWX_FORCE);

   return TRUE;
}
I don't know how to use C but if you could tell me where I could get a compiled version of the code, then I could just execute it from within my application.

Or prehaps a component of somekind?

Thanks for clearing up that it was ExitWindowsEx failing to work.

Andrew
listening
Instead of using EWX_POWEROFF alone you should better use EWX_POWEROFF or EWX_SHUTDOWN, respectively EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE.

Regards, Madshi.
this might get the shutdown dialog box

{declare the dll procedure}
procedure ExitWindowsDialog(Handle: THandle); Stdcall; External 'Shell32.dll' Index 60;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitWindowsDialog(Handle);
end;
What is the API call to logoff a computer over a network?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America 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
This question is current, but others are quite old, thus this posted in all.

Administration will be contacting you shortly.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20163146.html
https://www.experts-exchange.com/questions/Q.20184793.html
https://www.experts-exchange.com/questions/Q.20272260.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20265319.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange