<br>program DelMe;<br><br>{$APPTYPE CONSOLE}<br><br>uses<br> Windows, SysUtils;<br>var //<br> szSrc, //<br> szDest : array[0..MAX_PATH] of char; //<br> szCmdLine : string;<br> hFile : THandle;<br> hProcess : THandle;<br> SI : TStartupInfo;<br> PI : TProcessInformation;<br> nTimes : DWord;<br>begin<br>if ParamCount = 0 then<br> begin<br> GetModuleFileName(0, szSrc, MAX_PATH);<br> GetTempPath(MAX_PATH, szDest);<br> GetTempFileName(szDest, 'Tmp', 0, szDest);<br> CopyFile(szSrc, szDest, FALSE); // 将当前可执行文件复制一个副本<br><br> hFile := CreateFile(szDest, // pointer to name of the file<br> 0, // access (read-write) mode<br> FILE_SHARE_READ, // share mode<br> nil, // pointer to security attributes<br> OPEN_EXISTING, // how to create<br> FILE_FLAG_DELETE_ON_CLOSE, // file attributes<br> 0); // handle to file with attributes to copy<br><br> hProcess := OpenProcess(SYNCHRONIZE, // access flag<br> TRUE, // handle inheritance flag<br> GetCurrentProcessId); // process identifier<br><br><br> szCmdLine := Format('%s %d "%s"', [szDest, hProcess, szSrc]); //格式化命令行参数<br><br> FillChar(SI, SizeOf(SI), 0);<br> SI.cb := SizeOf(SI);<br><br> CreateProcess(nil, // pointer to name of executable module<br> PChar(szCmdLine), // pointer to command line string<br> nil, // pointer to process security attributes<br> nil, // pointer to thread security attributes<br> TRUE, // handle inheritance flag<br> 0, // creation flags<br> nil, // pointer to new environment block<br> nil, // pointer to current directory name<br> SI, // pointer to STARTUPINFO<br> PI); // pointer to PROCESS_INFORMATION<br><br> CloseHandle(hProcess);<br> CloseHandle(hFile);<br> end<br>else<br> begin<br> hProcess := THANDLE(StrToInt(ParamStr(1)));<br> WaitForSingleObject(hProcess, INFINITE); //等待主进程结束<br> CloseHandle(hProcess);<br><br> hFile := FileOpen(ParamStr(2), fmOpenReadWrite);<br> if hFile > 0 then<br> begin<br> FileSeek(hFile, $80, 0);<br> FileRead(hFile, nTimes, SizeOf(nTimes));<br> Inc(nTimes);<br> FileSeek(hFile, $80, 0);<br> FileWrite(hFile, nTimes, SizeOf(nTimes));<br> FileClose(hFile);<br> Writeln('我已经运行'+IntToStr(nTimes)+'次了');<br> end;<br><br> end;<br>end.