1.最简单最有效实现一个程序一次开机只运行一个实例,用于限制版或注册版;稍加修改可知是否已有本程序的实例在运行:
//在窗体的OnShow事件中书写如下代码:
Const
AtomStr = "OnlyOnce";
ReMind = "此程序版本一次开机只能运行一次,请注册后再使用!";
var
atom : integer;
begin
if GlobalFindAtom(AtomStr) = 0 then
atom := GlobalAddAtom(AtomStr)
else
begin
Application.MessageBox(ReMind, "提示", Mb_Ok + Mb_IconInformation);
Close;
end;
end;
2. 一个字符串加密解密程序(此程序在程序员98和2000光盘中多次出现,但都有错误,以下为修改
稿)
function TransChar(AChar: Char): Integer;
begin
if AChar in ["0".."9"] then
Result := Ord(AChar) - Ord("0")
else
Result := 10 + Ord(AChar) - Ord("A");
end;
function StrToHex(AStr: string): string;
var
I : Integer;
Tmp: string;
begin
Result := "";
For I := 1 to Length(AStr) do
begin
Result := Result + Format("%2x", [Byte(AStr[I])]);
end;
I := Pos(" ", Result);
While I $#@60;$#@62; 0 do
begin
Result[I] := "0";
I := Pos(" ", Result);
end;
end;
function HexToStr(AStr: string): string;
var
I : Integer;
CharValue: word;
begin
Result := "";
For I := 1 to Trunc(Length(Astr)/2) do
begin
Result := Result + " ";
CharValue := TransChar(AStr[2*I-1])*16 + TransChar(AStr[2*I]);
Result[I] := Char(CharValue);
end;
end;
function Encrypt(const S: String; Key: word): String;
var
I : Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
if Result[I] = Chr(0) then
Result[I] := S[I];
end;
Result := StrToHex(Result);
end;
function Decrypt(const S: String; Key: word): String;
var
I: Integer;
S1: string;
begin
S1 := HexToStr(S);
Result := S1;
for I := 1 to Length(S1) do
begin
if char(byte(S1[I]) xor (Key shr 8)) = Chr(0) then
begin
Result[I] := S1[I];
Key := (byte(Chr(0)) + Key) * C1 + C2; //保证Key的正确性 end
else
begin
Result[I] := char(byte(S1[I]) xor (Key shr 8));
Key := (byte(S1[I]) + Key) * C1 + C2;
end;
end;
end;

BK网络学院主要内容:平面设计教程,网站开发在线教程,网页制作教程,服务器教程,网络编程,数据库教程等。