🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages Started by ���� Fri, 15 Dec 2006 17:28
Log Restart or ShutDown
#2877
Author: ����
Date: Fri, 15 Dec 2006 17:28
4 lines
994 bytes
Hi
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
Re: Log Restart or ShutDown
#2878
Author: 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/
----------------------------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads