如何将记录类型的一个字段作为var参数传入函数!(200分)

  • 主题发起人 主题发起人 lccc
  • 开始时间 开始时间
L

lccc

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是Jeffrey Richter所写改进版同步对象中,记录结构的定义:<br>&nbsp; &nbsp;typedef struct {<br>&nbsp; &nbsp; &nbsp; DWORD m_dwSpinCount;<br>&nbsp; &nbsp; &nbsp; long &nbsp;m_lLockCount;<br>&nbsp; &nbsp; &nbsp; DWORD m_dwThreadId;<br>&nbsp; &nbsp; &nbsp; long &nbsp;m_lRecurseCount;<br>&nbsp; &nbsp;} SHAREDINFO, *PSHAREDINFO;<br><br>&nbsp; 我改为Delphi版本:<br>&nbsp; SHAREDINFO=packed record<br>&nbsp; &nbsp; &nbsp;m_dwSpinCount:DWORD; &nbsp; &nbsp; &nbsp; //spin计数<br>&nbsp; &nbsp; &nbsp;m_lLockCount:Integer; &nbsp; &nbsp; &nbsp;//锁定计数<br>&nbsp; &nbsp; &nbsp;m_dwThreadId:DWORD; &nbsp; &nbsp; &nbsp; &nbsp;//拥有该临界段的线程ID<br>&nbsp; &nbsp; &nbsp;m_lRecurseCount:Integer; &nbsp; //本线程拥有该临界段的计数<br>&nbsp; &nbsp;end;<br>&nbsp; TSharedInformation=SHAREDINFO;<br>&nbsp; PSharedInformation=^TSharedInformation;<br><br>&nbsp; Jeffrey Richter将记录的一个字段作为了var参数传入函数InterlockedExchange中:<br>&nbsp; InterlockedExchange((PLONG) &amp;m_pSharedInfo-&gt;m_dwSpinCount, dwSpinCount);<br>&nbsp; (这里将“(PLONG) &amp;m_pSharedInfo-&gt;m_dwSpinCount”作为了var参数传入)<br>&nbsp; 但是,改为Delphi语法时,不能将记录的一个字段作为var参数传入,如何修改这个函数为Delphi格式?
 
InterlockedExchange(Integer(m_pSharedInfo.m_dwSpinCount), dwSpinCount);
 
学习..学习..
 
use PSharedInformation, pls
 
问题解决:<br>var<br>&nbsp; pInt:Pointer;//声明一个Pointer。但实际指向Integer的指针,<br>begin<br>&nbsp; pInt:=m_pSharedInfo;//m_pSharedInfo为指针类型,故直接赋值;<br>&nbsp; //现在pInt指向了域m_dwSpinCount的地址<br>&nbsp; Inc(Integer(pInt),4);//指向的地址+4,现在pInt指向了m_pSharedInfo的<br>&nbsp; //域m_lLockCount的地址。<br>&nbsp; InterlockedExchange(Integer(pInt^), dwSpinCount); <br>end;
 
后退
顶部