Page 1 of 1

How to use msvcrt.dll or lib functions like sprintf()?

Posted: Sun Jul 23, 2017 6:16 pm
by skywalk
sprintf() used to work?

Code: Select all

Prototype sprintf(*a, *b, d.d)
OpenLibrary(0, "C:\Windows\System32\msvcrt.dll")
Global sprintf.sprintf = GetFunction(0, "sprintf")
Global.i *b, *f
*b = AllocateMemory(64)
*f = Ascii("%e")  ; = @"%e"
Debug sprintf(*b, *f, #PI)
y$ = PeekS(*b, -1, #PB_Ascii)
Debug y$    ;<-- 0.000000e+000

Re: How to use msvcrt.dll or lib functions like sprintf()?

Posted: Sun Jul 23, 2017 6:32 pm
by fryquez
Does not seems to accept a double as first parameter, dunno why?

Code: Select all

ImportC ""
  _snwprintf(sout, iCount, sformat, a1.s, a2.d)
EndImport

iBufferSize = 256
sOut.s = Space(iBufferSize)
_snwprintf(@sOut, iBufferSize, "%s %f", "Pi=", #PI)
Debug sOut


Re: How to use msvcrt.dll or lib functions like sprintf()?

Posted: Sun Jul 23, 2017 8:36 pm
by jack
fryquez, your solution works in unicode only, which is OK considering that the newest version of PB only supports unicode, however this works in both modes.

Code: Select all

ImportC ""
  _snprintf(*sout.p-Ascii, iCount.i, sformat.p-Ascii, s.p-Ascii, a2.d)
EndImport

iBufferSize = 256
sOut.s = Space(iBufferSize)
_snprintf(@sOut, iBufferSize, "%s %16.14e", "Pi=", #PI)
Debug PeekS(@sOut, -1, #PB_Ascii)