cdecl calling convention
<< Back
Starting with DF20, you can call Win32 APIs that use cdecl
calling convention. The only API that I know of uses cdecl calling
convention is wsprintf. Let's
take a look how you can take advantage of this new perk from DF20.
Use UI
#IF !@>=200
External_Function wsprintf "wsprintfA" USER32.DLL Address asResult String sTemplate ;
Address a0 Address a1 Address a2 Address a3 Address a4 Address a5 Address a6 Address a7 Address a8 Address a9 ;
Address a10 Address a11 Address a12 Address a13 Address a14 Address a15 Address a16 Address a17 Address a18 Address a19 ;
Address a20 Address a21 Address a22 Address a23 Address a24 Address a25 Address a26 Address a27 Address a28 Address a29 ;
Returns Integer
#ENDIF
Procedure Test
String sResult sDay
Integer iLength
Move "Sunday" to sDay
Move (Repeat(" ", 1024)) to sResult
Move (wsprintf(AddressOf(sResult),"Today: %s Time:%.2u:%.2u",AddressOf(sDay),4,3)) to iLength
Showln sResult
InKey FieldIndex
End_Procedure
Send Test
In this example, I take advantage of the fact that all the strings used are utf8/oem strings (every string character is less than 128 ASCII code).
In real life, you need to convert all the strings from utf8 (the default string encoding in DF20) to ANSI (since we are
calling the "A" version of wsprintf). The goal of this example is to show you how to call a Windows API call with cdecl
calling convention.
Of course Microsoft doesn't want you to use this function for good reason. Since the function doesn't take the size of the
final buffer (the first parameter, asResult), you can easily run into buffer overflow bug. In the documentation
of wsprintf, it mentions some replacement functions like StringCbPrintf. Those StringCbBlahBlahBlah
functions are not part of the Windows API. They are just some C++ header inline functions that utilize the C++ runtime library.