Guest:
Register
|
Login
|
Member List
|
Search
|
Statistics
|
FAQ
Language
----------
Simplifed Chinese
Traditional Chinese
English
CnPack Forum
»
技术板块灌水区
» 求一问题
‹‹ Last Thread
|
Next Thread ››
Poll
Trade
Reward
Activity
Printable Version
|
Email to Friend
|
Subscription
|
Favorites
Subject: 求一问题
dwf3110
新警察
UID 37768
Digest Posts 0
Credits 8
Posts 3
点点分 8
Reading Access 10
Registered 2008-2-23
Status Offline
#1
Post at 2008-3-4 18:55
Profile
|
Blog
|
P.M.
求一问题
在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))的工作原理是什么;
Passion
(LiuXiao)
管理员
UID 359
Digest Posts
19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
#2
Post at 2008-3-5 15:27
Profile
|
Blog
|
P.M.
Cardinal(pChar(ClassName))是获得代表其地址的四字节整数吧。
dwf3110
新警察
UID 37768
Digest Posts 0
Credits 8
Posts 3
点点分 8
Reading Access 10
Registered 2008-2-23
Status Offline
#3
Post at 2008-3-5 18:40
Profile
|
Blog
|
P.M.
回复 #2 Passion 的帖子
从代码的表面上看好象是获得代表其地址的四字节整数;但是如下例子:
function Check( SS: String; P: Int64Rec) : Boolean;
begin
P.Lo := Cardinal(PChar(SS));
P.Hi := Cardinal(PChar(SS));
Result := (P.Lo = Cardinal(PChar(SS)))
// P.Lo = P.Hi = Cardinal(PChar(SS)))
end;
function Check1(SS: String; P: Int64Rec) : Boolean;
begin
P.Lo := Cardinal(PChar(SS));
ss := '1';
P.Hi := Cardinal(PChar(SS));
Result := (P.Lo = Cardinal(PChar(SS)))
//P.Lo <> P.Hi = Cardinal(PChar(SS)))
end;
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;
有三种情况出现了。
(
好象与字符的写复制有关)
[
本帖最后由 dwf3110 于 2008-3-5 18:42 编辑
]
Rainstorey
普通灌水员
菜鸟
UID 977
Digest Posts 0
Credits 82
Posts 39
点点分 82
Reading Access 10
Registered 2005-8-8
Location 苏州
Status Offline
#4
Post at 2008-3-6 09:54
Profile
|
Blog
|
P.M.
Cardinal(pChar(ClassName))
pChar(ClassName);//得到指向ClassName的Char *
Cardinal(char *);//得到指针的地址的Cardinal表示
http://www.RainCoding.com/blog
bahamut8348
灌水司司长
UID 4743
Digest Posts
14
Credits 337
Posts 79
点点分 337
Reading Access 10
Registered 2007-1-18
Status Offline
#5
Post at 2008-3-8 16:49
Profile
|
Blog
|
P.M.
var
s: string;
begin
s:= '123';
PAnsiChar(s); //把DELPHI字符串转换成WINDOWS字符串, 不管是DELPHI还是WINDOWS的字符串,其实都是指针,指针就是一个4BYTE的数据类型,在C里经常可以看到 (DWORD) ptr这样的语句,其实就是把指针类型转换成数值型,但是值并没有变
而Cardinal(PAnsiChar(s));和(DWORD)ptr这样是等价的,你可以下一个断点,然后用IDE跟踪值看看就知道了
做人要厚道,看帖要回贴
Poll
Trade
Reward
Activity
CnPack Forum
CnPack English Forum
> CnWizards IDE Wizards
> CVSTracNT
> Announcements & Others
All times are GMT++8, the time now is 2024-11-22 03:37
Powered by
Discuz!
5.0.0
© 2001-2006
Comsenz Inc.
Processed in 0.008502 second(s), 7 queries , Gzip enabled
TOP
Clear Cookies
-
Contact Us
-
CnPack Website
-
Archiver
-
WAP
Member's CP Home
Edit Profile
Credits Transaction
Public User Groups
Buddy List
Main
Page Views
User Agents
Posts History
Top Forums
Top Threads
Post Ranking
Credit Ranking
Online Time
Team
Moderation Stats