Skip to content
Browse files

add uDownloadFiles.pas.

  • Loading branch information...
1 parent c3f1289 commit 3bd978afc366ef35a9adf13784c8e1dd1fc05f7f @sunliming committed
Showing with 82 additions and 25 deletions.
  1. +1 −1 JYUpdateService.cfg
  2. +6 −5 JYUpdateService.dof
  3. +4 −1 JYUpdateService.dpr
  4. BIN JYUpdateService.res
  5. +10 −3 Service.pas
  6. +1 −1 slmlog.pas
  7. +7 −0 uDownloadFiles.pas
  8. +3 −1 uGlobal.pas
  9. +50 −13 uUpdateService.pas
View
2 JYUpdateService.cfg
@@ -31,7 +31,7 @@
-M
-$M16384,1048576
-K$00400000
--E".\_output"
+-E"d:\jiaoyan\toll"
-N".\_temp"
-LE"c:\program files (x86)\borland\delphi6\Projects\Bpl"
-LN"c:\program files (x86)\borland\delphi6\Projects\Bpl"
View
11 JYUpdateService.dof
@@ -41,7 +41,7 @@ MaxStackSize=1048576
ImageBase=4194304
ExeDescription=
[Directories]
-OutputDir=.\_output
+OutputDir=d:\jiaoyan\toll
UnitOutputDir=.\_temp
PackageDLLOutputDir=
PackageDCPOutputDir=
@@ -66,7 +66,7 @@ AutoIncBuild=1
MajorVer=1
MinorVer=0
Release=0
-Build=35
+Build=71
Debug=0
PreRelease=0
Special=0
@@ -77,7 +77,7 @@ CodePage=936
[Version Info Keys]
CompanyName=
FileDescription=
-FileVersion=1.0.0.35
+FileVersion=1.0.0.71
InternalName=
LegalCopyright=
LegalTrademarks=
@@ -96,5 +96,6 @@ Item0=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;
Count=1
Item0=.\_temp
[HistoryLists\hlOutputDirectorry]
-Count=1
-Item0=.\_output
+Count=2
+Item0=d:\jiaoyan\toll
+Item1=.\_output
View
5 JYUpdateService.dpr
@@ -2,13 +2,16 @@ program JYUpdateService;
{$R *.res}
+{$message 'ɾ³ýËùÓÐMessageBox'}
+
uses
SysUtils,
Service in 'Service.pas',
slmlog in 'slmlog.pas',
ProgramVersion in 'ProgramVersion.pas',
uUpdateService in 'uUpdateService.pas',
- uGlobal in 'uGlobal.pas';
+ uGlobal in 'uGlobal.pas',
+ uDownloadFiles in 'uDownloadFiles.pas';
begin
Main;
View
BIN JYUpdateService.res
Binary file not shown.
View
13 Service.pas
@@ -7,7 +7,7 @@ procedure Main;
implementation
uses Windows, Messages, SysUtils, JwaWinSvc, JwaWinNT, slmlog,
- ProgramVersion, uGlobal;
+ ProgramVersion, uGlobal, uUpdateService;
const
SERVICE_NAME = 'JYAppUpdateService';
@@ -88,7 +88,7 @@ function install: boolean;
GetModuleFileName(0, szFilePath, MAX_PATH);
//创建服务
- hService := CreateService(hSCM, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szFilePath, nil, nil, '', nil, nil);
+ hService := CreateService(hSCM, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szFilePath, nil, nil, '', nil, nil);
if (hService = 0) then
begin
CloseServiceHandle(hSCM);
@@ -97,6 +97,12 @@ function install: boolean;
exit;
end;
+ //启动服务
+ if not StartService(hService, 0, nil) then
+ begin
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), 'Install(): Couldn''t Start service.');
+ end;
+
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
@@ -217,7 +223,8 @@ procedure ServiceMain(dwNumServicesArgs: DWORD; lpServiceArgVectors: LPSTR); std
SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), 'ServiceMain(): Started.');
while (status.dwCurrentState = SERVICE_RUNNING) do
begin
- Sleep(1000);
+ UpdateService('d:\jiaoyan\toll\JYUpdateService.exe.uTMP', SERVICE_NAME);
+ Sleep(5000);
end;
SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), 'ServiceMain(): Stop.');
View
2 slmlog.pas
@@ -23,7 +23,7 @@ function SaveToLogFile(const ALogFile, ALog : string; AFlag: byte = 0): boolean;
case AFlag of
1: begin
WriteLn(F, ''); //换行
- Write(F, Format('%s %s', [FormatDateTime('yyyy-MM-dd hh:mm:ss', now), ALog])); //一行的开始
+ Write(F, Format('%s %-16s %s', [FormatDateTime('yyyy-MM-dd hh:mm:ss.zzz', now), ChangeFileExt(ExtractFileName(GetModuleName(hInstance)), ''), ALog])); //一行的开始
end;
2: Write(F, Format(' %s', [ALog])); //一行的中间内容
3: WriteLn(F, Format(' %s', [ALog])); //一行的结束
View
7 uDownloadFiles.pas
@@ -0,0 +1,7 @@
+unit uDownloadFiles;
+
+interface
+
+implementation
+
+end.
View
4 uGlobal.pas
@@ -3,7 +3,9 @@
interface
const
- LOG_FILE = 'd:\jiaoyan\toll\AutoUpdateService_%s.log';
+ LOG_FILE = 'd:\jiaoyan\toll\AutoUpdateService_%s.log';
+ SERVER_IP_INI_FILE = 'd:\jiaoyan\toll\UpdateServerIP.ini';
+ UPDATE_BAT_FILE = 'UpdateAndRestartService.bat';
implementation
View
63 uUpdateService.pas
@@ -2,38 +2,75 @@
interface
+procedure UpdateService(ANewFile, AServiceName: string);
+
implementation
-uses JwaWinSvc, uGlobal;
+uses Windows, SysUtils, JwaWinSvc, uGlobal, slmlog;
-procedure UpdateService(AOldFile, ANewFile, AServiceName: string);
+procedure UpdateService(ANewFile, AServiceName: string);
var
F : TextFile;
hSCManager: THandle;
hService: THandle;
- qsc: TQueryServiceConfig;
+ buf: array[0..4096-1] of byte;
BytesNeeded: Cardinal;
AppExeName: string;
+ strBatFileLine: string;
begin
+ //检测文件是否存在
+ if not FileExists(ANewFile) then
+ begin
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('UpdateService(): File %s not exists.', [ANewFile]));
+ exit;
+ end;
+
//获取本服务信息
hSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
- if hSCManager = 0 then exit;
+ if hSCManager = 0 then
+ begin
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), 'UpdateService(): Can not Open SCManager.');
+ exit;
+ end;
hService := OpenService(hSCManager, PChar(AServiceName), SERVICE_ALL_ACCESS);
- if hService = 0 then exit;
- if not QueryServiceConfig(hService, @qsc, sizeof(qsc), BytesNeeded) then exit;
- AppExeName := qsc.lpBinaryPathName;
+ if hService = 0 then
+ begin
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), 'UpdateService(): Can not Open Service.');
+ exit;
+ end;
+ ZeroMemory(@buf[0], sizeof(buf));
+ if not QueryServiceConfig(hService, @buf[0], sizeof(buf), BytesNeeded) then
+ begin
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('UpdateService(): QueryServiceConfig error: %d', [GetLastError]));
+ exit;
+ end;
+ AppExeName := PQueryServiceConfig(@buf[0])^.lpBinaryPathName;
//准备批处理文件
- AssignFile(F, ExtractFilePath(AppExeName) + 'UpdateAndRestartService.bat');
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), '准备批处理文件...');
+ AssignFile(F, ExtractFilePath(AppExeName) + UPDATE_BAT_FILE);
ReWrite(F);
- WriteLn(F, 'net stop ' + ThisServiceDisplayName);
- WriteLn(F, 'del ' + AppExeName);
- WriteLn(F, 'ren ' + ANewFileName + ' ' + ExtractFileName(AppExeName));
- WriteLn(F, 'net start ' + ThisServiceDisplayName);
+ strBatFileLine := 'net stop ' + AServiceName;
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('| %s', [strBatFileLine]));
+ WriteLn(F, strBatFileLine);
+ strBatFileLine := Format('copy "%s" "%s" /y >> ' + LOG_FILE, [ANewFile, AppExeName, FormatDateTime('yyyy-mm', now)]);
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('| %s', [strBatFileLine]));
+ WriteLn(F, strBatFileLine);
+ strBatFileLine := Format('del "%s"', [ANewFile]);
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('| %s', [strBatFileLine]));
+ WriteLn(F, strBatFileLine);
+ strBatFileLine := 'net start ' + AServiceName;
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('| %s', [strBatFileLine]));
+ WriteLn(F, strBatFileLine);
+ strBatFileLine := Format('del "%s%s"', [ExtractFilePath(AppExeName), UPDATE_BAT_FILE]);
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), Format('| %s', [strBatFileLine]));
+ WriteLn(F, strBatFileLine);
CloseFile(F);
//执行批处理文件
- WinExec(PChar(ExtractFilePath(AppExeName) + 'UpdateAndRestartService.bat'), SW_HIDE);
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), '执行批处理文件...');
+ //WinExec(PChar(ExtractFilePath(AppExeName) + UPDATE_BAT_FILE + ' >> ' + Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)])), SW_HIDE);
+ WinExec(PChar(Format('"%s%s"', [ExtractFilePath(AppExeName), UPDATE_BAT_FILE])), SW_HIDE);
end;
end.

0 comments on commit 3bd978a

Please sign in to comment.
Something went wrong with that request. Please try again.