几种程序自杀的方法

procedure DeleteMe;
var
  BatchFile: TextFile;
  BatchFileName: string;
  ProcessInfo: TProcessInformation;
  StartUpInfo: TStartupInfo;
begin
  BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';
  AssignFile(BatchFile, BatchFileName);
  Rewrite(BatchFile);

  Writeln(BatchFile, ':try');
  Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
  Writeln(BatchFile,
    'if exist "' + ParamStr(0) + '"' + ' goto try');
  Writeln(BatchFile, 'del %0');
  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;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteMe;
  close;
end;

end.

第二种:

类 别:系统控制
  我们经常遇到这样的软件,运行之后就消失的无影无踪,特别是一些黑客的木马工具。
  如果我们能掌握这个技术,即使不做黑客工具,也可以在程序加密、软件卸载等方面发挥作用。
  那么他们是怎样实现的呢?
---- 以delphi为例,在form关闭的时候执行以下函数closeme即可:
procedure TForm1.closeme;
var f:textfile;
begin
assignfile(f,'./delme.bat');
rewrite(f);
writeln(f,'@echo off');
writeln(f,':loop');
writeln(f,'del "'+application.ExeName+'"');
writeln(f,'if exist ./file.exe goto loop');
writeln(f,'del ./delme.bat');
closefile(f);
winexec('./delme.bat', SW_HIDE);
close;
end;

winexec(pchar('command.com /c del '+ParamStr(0)),SW_MINIMIZE);//最小化执行删除操作,否则将看到DOS窗口的瞬间闪烁

第三种:

Delphi 版
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, ShlObj;

type
  TForm1 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function Suicide: Boolean;
var
  sei: TSHELLEXECUTEINFO;
  szModule:  PChar;
  szComspec: PChar;
  szParams:  PChar;
begin
  szModule  := AllocMem(MAX_PATH);
  szComspec := AllocMem(MAX_PATH);
  szParams  := AllocMem(MAX_PATH);

  // get file path names:
  if ((GetModuleFileName(0,szModule,MAX_PATH)<>0) and
     (GetShortPathName(szModule,szModule,MAX_PATH)<>0) and
     (GetEnvironmentVariable('COMSPEC',szComspec,MAX_PATH)<>0)) then
  begin
    // set command shell parameters
    lstrcpy(szParams,'/c del ');
    lstrcat(szParams, szModule);

    // set struct members
    sei.cbSize       := sizeof(sei);
    sei.Wnd          := 0;
    sei.lpVerb       := 'Open';
    sei.lpFile       := szComspec;
    sei.lpParameters := szParams;
    sei.lpDirectory  := 0;
    sei.nShow        := SW_HIDE;
    sei.fMask        := SEE_MASK_NOCLOSEPROCESS;

    // invoke command shell
    if (ShellExecuteEx(@sei)) then
    begin
      // suppress command shell process until program exits
      SetPriorityClass(sei.hProcess,HIGH_PRIORITY_CLASS);//IDLE_PRIORITY_CLASS);

      SetPriorityClass( GetCurrentProcess(),
                        REALTIME_PRIORITY_CLASS);

      SetThreadPriority( GetCurrentThread(),
                         THREAD_PRIORITY_TIME_CRITICAL);

      // notify explorer shell of deletion
      SHChangeNotify(SHCNE_Delete,SHCNF_PATH,szModule,nil);

      Result := True;
    end
    else
      Result := False;
  end
  else
    Result := False;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Suicide;
end;

第四种:

procedure deleteSelf;
var hModule: THandle;
szModuleName: array[0..MAX_PATH] of char;
hKrnl32: THandle;
pExitProcess, pdeleteFile, pFreeLibrary, pUnmapViewOfFile: pointer;
ExitCode: UINT;
begin
hModule := GetModuleHandle(nil);
GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));

hKrnl32 := GetModuleHandle('kernel32');
pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');
pdeleteFile := GetProcAddress(hKrnl32, 'deleteFileA');
pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');
pUnmapViewOfFile := GetProcAddress(hKrnl32, 'UnmapViewOfFile');
ExitCode := system.ExitCode;
if ($80000000 and GetVersion()) <> 0 then
// Win95, 98, Me
asm
lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pdeleteFile
push pFreeLibrary
ret
end
else
begin
CloseHandle(THANDLE(4));
asm
lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pdeleteFile
push pUnmapViewOfFile
ret
end
end
end;
 

以下是一个简单的围棋双人对战程序,包含吃子、计算外气、禁手规则以及自动提子的代码。这个程序使用Code::Blocks和C语言编写。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define BOARD_SIZE 9 #define EMPTY 0 #define BLACK 1 #define WHITE 2 int board[BOARD_SIZE][BOARD_SIZE]; int current_player = BLACK; bool is_legal_move(int x, int y) { if (board[x][y] != EMPTY) { return false; } // 判断是否为自杀 board[x][y] = current_player; bool has_liberty = false; if (x > 0 && board[x-1][y] != EMPTY) { has_liberty = true; } if (y > 0 && board[x][y-1] != EMPTY) { has_liberty = true; } if (x < BOARD_SIZE-1 && board[x+1][y] != EMPTY) { has_liberty = true; } if (y < BOARD_SIZE-1 && board[x][y+1] != EMPTY) { has_liberty = true; } if (!has_liberty) { board[x][y] = EMPTY; return false; } // 判断是否为禁手 if (x > 1 && board[x-1][y] == current_player && board[x-2][y] == current_player) { board[x][y] = EMPTY; return false; } if (y > 1 && board[x][y-1] == current_player && board[x][y-2] == current_player) { board[x][y] = EMPTY; return false; } if (x < BOARD_SIZE-2 && board[x+1][y] == current_player && board[x+2][y] == current_player) { board[x][y] = EMPTY; return false; } if (y < BOARD_SIZE-2 && board[x][y+1] == current_player && board[x][y+2] == current_player) { board[x][y] = EMPTY; return false; } // 判断是否吃子 if (x > 0 && board[x-1][y] == 3-current_player) { bool has_liberty = false; if (x > 1 && board[x-2][y] != EMPTY) { has_liberty = true; } if (y > 0 && board[x-1][y-1] != EMPTY) { has_liberty = true; } if (y < BOARD_SIZE-1 && board[x-1][y+1] != EMPTY) { has_liberty = true; } if (!has_liberty) { board[x][y] = EMPTY; board[x-1][y] = EMPTY; return true; } } if (y > 0 && board[x][y-1] == 3-current_player) { bool has_liberty = false; if (x > 0 && board[x-1][y-1] != EMPTY) { has_liberty = true; } if (x < BOARD_SIZE-1 && board[x+1][y-1] != EMPTY) { has_liberty = true; } if (y > 1 && board[x][y-2] != EMPTY) { has_liberty = true; } if (!has_liberty) { board[x][y] = EMPTY; board[x][y-1] = EMPTY; return true; } } if (x < BOARD_SIZE-1 && board[x+1][y] == 3-current_player) { bool has_liberty = false; if (x < BOARD_SIZE-2 && board[x+2][y] != EMPTY) { has_liberty = true; } if (y > 0 && board[x+1][y-1] != EMPTY) { has_liberty = true; } if (y < BOARD_SIZE-1 && board[x+1][y+1] != EMPTY) { has_liberty = true; } if (!has_liberty) { board[x][y] = EMPTY; board[x+1][y] = EMPTY; return true; } } if (y < BOARD_SIZE-1 && board[x][y+1] == 3-current_player) { bool has_liberty = false; if (x > 0 && board[x-1][y+1] != EMPTY) { has_liberty = true; } if (x < BOARD_SIZE-1 && board[x+1][y+1] != EMPTY) { has_liberty = true; } if (y < BOARD_SIZE-2 && board[x][y+2] != EMPTY) { has_liberty = true; } if (!has_liberty) { board[x][y] = EMPTY; board[x][y+1] = EMPTY; return true; } } board[x][y] = EMPTY; return true; } int count_liberties(int x, int y) { if (board[x][y] == EMPTY) { return 0; } int liberties = 0; if (x > 0 && board[x-1][y] == EMPTY) { liberties++; } if (y > 0 && board[x][y-1] == EMPTY) { liberties++; } if (x < BOARD_SIZE-1 && board[x+1][y] == EMPTY) { liberties++; } if (y < BOARD_SIZE-1 && board[x][y+1] == EMPTY) { liberties++; } return liberties; } bool has_liberties(int x, int y) { if (x > 0 && board[x-1][y] == EMPTY) { return true; } if (y > 0 && board[x][y-1] == EMPTY) { return true; } if (x < BOARD_SIZE-1 && board[x+1][y] == EMPTY) { return true; } if (y < BOARD_SIZE-1 && board[x][y+1] == EMPTY) { return true; } return false; } void remove_group(int x, int y) { board[x][y] = EMPTY; if (x > 0 && board[x-1][y] == current_player) { remove_group(x-1, y); } if (y > 0 && board[x][y-1] == current_player) { remove_group(x, y-1); } if (x < BOARD_SIZE-1 && board[x+1][y] == current_player) { remove_group(x+1, y); } if (y < BOARD_SIZE-1 && board[x][y+1] == current_player) { remove_group(x, y+1); } } void remove_captured_stones(int x, int y) { if (x > 0 && board[x-1][y] == 3-current_player && !has_liberties(x-1, y)) { remove_group(x-1, y); } if (y > 0 && board[x][y-1] == 3-current_player && !has_liberties(x, y-1)) { remove_group(x, y-1); } if (x < BOARD_SIZE-1 && board[x+1][y] == 3-current_player && !has_liberties(x+1, y)) { remove_group(x+1, y); } if (y < BOARD_SIZE-1 && board[x][y+1] == 3-current_player && !has_liberties(x, y+1)) { remove_group(x, y+1); } } void play_move(int x, int y) { board[x][y] = current_player; remove_captured_stones(x, y); current_player = 3 - current_player; } void print_board() { printf(" "); for (int i = 0; i < BOARD_SIZE; i++) { printf("%c ", 'A'+i); } printf("\n"); for (int y = 0; y < BOARD_SIZE; y++) { printf("%d ", y+1); for (int x = 0; x < BOARD_SIZE; x++) { if (board[x][y] == EMPTY) { printf(". "); } else if (board[x][y] == BLACK) { printf("X "); } else { printf("O "); } } printf("%d\n", y+1); } printf(" "); for (int i = 0; i < BOARD_SIZE; i++) { printf("%c ", 'A'+i); } printf("\n"); } int main() { for (int x = 0; x < BOARD_SIZE; x++) { for (int y = 0; y < BOARD_SIZE; y++) { board[x][y] = EMPTY; } } print_board(); while (true) { int x, y; printf("%c's turn:\n", current_player == BLACK ? 'X' : 'O'); scanf("%c%d", &x, &y); getchar(); // 吃掉回车符 x -= 'A'; y--; if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) { printf("Invalid move!\n"); continue; } if (!is_legal_move(x, y)) { printf("Illegal move!\n"); continue; } play_move(x, y); print_board(); } return 0; } ``` 这个程序使用了一个二维数组来表示棋盘,其中0表示空,1表示黑子,2表示白子。程序使用了一个while循环,一直等待用户输入,然后调用is_legal_move函数来判断是否为合法的落子。该函数会检查:是否为自杀、是否为禁手、是否吃子。如果是合法的落子,程序会调用play_move函数来更新棋盘,然后打印出新的棋盘状态。 需要注意的是,目前这个程序只支持命令行输入,用户需要输入类似A1这样的坐标来落子。如果想要添加GUI,需要使用相应的图形库来实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值