pb中如何应付指针类型

来源:互联网 发布:excel删除列 mac 编辑:程序博客网 时间:2024/05/16 11:19

在pb中使用WIN API和调用dll中的外部函数时,往往要用到指针。
sybase公司pb对指针的说明文字很少,但可以看到在pb一些例子中,使用了这些技术。特别是有了pb的反编译工具后,能“阅读”到更多的pb程序代码,借鉴其中的方法。

以下是一些坊间流传的pb处理指针的方法。有了这些方法,pb与vc之间的通道就更畅通了。

 

1、pb中,用字符串地址得到字符串

pb的函数String就可以实现,函数的语法为String ( handle, “Address” )
handle作为地址参数,函数的返回值就是我们需要的字符串。官方文档中没有这个内容。明显的Sybase公司在pb上是留了一手或几手的。


使用实例:
function ulong  CopyMem( ulong src, ref blob dest, ulong length ) library "kernel32" alias for "RtlMoveMemory"  
Function long   LocalAlloc(long Flags, long Bytes) library "kernel32.dll"  
Function long   LocalFree(long  MemHandle) library  "kernel32.dll"

string str_test_string 
long   l_str_len
long   l_str_handle     
str_test_string = 'this is a test string'  
l_str_len    = len(str_test_string) + 1  
l_str_handle = LocalAlloc(0, l_str_len  )    
CopyMem(l_str_handle,str_test_string,l_str_len)
messagebox('raw string', str_test_string )
messagebox('handle string',string(l_str_handle,"address"))  
LocalFree(l_str_handle)

 

2、得到pb中某个字符串变量的地址

用Win Api函数:Function long lstrcpy(ref string Destination, ref string Source) library "kernel32.dll"
函数原型:
The lstrcpy function copies a string to a buffer.
LPTSTR lstrcpy(
LPTSTR lpString1, // address of buffer
LPCTSTR lpString2 // address of string to copy
);
Return Values:If the function succeeds, the return value is a pointer to the buffer.

 

使用实例:
String lstr_dst
string lstr_src
long ll_address
ls_src= "will get my addr"
ls_dst =space(255)
ll_address = lstrcpy(lstr_dst,lstr_src)


3、在内存堆上分配空间,存储数据,读出数据并释放内存

要用到的api函数:
LocalAlloc 用来申请内存块
LocalFree  用来释放内存块
CopyMemory 用来复制内存块

CopyMemory函数,有三个参数
PVOID Destination, // address of move destination
CONST VOID *Source, // address of block to move
DWORD Length // size, in bytes, of block to move
前两个参数均是指针类型,因此我们可以根据需要在声明中将其定义为long 和ref ***的形式

 

使用实例:
在内存中写入下面的结构,并获得其指针
结构menuitemdata 如下:
type menuitemdata from structure
   unsignedlong hmenu
   integer level
end type

Function long LocalAlloc(long Flags, long Bytes) library "kernel32.dll"
Function long LocalFree(long MemHandle) library "kernel32.dll"
SUBROUTINE CopyMemory(long pDesc, ref menuitemdata pSrc,ulong size) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
SUBROUTINE CopyMemory(ref menuitemdata pDesc, long pSrc,ulong size) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"

long il_menuDataPointer //内存的指针
menuitemdata lpmenuitemdata

//下面代码将lpmenuitemdata 的内容写入到内存中
//用il_menuDataPointer做内存的指针
lpmenuitemdata.hmenu = 88
lpmenuitemdata.level = 1
il_menuDataPointer= LocalAlloc(0,6) //分配内存 6=sizeof(menuitemdata)
//写入数据
CopyMemory(il_menuDataPointer,lpmenuitemdata,6)

 

//从内存块中取出数据
CopyMemory(lpmenuitemdata, il_menuDataPointer,6)

 

//释放 il_menuDataPointer指示的内存块
LocalFree(il_menuDataPointer)

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gameslq/archive/2008/04/22/2314857.aspx