bcb 做个模仿更新软件的问题
我做了一个简单的模拟更新软件,原理是:
1.软件A上传到数据库
2.软件A打开时候检查版本,如果低于最新的就打开“autoup”程序从数据库里面读取软件A1,然后关掉A,然后用A1替换掉A
——————————————————————
这个是更新其他软件的,现在想做个也能更新自己的软件,但是更新用到的替换要在程序关闭的时候才能替换,所以上面的方法没法做到这点,请问各位大侠,有没有好的方法。。。
就是更新软件,不光可以更新其他的,也可以更新自己的方法。
[解决办法]
代码自己调试。只支持更新本身的exe。
//---------------------------------------
#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();
}
//---------------------------------------