Cardinal(pChar(ClassName)) 的工作原理问题

dwf3110 2008-03-05 01:19:15
在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))的工作原理是什么;



...全文
53 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dwf3110 2008-03-09
  • 打赏
  • 举报
回复
可以看看这个地方:
http://bbs.cnpack.org/viewthread.php?tid=1951&extra=page%3D1
ERR0RC0DE 2008-03-05
  • 打赏
  • 举报
回复
强制转换

我说下我的理解:

var
S: string;
P: PChar;
假设:S地址为:$1000, P地址:$2000
S := 'abc';
假设:S指向内存数据'abc'地址是:$8000

则:
P := PChar(S);
P地址不变,P指向内容为$8000
Cardinal(P)=》$8000

$8000为一个32bit数字,也是字符串'abc'的起始地址。

这样说不知有没错误。楼下指正。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧