🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2878

Re: Log Restart or ShutDown

#2878
From: Michel Leunen
Date: Mon, 18 Dec 2006 19:32
107 lines
4122 bytes
���� wrote:

> I want to write a little software to log (write me in a file)
> each time that the user restart or shutdown the PC
> please help

Just create a little application that start when your PC start and stop 
when you shut it down. Since this is the API group, here is a pure win32 
API solution which logs a message even if the user logs out:

#include <windows.h>
#include <string>
#pragma hdrstop

const char FileName[] = "log.txt";
//----------------------------------------------------------------------
typedef LRESULT CALLBACK (*WINDOWPROC)(HWND,UINT,WPARAM,LPARAM);
//----------------------------------------------------------------------
void LogMessage(const std::string &Message)
{
   std::ofstream OutFile;
   OutFile.open(FileName,std::ios::app);
   if(OutFile)
   {
     OutFile<<Message<<std::endl;
   }
}
//----------------------------------------------------------------------
// window procedure
LRESULT CALLBACK WndProc(HWND HWnd,unsigned int Msg,WPARAM WParam,LPARAM 
LParam)
{
   switch (Msg)
   {
   	case WM_CLOSE:
			DestroyWindow(HWnd);
			break;
     case WM_CREATE:
       LogMessage("login");
       break;
     case WM_QUERYENDSESSION:
       LogMessage("logout");
       break;
     case WM_DESTROY:
       LogMessage("logout");
       PostQuitMessage(0);
       return 0;
   }
   return DefWindowProc(HWnd, Msg, WParam, LParam);
}

HWND CreateHiddenWindow(WINDOWPROC wndproc)
{
	WNDCLASS HiddenWindowClass ;
	HiddenWindowClass.style=0;
	HiddenWindowClass.lpfnWndProc=wndproc;
	HiddenWindowClass.cbClsExtra=0;
	HiddenWindowClass.cbWndExtra=0;
	HiddenWindowClass.hInstance=0;
	HiddenWindowClass.hIcon=0;
	HiddenWindowClass.hCursor=0;
	HiddenWindowClass.hbrBackground=0;
	HiddenWindowClass.lpszMenuName=NULL;
	HiddenWindowClass.lpszClassName="HiddenWindow";
	
	if(!RegisterClass(&HiddenWindowClass))return 0;
	HWND hwnd = CreateWindow(HiddenWindowClass.lpszClassName, "",
	                       WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS |
	                       WS_SYSMENU | WS_MINIMIZEBOX,
	                       GetSystemMetrics(SM_CXSCREEN)/2,
	                       GetSystemMetrics(SM_CYSCREEN)/2,
	                       0,0,0,0,NULL,NULL);
	if(hwnd == NULL)return 0;
	HMENU	SysMenu;	
	SysMenu=GetSystemMenu(hwnd,false);
	DeleteMenu(SysMenu,SC_MAXIMIZE,MF_BYCOMMAND);
	DeleteMenu(SysMenu,SC_SIZE,MF_BYCOMMAND);
         DeleteMenu(SysMenu,SC_MOVE,MF_BYCOMMAND);
	return hwnd;
}

//----------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE HInstance,HINSTANCE HPrevInstance,
                       LPTSTR lpCmdLine,int nCmdShow)
{
  	if (CreateHiddenWindow(WndProc))
	{
	  MSG msg;
	  while (GetMessage(&msg, NULL, 0, 0)>0)
	  {
	    TranslateMessage(&msg);
	    DispatchMessage(&msg);
	  }
	}
   return 0;
}
//----------------------------------------------------------------------

HTH
Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Technical Details
Message-ID: <4586ec9e@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4586e52a@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,2aeb51e646dbe2b5
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII
Date: Mon, 18 Dec 2006 20:32:49 +0100
From: Michel Leunen <nospam@noreply.be>
Organization: www.leunen.com
User-Agent: Thunderbird 1.5.0.8 (Windows/20061025)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Log Restart or ShutDown
References: <4586e52a@newsgroups.borland.com>
In-Reply-To: <4586e52a@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 81.247.67.12
Message-ID: <4586ec9e@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1166470302 81.247.67.12 (18 Dec 2006 11:31:42 -0800)
Lines: 107
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail