在Aimgoo编写的CpuWHelper代码中,有一段程序如下:
function EnumFunc(Handle : HWnd; var PI64 : Int64Rec): Bool; stdcall;
var
_ClassName : array [0..255] of Char;
PID : Cardinal;
begin
Result := True;
PID := 0;
GetWindowThreadProcessId(Handle, @PID);
if PID = PI64.Lo then
begin
GetClassName(Handle, @_ClassName[0], 255);
if StrComp(_ClassName, pChar(PI64.Hi)) = 0 then
begin
PI64.Hi := Handle;
Result := False;
end;
end;
end;
function HwndByClassName(ClassName : String; PID : Integer = 0) : HWND;
var
PI64 : Int64Rec;
begin
if PID = 0 then
PI64.Lo := GetCurrentProcessID
else
PI64.Lo := PID;
PI64.Hi := Integer(pChar(ClassName));
EnumWindows(@EnumFunc, Integer(@PI64));
if PI64.Hi = Cardinal(pChar(ClassName)) then
Result := 0
else
Result := PI64.Hi;
end;
首先确定上面的代码是正确,且可以正常运行;其中红色标注部分另我十分迷惑,请求大家帮忙看看。
自己写了段相比较的代码
function HwndByClassName1(ClassName : String; PID : Integer = 0) : HWND;
var
PI64, PI642 : Int64Rec;
begin
if PID = 0 then
PI64.Lo := GetCurrentProcessID
else
PI64.Lo := PID;
PI64.Hi := Cardinal(pChar(ClassName));
PI642.Hi := Integer(pChar(ClassName));
EnumWindows(@EnumFunc, Integer(@PI64));
PI642.Hi := Cardinal(pChar(ClassName));
if PI642.Hi = Cardinal(pChar(ClassName)) then //PI64.Hi与PI642.Hi不相同;
Result := 0
else
Result := PI64.Hi;
end;
请问Cardinal(pChar(ClassName))的工作原理是什么; Author:
Passion Time: 2008-3-5 15:27
procedure TForm1.Button1Click(Sender: TObject);
var
vInt1: Int64Rec;
begin
if Check(Caption, vInt1) then
Showmessage('True')
else
ShowMessage('False');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
vInt1: Int64Rec;
begin
vInt1.Lo := Cardinal(PChar(Caption));
vInt1.Hi := Cardinal(PChar(Caption));
if vInt1.Lo = Cardinal(PChar(Caption)) then //P.Lo <> P.Hi <> Cardinal(PChar(Caption)))
Showmessage('True')
else
ShowMessage('False');
end;
procedure TForm1.Button3Click(Sender: TObject);
var
vInt1: Int64Rec;
begin
if Check1(Caption, vInt1) then
Showmessage('True')
else
ShowMessage('False');
end;
有三种情况出现了。(好象与字符的写复制有关)