Winapi: formatting output functions of wvsprintf and wsprintf-Windows

Source: Internet
Author: User
Wvsprintf function declaration:
 
Wvsprintf (output: pchar; {used to return the buffer} format: pchar; {format; similar to the format, it should be the same as the C-language printf format} Arglist: va_list {array pointer}): integer; {length of the actual string returned to the buffer}

  

Example of the wvsprintf function:

 
// Example 1: var Buf: pchar; arr: array [0 .. 1] of pointer; begin Buf: = pchar (stringofchar (#0,255); {prepare a buffer like this} arr [0]: = pchar ('in case '); arr [1]: = pointer (123); wvsprintf (BUF, '% s, % d', @ ARR); showmessage (BUF); {display result: In case, 123} end; // Example 2: var Buf: pchar; arr: array of pointer; {the difference with the previous example is that dynamic array is used here} begin Buf: = pchar (stringofchar (#0,255); setlength (ARR, length (ARR) + 1); arr [high (ARR)]: = pchar ('case '); setlength (ARR, length (ARR) + 1); arr [high (ARR)]: = pointer (123); wvsprintf (BUF, '% s, % d ', pchar (ARR); // wvsprintf (BUF, '% s, % d', @ arr [0]); {or so} showmessage (BUF); {display result: in case, 123} end;

  

The difference between the wsprintf function and wvsprintf function is that wvsprintf is a set of data, and wsprintf is a set of data. The declaration is as follows:

 
Wsprintf (output: pchar; {buffer returned} format: pchar; {format} data: pointer {Data Pointer}): integer; {length of the actual string returned to the buffer}

  

In particular, the wsprintf function declaration in Delphi 2007 is incorrect and a parameter is missing. You need to declare it again:

If it is used only in this unit, you can declare it as follows:

 
... Implementation {$ R *. DFM} function wsprintf (output: pchar; format: pchar; Data: pointer): integer; stdcall; External USER32 name 'wsprintfa ';...

If global use requires such declaration:

 
... Function wsprintf (output: pchar; format: pchar; Data: pointer): integer; stdcall; var form1: tform1; implementation {$ R *. DFM} function wsprintf; External USER32 name 'wsprintfa ';...

  

Example of the wsprintf function:

Function wsprintf (output: pchar; format: pchar; Data: pointer): integer; stdcall; External USER32 name 'wsprintfa '; Procedure tform1.formcreate (Sender: tobject); var Buf: pchar; begin Buf: = pchar (stringofchar (#0,255); wsprintf (BUF, 'I am % s', pchar (' In case '); showmessage (BUF ); {I am in case} wsprintf (BUF, 'current is % d year', pointer (2008); showmessage (BUF); {now is July 22, 2008} end;

  

Finally, use the hexadecimal notation to test their differences with the format function (the format function of Delphi is rewritten by Assembly rather than calling the API ):

 
VaR Buf: pchar; arr: array [0 .. 0] of pointer; I: integer; begin Buf: = pchar (stringofchar (#0,255); I: = maxint; arr [0]: = pointer (I ); showmessage (format ('% x', [I]); {7 fffffff} showmessagefmt (' % x', [I]); {7 fffffffff} wvsprintf (BUF, '% x', @ ARR); showmessage (BUF); {7 fffffff} end;

  
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.