Several methods for using the pchar parameter in API functions

Source: Internet
Author: User
// Take getwindowsdirectory as an example: {buffer using static arrays} procedure tform1.button1click (Sender: tobject); var Buf: array [0 .. MAX_PATH-1] of char; begin getwindowsdirectory (BUF, sizeof (BUF); showmessage (BUF); {C: \ WINDOWS} end; {self-allocated memory} procedure tform1.button2click (Sender: tobject); var P: pchar; begin P: = stralloc (max_path); getwindowsdirectory (p, strbufsize (p); showmessage (p); {C: \ Windows} strdispose (p); end; {use string directly; this and the following methods both need to delete the trailing Blank} procedure tform1.button3click (Sender: tobject); var STR: string; len: integer; begin setlength (STR, max_path); Len: = getwindowsdirectory (pchar (STR), bytelength (STR); setlength (STR, Len); showmessage (STR ); {C: \ Windows} end; {change pchar (STR) to @ STR [1]} procedure tform1.button4click (Sender: tobject); var STR: string; Len: integer; begin setlength (STR, max_path); Len: = getwindowsdirectory (@ STR [1], bytelength (STR); setlength (STR, Len); showmessage (STR ); {C: \ Windows} end; {This method is the best. Obtain the result length first ...} procedure tform1.button5click (Sender: tobject); var Len: integer; STR: string; begin Len: = getwindowsdirectory (nil, 0); setlength (STR, Len ); getwindowsdirectory (pchar (STR), Len); showmessage (STR); {C :\\ windows} end;
 
  
 

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.