JohnPhan 发表于 2008-2-28 09:14:20

pb中如何应付指针类型 明显pb是留了一手

<p><font face="Courier New">在pb中使用WIN API和调用dll中的外部函数时,往往要用到指针。<br/>sybase公司pb对指针的说明文字很少,但可以看到在pb一些例子中,使用了这些技术。特别是有了pb的反编译工具后,能“阅读”到更多的pb程序代码,借鉴其中的方法。</font></p>
<p><font face="Courier New">以下是一些坊间流传的pb处理指针的方法。</font><font face="Courier New">有了这些方法,pb与vc之间的通道就更畅通了。</font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">1、pb中,用字符串地址得到字符串 </font></p>
<p><font face="Courier New">pb的函数String就可以实现,函数的语法为String ( handle, “Address” )<br/>handle作为地址参数,函数的返回值就是我们需要的字符串。官方文档中没有这个内容。明显的Sybase公司在pb上是留了一手或几手的。</font></p>
<p><br/><font face="Courier New">使用实例:<br/>function ulong&nbsp; CopyMem( ulong src, ref blob dest, ulong length ) library "kernel32" alias for "RtlMoveMemory"&nbsp;&nbsp; <br/>Function long&nbsp;&nbsp; LocalAlloc(long Flags, long Bytes) library "kernel32.dll"&nbsp;&nbsp; <br/>Function long&nbsp;&nbsp; LocalFree(long&nbsp; MemHandle) library&nbsp; "kernel32.dll" </font></p>
<p><font face="Courier New">string str_test_string&nbsp; <br/>long&nbsp;&nbsp; l_str_len<br/>long&nbsp;&nbsp; l_str_handle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>str_test_string = 'this is a test string'&nbsp;&nbsp; <br/>l_str_len&nbsp;&nbsp;&nbsp; = len(str_test_string) + 1&nbsp;&nbsp; <br/>l_str_handle = LocalAlloc(0, l_str_len&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp; <br/>CopyMem(l_str_handle,str_test_string,l_str_len)<br/>messagebox('raw string', str_test_string ) <br/>messagebox('handle string',string(l_str_handle,"address"))&nbsp;&nbsp; <br/>LocalFree(l_str_handle)</font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">2、得到pb中某个字符串变量的地址 </font></p>
<p><font face="Courier New">用Win Api函数:Function long lstrcpy(ref string Destination, ref string Source) library "kernel32.dll" <br/>函数原型: <br/>The lstrcpy function copies a string to a buffer. <br/>LPTSTR lstrcpy(<br/>LPTSTR lpString1, // address of buffer <br/>LPCTSTR lpString2 // address of string to copy <br/>); <br/>Return Values:If the function succeeds, the return value is a pointer to the buffer. </font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">使用实例:<br/>String lstr_dst <br/>string lstr_src<br/>long ll_address<br/>ls_src= "will get my addr"<br/>ls_dst =space(255) <br/>ll_address = lstrcpy(lstr_dst,lstr_src) </font></p>
<p><br/><font face="Courier New">3、在内存堆上分配空间,存储数据,读出数据并释放内存</font></p>
<p><font face="Courier New">要用到的api函数:<br/>LocalAlloc 用来申请内存块<br/>LocalFree&nbsp; 用来释放内存块<br/>CopyMemory 用来复制内存块</font></p>
<p><font face="Courier New">CopyMemory函数,有三个参数<br/>PVOID Destination, // address of move destination <br/>CONST VOID *Source, // address of block to move <br/>DWORD Length // size, in bytes, of block to move <br/>前两个参数均是指针类型,因此我们可以根据需要在声明中将其定义为long 和ref ***的形式</font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">使用实例: <br/>在内存中写入下面的结构,并获得其指针<br/>结构menuitemdata 如下:<br/>type menuitemdata from structure<br/>&nbsp;&nbsp; unsignedlong hmenu<br/>&nbsp;&nbsp; integer level<br/>end type </font></p>
<p><font face="Courier New">Function long LocalAlloc(long Flags, long Bytes) library "kernel32.dll"<br/>Function long LocalFree(long MemHandle) library "kernel32.dll"<br/>SUBROUTINE CopyMemory(long pDesc, ref menuitemdata pSrc,ulong size) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory" <br/>SUBROUTINE CopyMemory(ref menuitemdata pDesc, long pSrc,ulong size) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory" </font></p>
<p><font face="Courier New">long il_menuDataPointer //内存的指针<br/>menuitemdata lpmenuitemdata </font></p>
<p><font face="Courier New">//下面代码将lpmenuitemdata 的内容写入到内存中<br/>//用il_menuDataPointer做内存的指针<br/>lpmenuitemdata.hmenu = 88<br/>lpmenuitemdata.level = 1<br/>il_menuDataPointer= LocalAlloc(0,6) //分配内存 6=sizeof(menuitemdata)<br/>//写入数据<br/>CopyMemory(il_menuDataPointer,lpmenuitemdata,6) </font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">//从内存块中取出数据<br/>CopyMemory(lpmenuitemdata, il_menuDataPointer,6)</font></p>
<p><font face="Courier New"></font>&nbsp;</p>
<p><font face="Courier New">//释放 il_menuDataPointer指示的内存块<br/>LocalFree(il_menuDataPointer)</font></p>
[此贴子已经被作者于2008-2-28 13:55:18编辑过]

JohnPhan 发表于 2008-2-28 09:25:11

在“阅读”别人pb程序时,学到的“副产品”。
[此贴子已经被作者于2008-2-28 13:56:32编辑过]

ribut922 发表于 2008-3-3 08:39:55

<p>呵呵</p>
<p>写得很好</p>
<p>&nbsp;</p>
<p>学习了</p>

aladdin 发表于 2008-3-19 15:43:27

<p>学习了,对PB的认识又增长了</p>
页: [1]
查看完整版本: pb中如何应付指针类型 明显pb是留了一手

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com