Memory operation in PowerBuilder

xiaoxiao2021-03-06  22

We know that the pointer is not supported in the PB, but when using the Win32 API and the external function in some DLL, we often have some intersections with them, so some of the relevant techniques will be collected.

1. Get the string according to the string address

The function string itself through the PB can be implemented, the syntax of the function is String (data, {format}), when we use the variable address as a Data parameter, the string "address" as the format parameter, the return value of the function is what we need String. This is an undisclosed (huh, it can't be found in the help of PB), but it is widely used.

Example: string ls_tmp ls_tmp = string (HSTRDATA, "Address")

2, get the address of a string variable in the PB

This time, it's just that the PB itself is not available. If you need to come to the Win API function to help:

Master: Function Long LSTRCPY (Ref string desination, ref string source) library "kernel32.dll"

prototype:

. 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.

See how it is big to show your hand:

Define instance variables: string is_dst

String ls_src long ll_address ls_src = "test me" ls_dst = space (255) ll_address = lstrcpy (ls_dst, ls_src) // copy the contents of the ls_src to LS_DST, and return the storage address of LS_DST

Trouble is trouble, but finally know that you are hiding in ll_address there.

3, allocate space on the internal stack, and store the contents of the variable

Here you need Localalloc, Localfree, CopyMemory three API functions, where localloc, localfree is used to apply, release the memory block, and copyMemory is used to copy memory blocks. Here you focus on the CopyMemory function, there are three parameters

PVOID DESTINATION, / / ​​Address of move, in // address loid-/ size, in bytes, of block to move

The first two parameters are the type of pointer, so we can define it as long or ref *** as needed, anyway, the address of the variable, defined as needed!

example:

Now there is a long member in a structure for an API to store the address of another structure MenuiteMData for future needs.

The structural menuitemdata is as follows:

Type Menuitemdata from Structure Unsignedlong HMENU INTEGER Leveled Type

Ok, look at how to solve this problem.

Related external functions:

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 "

Example Variable: long il_menudatapointer Menuitemdata LpMenuItemData

// The following code copies the contents of the memory block il_menuDataPointer lpmenuitemdata in lpmenuitemdata.hmenu = 12345lpmenuitemdata.level = 1il_menuDataPointer = LocalAlloc (0,6) // 6 = sizeof (menuitemdata) CopyMemory (il_menuDataPointer, lpmenuitemdata, 6)

// So, if you take it again from the memory block? ? CopyMemory (lpMenuitemdata, IL_MENUDATAPOINTER, 6) / / is very simple!

// Now, I don't need IL_menudataPointer this memory LocalFree (Il_menudataPointer)

Search .................... Waiting ......................... .

转载请注明原文地址:https://www.9cbs.com/read-44051.html

New Post(0)