Delphi memory operation function (6): memory allocation across processes

Source: Internet
Author: User
Delphi provides us with three convenient functions:
 
Globalallocptr {globalalloc for simplified self-API} globalreallocptr {globalrealloc for simplified self-API} globalfreeptr {globalfree for simplified self-API}

Read/writeProgramIt is convenient to use other data, such:

 
P: = globalallocptr (0, Len); {allocation} p: = globalreallocptr (p, Len, 0); {redistribution} globalfreeptr (p); {release}

Note that the first parameter of globalallocptr and the last parameter of globalreallocptr are all 0;
The two parameters have the same meaning. The specification should be written as gmem_fixed (indicating the allocation of fixed memory). Common parameters include:

 
Gmem_moveable {allocating removable memory} gmem_zeroinit {clearing memory at the same time} ghnd {allocating removable memory and clearing at the same time} gptr {allocating fixed memory and clearing at the same time}

There are many other parameters, which are consistent with the parameters of the corresponding API functions.

The following example does not use objects other than processes. You can use memo1.handle as an external text container (such as NotePad) to try it out:

Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; button1: tbutton; button2: tbutton; Procedure formcreate (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); var P: pointer; Len: integer; begin Len: = 6 + 1; {if you want to read 6 characters, leave empty characters to end} p: = globalallocptr (0, Len * 2); {allocated memory Len * 2 is for dubyte characters} sendmessage (memo1.handle, wm_gettext, Len, cardinal (p); showmessage (pchar (p); {codege} globalfreeptr (p); end; Procedure tform1.button2click (Sender: tobject); var P: pointer; Len: integer; begin Len: = 6 + 1; {if you want to read 6 characters, the empty character to exit} p: = globalallocptr (0, Len * 2 ); {allocated memory Len * 2 is for double byte characters} sendmessage (memo1.handle, wm_gettext, Len, Cardinal (p); showmessage (pchar (p )); {codege} {continue on the basis of the previous example. first obtain the actual length} Len: = sendmessage (memo1.handle, wm_gettextlength, 0, 0); Len: = (LEN + 1) * 2; p: = globalreallocptr (p, Len, ghnd); {realallocate memory} sendmessage (memo1.handle, wm_gettext, Len, Cardinal (p); showmessage (pchar (p )); {codegear Delphi 2009} globalfreeptr (p); end; Procedure tform1.formcreate (Sender: tobject); begin memo1.align: = alleft; cursor: = ssvertical; memo1.text: = 'codegear Delphi 2009 '; end; end.
 
   
 

Form file:

 
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 151 clientwidth = 295 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false oncreate = formcreate pixelsperinch = 96 textheight = 13 object memo1: tmemo left = 8 Top = 8 width = 169 Height = 89 lines. strings = ('memo1') taborder = 0 end object button1: tbutton left = 192 Top = 32 width = 75 Height = 25 caption = 'button1' taborder = 1 onclick = button1click end object button2: tbutton left = 192 Top = 72 width = 75 Height = 25 caption = 'button2' taborder = 2 onclick = button2click endend

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.