bcb 做个模仿更新软件的问题

tongtong198808 2013-08-31 11:00:03
我做了一个简单的模拟更新软件,原理是:
1.软件A上传到数据库
2.软件A打开时候检查版本,如果低于最新的就打开“autoup”程序从数据库里面读取软件A1,然后关掉A,然后用A1替换掉A

——————————————————————
这个是更新其他软件的,现在想做个也能更新自己的软件,但是更新用到的替换要在程序关闭的时候才能替换,所以上面的方法没法做到这点,请问各位大侠,有没有好的方法。。。

就是更新软件,不光可以更新其他的,也可以更新自己的方法。
...全文
187 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Luisfan 2013-09-06
  • 打赏
  • 举报
回复
服务器上放一个xml文件,保存你的文件位置及版本号,然后通过本的的版本号对比,以决定是否需要升级,当然你也可以强制用户升级
chinayu2007 2013-09-06
  • 打赏
  • 举报
回复
在服务器上放一个下载列表文件,每次下载之前,先下载这个文件,读出文件内容,再根据其中的内容决定是否更新,可以把版本号放这个文件里
fbmsyu 2013-08-31
  • 打赏
  • 举报
回复
h文件

#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

fbmsyu 2013-08-31
  • 打赏
  • 举报
回复
代码自己调试。只支持更新本身的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();        
}
//---------------------------------------------------------------------------

cptang 2013-08-31
  • 打赏
  • 举报
回复
1、有一个更新组件,ccrun官网有下载,但是下载链接是否有效,就看你运气了 2、可以在本地目录下配置一个ini文件,用于存放版本记录,然后和服务器对比,有更新就下载。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧