13,825
社区成员
发帖
与我相关
我的任务
分享
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdHTTP.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
//---------------------------------------------------------------------------
class TForm1:public TForm
{
__published: // IDE-managed Components
TButton * Button1;
TButton *Button2;
TMemo *Memo1;
TIdHTTP *IdHTTP1;
TButton *Button3;
TButton *Button4;
void __fastcall Button3Click(TObject * Sender);
void __fastcall FormCreate(TObject * Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
String __fastcall GetFileVersion(String FileName);
__fastcall TForm1(TComponent * Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#include <Inifiles.hpp>
#include "tlhelp32.hpp"
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool ShowHistory = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
{
}
int __fastcall WatchMointor(char ppExeName[], int kill)
{
String ProcessName;
int ProcessSize;
bool ContinueLoop;
HANDLE FSnapshotHandle;
TProcessEntry32 FProcessEntry32;
char tmpExeName[2048];
HANDLE pHand;
FSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize = sizeof(FProcessEntry32);
ContinueLoop = Process32First(FSnapshotHandle,&FProcessEntry32);
// 获取进程列表
while (ContinueLoop)
{
ProcessName = FProcessEntry32.szExeFile;
StrPCopy(tmpExeName, ProcessName);
if (stricmp(ppExeName, tmpExeName)==0)
{
if (kill == 1)
{
pHand = OpenProcess(1,false,FProcessEntry32.th32ProcessID);
if (pHand!=NULL)
TerminateProcess(pHand,-1);
CloseHandle(pHand);
pHand = NULL;
CloseHandle(FSnapshotHandle);
FSnapshotHandle=NULL;
}
return 0;
}
ContinueLoop = Process32Next(FSnapshotHandle,&FProcessEntry32);
}
try {
CloseHandle(pHand);
pHand = NULL;
CloseHandle(FSnapshotHandle);
FSnapshotHandle=NULL;
}
catch(...)
{
}
return -1;
}
String __fastcall TForm1::GetFileVersion(String FileName)
{
int iVerInfoSize;
char *pBuf;
AnsiString asVer = "";
VS_FIXEDFILEINFO *pVsInfo;
unsigned int iFileInfoSize = sizeof(VS_FIXEDFILEINFO);
iVerInfoSize = GetFileVersionInfoSize(FileName.c_str(), NULL);
if(iVerInfoSize != 0)
{
pBuf = new char[iVerInfoSize];
if(GetFileVersionInfo(FileName.c_str(), 0, iVerInfoSize, pBuf))
{
if(VerQueryValue(pBuf, "\\", (void **)&pVsInfo, &iFileInfoSize))
{
asVer = IntToStr(HIWORD(pVsInfo->dwFileVersionMS)) + ".";
asVer += IntToStr(LOWORD(pVsInfo->dwFileVersionMS)) + ".";
asVer += IntToStr(HIWORD(pVsInfo->dwFileVersionLS)) + ".";
asVer += IntToStr(LOWORD(pVsInfo->dwFileVersionLS));
}
}
delete[]pBuf;
}
return asVer;
}
bool LeftIsNewVer(String left, String right)
{
TStringList *lstleft = new TStringList();
TStringList *lstright = new TStringList();
lstleft->Delimiter = '.';
lstright->Delimiter = '.';
lstleft->DelimitedText = left;
lstright->DelimitedText =right;
if(left == right)
{
return false;
}
if(left == "0.0.0.0")
{
return false;
}
if(StrToInt(lstleft->Strings[0]) < StrToInt(lstright->Strings[0]))
{
return false;
}
else if(StrToInt(lstleft->Strings[0]) > StrToInt(lstright->Strings[0]))
{
return true;
}
if(StrToInt(lstleft->Strings[1]) < StrToInt(lstright->Strings[1]))
{
return false;
}
else if(StrToInt(lstleft->Strings[1]) > StrToInt(lstright->Strings[1]))
{
return true;
}
if(StrToInt(lstleft->Strings[2]) < StrToInt(lstright->Strings[2]))
{
return false;
}
else if(StrToInt(lstleft->Strings[2]) > StrToInt(lstright->Strings[2]))
{
return true;
}
if(StrToInt(lstleft->Strings[3]) < StrToInt(lstright->Strings[3]))
{
return false;
}
else if(StrToInt(lstleft->Strings[3]) > StrToInt(lstright->Strings[3]))
{
return true;
}
return false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject * Sender)
{
if(!FileExists(ExtractFilePath(Application->ExeName) + "updater.cfg"))
{
Application->Terminate();
}
TIniFile *ini = new TIniFile(ExtractFilePath(Application->ExeName) + "updater.cfg");
TMemoryStream *pms = new TMemoryStream();
String remoteverurl, remoteverfile, localver, remotever;
remoteverurl = ini->ReadString("exe", "iniurl", "");
remoteverfile = ExtractFilePath(Application->ExeName) + "remotever.ini";
if(remoteverfile == "")
{
Application->Terminate();
}
try
{
IdHTTP1->Get(remoteverurl, pms);
// TFileStream *fs = new TFileStream(remoteverfile, fmCreate);
try
{
pms->Position = 0;
// fs->CopyFrom(pms, pms->Size);
// delete fs;
pms->SaveToFile(remoteverfile);
}
catch(...)
{
Application->Terminate();
}
}
catch(Exception & e)
{
Application->Terminate();
}
delete pms;
TIniFile *ini2 = new TIniFile(remoteverfile);
remotever = ini2->ReadString("ver", "version", "0.0.0.0");
localver = ini->ReadString("ver", "version", "0.0.0.0");
if(LeftIsNewVer(remotever, localver) && ShowHistory == false)
{
Button1->Click();
}
if(LeftIsNewVer(remotever, localver) ==false)
{
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject * Sender)
{
if(ParamStr(1).LowerCase() == "showhistory")
{
ShowHistory = true;
Button4->Click();
}
else
{
Button3->Click();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TIniFile *ini = new TIniFile(ExtractFilePath(Application->ExeName) + "updater.cfg");
String exename = ExtractFileName(ini->ReadString("exe","Path","")) ;
WatchMointor(exename.c_str(),1);
TMemoryStream *pms = new TMemoryStream();
String remotebinurl, remotebinfile;
remotebinurl = ini->ReadString("exe", "url", "");
remotebinfile = ini->ReadString("exe","Path","") ;
if(remotebinfile == "")
{
Application->Terminate();
}
try
{
IdHTTP1->Get(remotebinurl, pms);
// TFileStream *fs = new TFileStream(remoteverfile, fmCreate);
try
{
pms->Position = 0;
// fs->CopyFrom(pms, pms->Size);
// delete fs;
pms->SaveToFile(remotebinfile);
}
catch(...)
{
Application->Terminate();
}
}
catch(Exception & e)
{
Application->Terminate();
}
delete pms;
ShellExecute(this, "open", remotebinfile.c_str(), "", "", 0);
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
TIniFile *ini = new TIniFile(ExtractFilePath(Application->ExeName) + "updater.cfg");
TMemoryStream *pms = new TMemoryStream();
String remotehistoryurl , historyfile ;
remotehistoryurl = ini->ReadString("exe", "historyurl", "");
historyfile = ExtractFilePath(Application->ExeName) + "history.txt";
try
{
IdHTTP1->Get(remotehistoryurl, pms);
try
{
pms->Position = 0;
pms->SaveToFile(historyfile);
Memo1->Lines->LoadFromFile(historyfile);
}
catch(...)
{
}
}
catch(Exception & e)
{
}
delete pms;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------