Powerbuilder中的内存操作大搜集

王朝mssql·作者佚名  2006-12-17
宽屏版  字体: |||超大  

Powerbuilder中的内存操作大搜集

Powerbuilder中的内存操作大搜集 我们知道pb中不支持指针,但我们在使用WIN32 API和调用一些dll中的外部函数时候,经常会与其打些交道,所以这里将相关的一些技巧收集整理起来。

1、根据字符串地址得到字符串

完全通过pb自带的函数String就可以实现,函数的语法为String ( data, { format } ),当我们将变量

地址作为Data参数,字符串“Address”作为format参数,函数的返回值就是我们需要的字符串。这是种

未公开(呵呵,pb的帮助中找不到),但被广泛使用的方法。

例:string ls_tmp

ls_tmp =string(hStrData,'Address')

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

这次,单纯依靠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 is_dst

string ls_src

long ll_address

ls_src= 'test me'

ls_dst =space(255)

ll_address=lstrcpy(ls_dst,ls_src)

麻烦是麻烦点,不过终于知道你藏身在ll_address那里了。

3、在内存堆上分配空间,并存储变量内容

这里需要LocalAlloc,LocalFree,CopyMemory三个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 ***的形式,反正都

是指变量的地址,根据需要定义喽!

例:

现在某个Api用到的结构中有一个long成员,用来存储另外一个结构Menuitemdata的地址,以备将来所需

结构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 = 12345

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)

搜集中......................WAITING..........................

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有