Internet - criar um programa com live update atualiza pela internet |
Top Previous Next |
|
// criar um programa com live update first of all ,u need create a HInternet when it is created as following
constructor TULiveUpdate.Create(AOwner: TComponent); var FINet: HInternet; // U need add WinINet to uses first begin inherited; FINet := InternetOpen('WinINet1', 0, nil, nil, 0); end;
then u need check if it is need update?
function TULiveUpdate.NeedUpdate(const FileName: string): Boolean; var LFileS, SFileS: Integer; LFileT, SfileT: string; begin // Link to ftp Server(u need declare FPath first) FftpHandle := InternetConnect(FINet, PChar(FPath), 0, '', '', Internet_Service_Ftp, 0, 255); if FftpHandle = nil then raise Exception.Create('Can''t link to ftp Server'+ FPath + '!');
// Compare FileSize LFileS := GetFileSizeA(FileName); SFileS := GetFileSizeW(ExtractFileName(FileName)); // Compare FileDate LFileT := GetFileDateTimeA(FileName); SFileT := GetFileDateTimeW(FileName); Result := (LFileS <> SFileS) or (LFileT <> SFileT); end;
last , u can LiveUpdate now, If file is run , u need close it and down it, then open it again
function TULiveUpdate.LiveUpdate(const FileName: string): Boolean; var FileIsRun: Boolean; hFile: THandle; begin FileIsRun := FileIsInUse(FileName); // u need set TargetFile first if not FtpGetFile(FFTPHandle, PChar(FileName), PChar(TargetFile), False, File_Attribute_Normal, Ftp_Transfer_Type_Binary, 0) then RaiselastOSError; if FileIsRun then begin // If file is run, delete it and open a new process to open the Lasted file DeleteMe(FileName); hFile := OpenProcess(1, False, FileProcessID); if SameText(FileName, ExtractFileName(ParamStr(0))) then ShellExecute(Application.Handle, '', PChar(FileName), nil, nil, 1); TerminateProcess(hFile, 0); end; end; |