webkiller 发表于 2011-12-26 15:44:59

如何为计算机增加启动日志

Logon.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#include "Registry.hpp"
#pragma hdrstop
//---------------------------------------------------------------------------
//USEFORM("Mainform.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
   int Number=0;
   TRegistry *Registry = new TRegistry;
   Registry->RootKey = HKEY_USERS;
   if(!Registry->OpenKey(".DEFAULT\\MonitorLogon\\History", false))
   { //如果打开主键失败
   Registry->CreateKey(".DEFAULT\\MonitorLogon\\History"); //创建主键
   if(!(Registry->OpenKey(".DEFAULT\\MonitorLogon\\History", false)))
   {
       return 0;
   }
   else
   {//将程序设置为开机时自动运行程序
      TRegistry *Reg = new TRegistry;
      Reg->RootKey = HKEY_LOCAL_MACHINE;
      Reg->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
      Reg->WriteString("MonitorLogon",Application->ExeName);
      Reg->CloseKey();
      delete Reg;
      Application->MessageBox("程序注册成功!","信息提示",MB_OK); // ②
      Registry->WriteInteger("开机次数",Number);
      Registry->WriteString("程序注册时间",Date()+Time());
      return 0;
      }
   }
   else
   {
   Number=Registry->ReadInteger("开机次数");
   Registry->WriteInteger("开机次数",Number+1); //记录开机次数
   Registry->WriteString("第"+AnsiString(Number+1)+"次开机",Date()+Time()); //记录开机时间
   }
   Registry->CloseKey();
   delete Registry;
   Application->Initialize();
   Application->Run();
}
catch (Exception &exception)
{
   Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------

Mainform.h
//---------------------------------------------------------------------------

#ifndef MainformH
#define MainformH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
private:    // User declarations
public:      // User declarations
      __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Mainform.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Mainform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
      : TForm(Owner)
{
}
//---------------------------------------------------------------------------



页: [1]
查看完整版本: 如何为计算机增加启动日志