zoukankan      html  css  js  c++  java
  • Delphi实现程序自销毁(自杀)

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    StdCtrls;
    type
    TForm1 = class(TForm)
    Button1: TButton;
    procedure DeleteMe; //自定义程序自杀过程
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.DFM}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    DeleteMe;
    end;
    procedure TForm1.DeleteMe; //程序自杀

    //如果要轉載本文請注明出處,免的出現版權紛爭,我不喜歡看到那種轉載了我的作品卻不注明出處的人 Seven{See7di#Gmail.com}
    //-----------------------------------------------------------
    function GetShortName(sLongName: string): string; //转换长文件名
    var
    sShortName: string;
    nShortNameLen: integer;
    begin
    SetLength(sShortName, MAX_PATH);
    nShortNameLen := GetShortPathName(PChar(sLongName),
    PChar(sShortName), MAX_PATH - 1);
    if (0 = nShortNameLen) then
    begin
    // handle errors...
    end;
    SetLength(sShortName, nShortNameLen);
    Result := sShortName;
    end;
    //-------------------------------------------------
    var
    BatchFile: TextFile;
    BatchFileName: string;
    ProcessInfo: TProcessInformation;
    StartUpInfo: TStartupInfo;
    begin
    BatchFileName := ExtractFilePath(ParamStr(0)) + '$$Delme$$.bat';
    AssignFile(BatchFile, BatchFileName);
    Rewrite(BatchFile);
    Writeln(BatchFile, ':try');
    Writeln(BatchFile, 'del "' + GetShortName(ParamStr(0)) + '"');
    Writeln(BatchFile, 'if exist "' + GetShortName(ParamStr(0)) + '"' + ' goto try');
    Writeln(BatchFile, 'del %0');
    Writeln(BatchFile, 'cls');
    Writeln(BatchFile, 'exit');
    CloseFile(BatchFile);
    FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
    StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
    StartUpInfo.wShowWindow := SW_Hide;
    if CreateProcess(nil, PChar(BatchFileName), nil, nil,
    False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo,
    ProcessInfo) then
    begin
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);
    end;
    Application.Terminate;
    end;
    end.


  • 相关阅读:
    jira使用教程管理项目
    JIRA缺陷管理工具
    目前流行的缺陷管理工具
    delphi 控件dxLayoutControl详解
    Delphi CxGrid 用法详解说明
    Delphi 10.3MitovLabs VCL 控件包关于“E2225: Never-build package 'Mitov_Runtime.dpk' must be recompiled”的问题
    DELPHI的编译指令
    [Delphi] Delphi版本号对照
    DELPHI 2010 > Consider using 'CharInSet' function in 'SysUtils' unit.
    ORA-28000 账号被锁定的解决办法
  • 原文地址:https://www.cnblogs.com/see7di/p/2239914.html
Copyright © 2011-2022 走看看