Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Showing with 111 additions and 12 deletions.
  1. +1 −1 JYUpdateService.cfg
  2. +6 −5 JYUpdateService.dof
  3. +5 −1 JYUpdateService.dpr
  4. BIN JYUpdateService.res
  5. +10 −4 Service.pas
  6. +1 −1 slmlog.pas
  7. +7 −0 uDownloadFiles.pas
  8. +12 −0 uGlobal.pas
  9. +69 −0 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
6 JYUpdateService.dpr
@@ -2,12 +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';
+ uUpdateService in 'uUpdateService.pas',
+ uGlobal in 'uGlobal.pas',
+ uDownloadFiles in 'uDownloadFiles.pas';
begin
Main;
View
BIN JYUpdateService.res
Binary file not shown.
View
14 Service.pas
@@ -7,11 +7,10 @@ procedure Main;
implementation
uses Windows, Messages, SysUtils, JwaWinSvc, JwaWinNT, slmlog,
- ProgramVersion;
+ ProgramVersion, uGlobal, uUpdateService;
const
SERVICE_NAME = 'JYAppUpdateService';
- LOG_FILE = 'd:\jiaoyan\toll\AutoUpdateService_%s.log';
var
hServiceStatus: SERVICE_STATUS_HANDLE;
@@ -89,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);
@@ -98,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);
@@ -218,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
12 uGlobal.pas
@@ -0,0 +1,12 @@
+unit uGlobal;
+
+interface
+
+const
+ LOG_FILE = 'd:\jiaoyan\toll\AutoUpdateService_%s.log';
+ SERVER_IP_INI_FILE = 'd:\jiaoyan\toll\UpdateServerIP.ini';
+ UPDATE_BAT_FILE = 'UpdateAndRestartService.bat';
+
+implementation
+
+end.
View
69 uUpdateService.pas
@@ -2,6 +2,75 @@
interface
+procedure UpdateService(ANewFile, AServiceName: string);
+
implementation
+uses Windows, SysUtils, JwaWinSvc, uGlobal, slmlog;
+
+procedure UpdateService(ANewFile, AServiceName: string);
+var
+ F : TextFile;
+ hSCManager: THandle;
+ hService: THandle;
+ 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
+ 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
+ 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;
+
+ //准备批处理文件
+ SaveToLogFile(Format(LOG_FILE, [FormatDateTime('yyyy-mm', now)]), '准备批处理文件...');
+ AssignFile(F, ExtractFilePath(AppExeName) + UPDATE_BAT_FILE);
+ ReWrite(F);
+ 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);
+
+ //执行批处理文件
+ 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.

No commit comments for this range

Something went wrong with that request. Please try again.