codice:
#include <windows.h>
#define CHECKINTERVAL 5 //Secondi ogni quanto controllare
typedef DWORD (*RSP)(DWORD dwProcessId, DWORD dwType);
bool ReadMessages(HANDLE hSlot);
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
LPSTR lpszSlotName = "\\\\.\\mailslot\\stoptime_mailslot";
if (*lpCmdLine=='\0')
{
HANDLE hSlot= CreateMailslot(lpszSlotName,
0, // no maximum message size
MAILSLOT_WAIT_FOREVER, // no time-out for operations
(LPSECURITY_ATTRIBUTES) NULL); // no security attributes
if (hSlot==INVALID_HANDLE_VALUE)
{
MessageBox(0,"Can't create mailslot. Probably another instance of StopTime is still running.","StopTime",MB_ICONERROR);
return 1;
}
//Tempo prima della pausa
__int64 old;
//Tempo dopo la pausa
__int64 now;
//Tempo da assegnare
SYSTEMTIME st;
RSP ProcAddr;
//Nasconde l'eseguibile dalla lista dei processi di Windows 9x
HINSTANCE hInst=LoadLibrary("KERNEL32");
if (hInst != NULL)
{
ProcAddr = (RSP) GetProcAddress(hInst,"RegisterServiceProcess");
if (NULL!=ProcAddr)
{
(ProcAddr)(GetCurrentProcessId(),1);
}
}
//Ciclo dipendente dalla funzione ReadMessages;
//se c'è un messaggio che dice di uscire restituisce false e il ciclo termina
while (ReadMessages(hSlot))
{
GetSystemTimeAsFileTime((FILETIME *) &old);
Sleep(CHECKINTERVAL*1000);
GetSystemTimeAsFileTime((FILETIME *) &now);
if ((now-old>(CHECKINTERVAL+CHECKINTERVAL*0.1)*10000000) || (now-old<(CHECKINTERVAL-CHECKINTERVAL*0.1)))
{
old+=CHECKINTERVAL*10000000;
FileTimeToSystemTime((FILETIME *)&old,&st);
SetSystemTime(&st);
}
}
CloseHandle(hSlot);
}
else if (strcmp(lpCmdLine,"-?")==0||strcmp(lpCmdLine,"/?")==0)
{
MessageBox(0,
"StopTime by Matteo Italia: preserves the system clock from modifications.\n\n"
"Syntax:\n"
"StopTime Starts StopTime;\n"
"StopTime -? Shows this help screen;\n"
"StopTime /? Shows this help screen;\n"
"StopTime <parameters> Sends <parameters> to the StopTime mailslot.\n\n"
"Actually only \"terminate\" parameter is supported; it makes StopTime terminate.",
"StopTime",MB_ICONINFORMATION);
}
else
{
BOOL fResult;
HANDLE hFile;
DWORD cbWritten;
hFile = CreateFile(lpszSlotName,
GENERIC_WRITE,
FILE_SHARE_READ, // required to write to a mailslot
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(0,"Can't open the mailslot. Probably StopTime is not running.","StopTime",MB_ICONERROR);
return 2;
}
fResult = WriteFile(hFile,
lpCmdLine,
(DWORD) lstrlen(lpCmdLine) + 1, // include terminating null
&cbWritten,
(LPOVERLAPPED) NULL);
if (!fResult)
{
MessageBox(0,"Can't write on the mailslot.","StopTime",MB_ICONERROR);
return 3;
}
fResult = CloseHandle(hFile);
if (!fResult)
{
MessageBox(0,"Can't close mailslot handle.","StopTime",MB_ICONERROR);
return 4;
}
MessageBox(0,"Message successfully sent to the mailslot.","StopTime",MB_ICONINFORMATION);
}
return 0;
}
bool ReadMessages(HANDLE hSlot)
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPSTR lpszBuffer;
CHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, "StopTimeEvent");
if( NULL == hEvent )
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
// Mailslot handle "hSlot1" is declared globally.
fResult = GetMailslotInfo(hSlot, // mailslot handle
(LPDWORD) NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD) NULL); // no read time-out
if (!fResult)
{
MessageBox(0,"Can't get mailslot infos.","StopTime",MB_ICONERROR);
return false;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
return true;
}
cAllMessages = cMessage;
while (cMessage != 0) // retrieve all messages
{
// Allocate memory for the message.
lpszBuffer = (LPSTR) GlobalAlloc(GPTR,
lstrlen((LPSTR) achID) + cbMessage);
if( NULL == lpszBuffer )
{
MessageBox(0,"Can't allocate memory to read the message from the mailbox.","StopTime",MB_ICONERROR);
return false;
}
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot,
lpszBuffer,
cbMessage,
&cbRead,
&ov);
if (!fResult)
{
MessageBox(0,"Can't read the message from the maibox.","StopTime",MB_ICONERROR);
GlobalFree((HGLOBAL) lpszBuffer);
return false;
}
// Interpretes the message
if (strcmp(strlwr(lpszBuffer),"terminate")==0)
{
MessageBox(0,"Terminate message recieved; press OK to exit StopTime.","StopTime",MB_ICONINFORMATION);
return false; //Esce
}
GlobalFree((HGLOBAL) lpszBuffer);
fResult = GetMailslotInfo(hSlot, // mailslot handle
(LPDWORD) NULL, // no maximum message size
&cbMessage, // size of next message
&cMessage, // number of messages
(LPDWORD) NULL); // no read time-out
if (!fResult)
{
MessageBox(0,"Can't get mailslot infos.","StopTime",MB_ICONERROR);
return false;
}
}
return true;
}
@danielessj5