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

Just starting out? Need help? Post your questions and find answers here.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

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

Post 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

jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

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

Post 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) 
Post Reply