大家来看下,很有意思,(90分)

  • 主题发起人 主题发起人 梦中过客
  • 开始时间 开始时间

梦中过客

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下,这个窗体是在程序运行中建立,我想让它删除自身并复制自己到系统目录中启动,有窗体的能做到,但这个自己用api建立的窗体,我每回都做失败,请指教,<br>program paswin;<br><br>uses<br>&nbsp; Windows,Messages;<br><br>function WindowProc(Window:Hwnd; AMessage,Wparam,Lparam:Longint):Longint;stdcall;export;<br>begin<br>&nbsp; WindowProc:=0;<br>&nbsp; case AMessage of<br>&nbsp; &nbsp; wm_destroy:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; PostQuitMessage(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; WindowProc:=DefWindowProc(Window,AMessage,Wparam,Lparam);<br>end;<br><br>function WinRegister:Boolean;<br>var<br>&nbsp; wincls:TWndClass;<br>begin<br>&nbsp; wincls.style:=cs_hRedraw or cs_vRedraw;<br>&nbsp; wincls.lpfnWndProc:=@WindowProc;<br>&nbsp; wincls.cbClsExtra:=0;<br>&nbsp; wincls.cbWndExtra:=0;<br>&nbsp; wincls.hInstance:=Hinstance;<br>&nbsp; wincls.hIcon:=LoadIcon(0,IDI_APPLICATION);<br>&nbsp; wincls.hCursor:=LoadCursor(0,IDC_Arrow);<br>&nbsp; wincls.hbrBackground:=hBrush(Color_Window);<br>&nbsp; wincls.lpszMenuName:=nil;<br>&nbsp; wincls.lpszClassName:='BLANKWIN';<br>&nbsp; Result:=RegisterClass(Wincls)&lt;&gt;0;<br>end;<br><br>function WinCreate:Hwnd;<br>var<br>&nbsp; hWindow:HWnd;<br>begin<br>&nbsp; hWindow:=CreateWindow('BLANKWIN','A BLANK WINDOW',WS_OVERLAPPEDWINDOW,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,Hinstance,nil);<br>&nbsp; if hWindow&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ShowWindow(hWindow,Cmdshow);<br>&nbsp; &nbsp; &nbsp; UpdateWindow(hWindow);<br>&nbsp; &nbsp; end;<br>&nbsp; Result:=hWindow;<br>end;<br><br>var<br>&nbsp; AMessage:TMsg;<br>&nbsp; hwindow:HWnd;<br>begin<br>&nbsp; if not WinRegister then Exit;<br>&nbsp; hWindow:=winCreate;<br>&nbsp; if hWindow=0 then Exit;<br>&nbsp; while GetMessage(AMessage,0,0,0) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; TranslateMessage(AMessage);<br>&nbsp; &nbsp; &nbsp; DispatchMessage(AMessage);<br>&nbsp; &nbsp; end;<br>end.
 
var<br>&nbsp; sysdir:string[255];<br>begin<br>&nbsp; sysdir[0]:=GetSystemDirectory(pchar(sysdir),255); &nbsp; &nbsp; &nbsp;//系统目录<br>&nbsp; if sysdir[length(sysdir)]&lt;&gt;'/' then &nbsp; sysdir:=sysdir+'/';<br><br>&nbsp; if not SameText(ExtractFilePath(ParamStr(0)),sysdir) //不是执行自系统目录<br>&nbsp; begin<br>&nbsp; &nbsp; Try<br>&nbsp; &nbsp; &nbsp; CopyFile(Pchar(ParamStr(0)),Pchar(sysdir+Extractfilename(ParamStr(0)),false);//复制到系统目录<br>&nbsp; &nbsp; &nbsp; Winexec(sysdir+Extractfilename(ParamStr(0)),sw_hide); //自系统录下隐藏执行<br>&nbsp; &nbsp; Finally<br>&nbsp; &nbsp; &nbsp; halt;<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; //原来 主程序中的 begin end &nbsp;里面内容。。。<br>&nbsp; end;<br>end;<br><br>&nbsp; &nbsp; &nbsp;gei &nbsp; fen &nbsp; !! &nbsp; &nbsp;3ks &nbsp; !! &nbsp;:)
 
这样可以:<br>var<br>&nbsp; myMessage:TMsg;<br>&nbsp; hwindow:HWnd;<br>&nbsp; sysdir,filename:string[255];<br>&nbsp; //主程序<br>begin<br>&nbsp; filename:=Extractfilename(ParamStr(0)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得文件名<br>&nbsp; sysdir[0]:=char(GetSystemDirectory(Pchar(@sysdir[1]),255)); //取得系统目录<br>&nbsp; if sysdir[length(sysdir)]&lt;&gt;'/' then &nbsp; &nbsp;sysdir:=sysdir+'/'; &nbsp;//规范化<br><br>&nbsp; if not SameText(ExtractFilePath(ParamStr(0)),sysdir) then &nbsp; //不是执行自系统目录<br>&nbsp; begin<br>&nbsp; &nbsp; showmessage('嘿嘿');<br>&nbsp; &nbsp; Try<br>&nbsp; &nbsp; &nbsp; CopyFile( Pchar(ParamStr(0)) , Pchar( sysdir+filename), false) ;//复制到系统目录<br>&nbsp; &nbsp; &nbsp; Winexec(pchar(sysdir+filename),sw_hide); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//自系统录下隐藏执行<br>&nbsp; &nbsp; Finally<br>&nbsp; &nbsp; &nbsp; halt; &nbsp; &nbsp; &nbsp;//退出<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; if not WinRegister then Exit;<br>&nbsp; &nbsp; hWindow:=winCreate;<br>&nbsp; &nbsp; if hWindow=0 then Exit;<br>&nbsp; &nbsp; while GetMessage(myMessage,0,0,0) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; TranslateMessage(myMessage);<br>&nbsp; &nbsp; &nbsp; &nbsp; DispatchMessage(myMessage);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;
 
刘麻子,的做法应该可以。<br>你的先把自己不过去,然后再删除,删除时程序应该已经退出<br>就是调几个api函数吗!
 
删除自己,代码来自《Delphi下深入Windows核心编程》...... &nbsp;就是建立一个Dos批处理文件并且执行<br><br>procedure DeleteMe;<br>var<br>&nbsp; BatchFile: TextFile;<br>&nbsp; BatchFileName: string;<br>&nbsp; ProcessInfo: TProcessInformation;<br>&nbsp; StartUpInfo: TStartupInfo;<br>begin<br>&nbsp; BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';<br>&nbsp; AssignFile(BatchFile, BatchFileName);<br>&nbsp; Rewrite(BatchFile);<br><br>&nbsp; Writeln(BatchFile, ':try');<br>&nbsp; Writeln(BatchFile, 'del "' + ParamStr(0) + '"');<br>&nbsp; Writeln(BatchFile,<br>&nbsp; &nbsp; 'if exist "' + ParamStr(0) + '"' + ' goto try');<br>&nbsp; Writeln(BatchFile, 'del %0');<br>&nbsp; CloseFile(BatchFile);<br><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,<br>&nbsp; &nbsp; False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo,<br>&nbsp; &nbsp; ProcessInfo) then<br>&nbsp; begin<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hThread);<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hProcess);<br>&nbsp; end;<br>end;
 
谢谢刘兄,分是你的,
 
多人接受答案了。
 
后退
顶部