pb中的内存操作大搜集
2014-05-21 21:52
274 查看
我们知道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)
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)
相关文章推荐
- Powerbuilder中的内存操作大搜集
- Powerbuilder中的内存操作大搜集
- Powerbuilder中的内存操作大搜集
- powerbuilder PB 操作内存
- 最新MDB数据库操作大全(自己搜集整理基本上很全了)
- 一个操作共享内存的类!可以方面的添加,删除数据,包括各种结构体!
- 一个操作共享内存的类!可以方便的添加,删除数据,包括各种结构体!
- C语言嵌入式系统编程修炼之内存操作
- 一个操作共享内存的类!可以方面的添加,删除数据,包括各种结构体!
- 循环队列代码(支持对共享内存的操作)
- mips1处理器内存操作优化总结
- 如何在PB中进行位操作以及二进制与十进制转换
- VC++多线程下内存操作的优化
- VC++多线程下内存操作的优化
- pb 对excel的操作
- 最新MDB数据库操作大全(自己搜集整理基本上很全了)
- 如何在PB中进行位操作以及二进制与十进制转换(摘录)
- vss 命令行执行相关操作搜集整理
- pb中创建数据操作日志字典
- C语言嵌入式编程之:内存操作