Delphi character and string [5]-string and Windows API

Source: Internet
Author: User
In Windows API, the string corresponds to the pchar (pansichar) of Delphi. In API, the string of Delphi is flexible.

Assign values first:

 
// Assignment Method 1: Give begin setwindowtext (handle, 'newtitle'); end;

  // Assignment Method 2: define the type var P: pchar; begin P: = 'newtitle'; setwindowtext (handle, P); end;

  // Value assignment method 3: convert to the desired var STR: string; begin STR: = 'newtitle'; setwindowtext (handle, pchar (STR); end; // Assignment Method 4: var arr: array [0 .. 255] of char; begin arr: = 'new title'; setwindowtext (handle, arr); end;

  

Value:

// Method 1: Use a character array (often called "buffer") var arr: array [0 .. 254] of char; begin getwindowtext (handle, arr, 255); showmessage (ARR); {form1} end;

  // Method 2: Use getmem to allocate memory for pchar var P: pchar; begin getmem (p, 255); {memory allocation} getwindowtext (handle, P, 255 ); showmessage (p); {form1} freemem (p); {release memory} end;

  // Method 3: Use globalalloc to allocate global memory (slower than getmem) var P: hglobal; begin P: = globalalloc (0,255 ); {0 or gmem_fixed indicates that fixed memory is allocated.} getwindowtext (handle, pchar (P), 255); showmessage (pchar (p )); {form1} globalfree (p); {release memory} end;

  // Method 4: Use string directly; setlength is required first, and then the blank space is removed: var STR: string; begin setlength (STR, 255 ); {set the STR length first} getwindowtext (handle, pchar (STR), 255); {but the STR length is 255 at this time !} STR: = pchar (STR); {the actual length of the string} showmessage (STR); {form1} end;

  

The fixed-length string does not end with #0. It is not compatible with the API and is generally not used in the API.

Related Article

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.