⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.txt

📁 实现软件在线更新源码
💻 TXT
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include <urlmon.h>
#include "main.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma comment (lib, "urlmon.lib")
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
bool __fastcall TfrmMain::DownLoadFile(AnsiString URL,AnsiString Local)
{
  HRESULT hr;
  try
  {
  hr=URLDownloadToFile(0, URL.c_str(), Local.c_str(), 0, 0);
  // #include <urlmon.h>   #pragma comment (lib, "urlmon.lib") project->options->directorie/conditionals->libary path 加入$(BCB)\lib\PSDK
  }
  catch(EOleException &e)
  {
    MessageBox(Handle,"软件更新失败!请检查网络连接是否通畅!","警告",MB_OK|MB_ICONWARNING);
  }
 if(hr==S_OK)
   return true;
 else
  return false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormActivate(TObject *Sender)
{
  AnsiString URL,Local,newname,other;
  AnsiString ExePath = ExtractFileDir(Application->ExeName);
  URL= "http://ip/goods.exe";
  Local=ExePath +"\\goods.dat";
  newname=ExePath +"\\goods.exe";
  other=ExePath +"\\goods";

    if(FileExists(Local))
      DeleteFile(Local);
   if(FileExists(other))
      DeleteFile(other);

  HANDLE hWindow=NULL;
  hWindow= FindWindow(NULL,"设备资料管理系统(网络版)管理员端");
  if(hWindow == NULL)
  hWindow= FindWindow(NULL,"设备资料管理系统(网络版)用户端");
  if(hWindow)
  {
  Label1->Caption="软件升级中...请稍候!";
  Label1->Refresh();
  frmMain->Refresh();
  Sleep(5000);
  if(DownLoadFile(URL,Local))
   {
     SendMessage(hWindow, WM_CLOSE, 0, 0);

     if(FileExists(newname));
     DeleteFile(newname);
     if(rename(Local.c_str(),newname.c_str())==0)
     {
      MessageBox(Handle,"资料管理系统升级成功!","警告",MB_OK|MB_ICONWARNING);
      if(FileExists(newname));
      WinExec(newname.c_str(),SW_SHOWNORMAL);
      }
       if(FileExists(ExePath +"\\update.exe"))
     {
       URL= "http://info.ahau.edu.cn/goods/update.exe";
       Local=ExePath +"\\update.dat";
        DownLoadFile(URL,Local);
     }
   }
   else
   {
    Label1->Caption="升级失败!请检查网络是否连接!";
   }
  }
  else
  Label1->Caption="升级失败!请检查网络是否连接!";
  Application->Terminate();
}
//---------------------------------------------------------------------------



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -