程序自杀方式

     有点电脑常识的人都知道,在Windows下如果程序正在运行,那么是无法将其删除的,当然,也不是绝对不可以。CIH大家应该知道吧,它的核心有两个,一个就是取得Ring0级权限,然后就可以随便干想干的事(比如说修改正在运行的文件),而我们一般的程序是运行在Ring3级上的。顺便提一句---NT下没有Ring的概念,所以CIH对其无效。用Delphi内镶汇编也可以取得Ring0级权限,但如果我们的程序运行在NT或者Win2000下就没有效果了。在说句题外话,现在的编译器都很不错了,大多数程序员都编不出比编译器编译出的更理想的代码,象Delphi,如果将它的某些单元代码改用内镶汇编,在某些方面如字符串处理方面会提高5倍左右的效率,但NT不支持某些汇编代码,如果程序在NT下工作就会出错,怎么办?稳定第一!所以我们不用这个方法,而且,用这个方法有点杀鸡用牛刀的味道。
   用过DOS的朋友应该还记得批处理文件吧,新建一个批处理文件a.bat,编辑其内容为:del %0,然后运行它,怎么样?a.bat把自己删除掉了!!!好,我们就用它来进行程序的“自杀”!
找一个EXE可执行文件,比如说abc.exe,新建一个批处理文件a.bat,编辑其内容为:
:pp
del abc.exe
if exist abc.exe goto pp
del %0
先运行abc.exe,再运行a.bat,然后将abc.exe退出,你会发现a.exe和a.bat都没有了!!!按照这个思路,我们可以在程序中根据文件名称写一个批处理,将上面的abc.exe换成自己的EXE文件名就可以了。运行Delphi,新建一个工程,添加一个Button到窗体上,点击Button,写下如下代码:

procedure TForm1.Button1Click(Sender: TObject);
var Selfname,BatFilename,s1,s2:string;
BatchFile: TextFile;
begin
Selfname:=Extractfilename(application.exename);//取EXE文件自己的名称
BatFilename:=ExtractFilePath(Application.ExeName)+ 'a.bat';//批处理文件名称
S1:='@del '+Selfname;
S2:='if exist '+Selfname+' goto pp';
assignfile(BatchFile,BatFilename);
rewrite(BatchFile);
writeln(BatchFile,':pp');
writeln(BatchFile,S1);
writeln(BatchFile,S2);
writeln(BatchFile,'@del %0');
closefile(BatchFile);
winexec(pchar(BatFilename),sw_hide);//隐藏窗口运行a.bat
application.Terminate;//退出程序
end;
那我们的事情是不是就完了?NO!上面的程序原理是对的,但如果你的程序是运行在系统目录下如Windows目录下或者Windows/System等目录下,除非你打开那个目录看着它删除,否则根本没法卸掉的。那怎么办?别急,我们请出一个函数CreateProcess,它的原型为:
BOOL CreateProcess(
LPCTSTR lpApplicationName, // pointer to name of executable module 
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes 
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes 
BOOL bInheritHandles, // handle inheritance flag 
DWORD dwCreationFlags, // creation flags 
LPVOID lpEnvironment, // pointer to new environment block 
LPCTSTR lpCurrentDirectory, // pointer to current directory name 
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO 
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION 
);
这个函数和OpenProcess、ReadProcessMemory、WriteProcessMemory使用可以用来读取和修改内存数据,常用的游戏修改器就是用它。由于这些不是本文的重点所以这里不作详细介绍,感兴趣的读者可自行翻阅Delphi自带的帮助文件。用CreateProcess函数创建一个进程就可以完美的完成我们的“程序自杀”了。
运行Delphi,新建一个工程,添加一个Button到窗体上,全部代码如下:

unit Unit1;

interface

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
My_DeleteMe;
end;
procedure TForm1.My_DeleteMe; //程序自杀
//-----------------------------------------------------------
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)) + '$$a$$.bat';
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ':try');
Writeln(BatchFile, 'del "' + GetShortName(
该文章转载自1024k:http://www.1024k.cn/faq/2007/200706/39794.html 

以下是一个简单的围棋双人对战程序,包含吃子、计算外气、禁手规则以及自动提子的代码。这个程序使用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、付费专栏及课程。

余额充值