var WinTempdir: string;
// 第一个过程是释放exe
//第2个过程是调用 第一个过程并执行释放出的exe
Procedure TNetUpdate.ExtractUpdateResource;
Var
reshandle: THandle;
hglobal: THandle;
ressize: dword;
ptr: pointer;
binfile: File Of byte;
Srcname: String;
buf: Array[0..MAX_PATH] Of Char;
Begin
// UPD 是以资源方式编译进去的exe 的资源名
ResHandle := FindResource(hinstance, 'UPD', PChar(RT_RCDATA));
hglobal := LoadResource(hinstance, reshandle);
Ressize := SizeOfResource(hinstance, reshandle);
ptr := LockResource(hglobal);
//取Windows Temp 目录
If (Length(WinTempDir) >= 1) And (WinTempDir[Length(WinTempDir)] <> '/') Then
WinTempDir := WinTempDir + '/';
//准备释放到temp 目录下的 upd.exe
SrcName := WinTempDir + 'upd.exe';
//写 upd.exe
AssignFile(binfile, SrcName);
Rewrite(binfile);
Blockwrite(binfile, ptr^, ResSize);
Closefile(binfile);
End;
//********************************************************
//********************************************************
//执行释放出来的 EXE
Procedure TNetUpdate.RestartProgram;
Var
proch: dword;
allow: Boolean;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
StartErrorCode: dword;
TmpBuf: Array[0..1024] Of char;
RestartParam : String;
Begin
Allow := True;
//调用上面的释放过程, 从资源文件 wupdate.res 中提取出 upd.exe 的压缩文件资源
ExtractUpdateResource;
//取得当前进程ID
proch := GetCurrentProcessID;
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOWNORMAL;
If Not CreateProcess(Nil,
pchar(WinTempdir + 'upd.exe '),
Nil,
Nil,
true,
CREATE_NEW_CONSOLE Or NORMAL_PRIORITY_CLASS, { creation flags }
Nil, { pointer to new environment block }
Nil, { pointer to current directory name, PChar}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) Then
Begin
StartErrorCode := GetLastError;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
Nil,
StartErrorCode,
0,
TmpBuf,
SizeOf(TmpBuf),
Nil);
End;
End;