Page 1 of 1

import lib and variable number of arguments

Posted: Wed Mar 26, 2008 8:37 am
by bingo
possible , but fussily ... :cry:

Code: Select all

Import "msvcrt.lib" 
_snprintf1(buffer.l,count.l,format.l,arg1.l) As "__snprintf@16" 
_snprintf2(buffer.l,count.l,format.l,arg1.l,arg2.l) As "__snprintf@16" 
EndImport 

msize.l = 200
membuffer.l = AllocateMemory(msize)

_snprintf1(membuffer,msize,@"test %s",@"xxx1")

Debug PeekS(membuffer)

_snprintf2(membuffer,msize,@"test %s + %s",@"xxx1",@"xxx2")

Debug PeekS(membuffer)
any hints to handle variable number of arguments ?

Posted: Wed Mar 26, 2008 8:55 am
by ABBKlaus

Code: Select all

Import "msvcrt.lib" 
 _snprintf(buffer.l,count.l,format.l,arg1.l,arg2.l=0) As "__snprintf@16" 
EndImport 

msize.l = 200 
membuffer.l = AllocateMemory(msize) 

_snprintf(membuffer,msize,@"test %s",@"xxx1") 

Debug PeekS(membuffer) 

_snprintf(membuffer,msize,@"test %s + %s",@"xxx1",@"xxx2") 

Debug PeekS(membuffer)

Posted: Wed Mar 26, 2008 9:31 am
by tinman
For sprintf type of functions you could use the vprintf versions. Instead of variable number of arguments, it takes a pointer to the arguments in memory.

Posted: Wed Mar 26, 2008 11:01 am
by bingo
@tinman
do you have a sample ... ?
http://msdn2.microsoft.com/en-us/librar ... S.71).aspx
va_list argptr :?:

@ABBKlaus
...arg2.l=0... is possible - ok !
arg2.l=0,arg3.l=0,arg4.l=0,arg5.l=0,arg6.l=0,arg7.l=0,arg8.l=0,arg9.l=0 :lol:

Posted: Wed Mar 26, 2008 11:38 am
by tinman
bingo wrote:@tinman
do you have a sample ... ?
http://msdn2.microsoft.com/en-us/librar ... S.71).aspx
va_list argptr :?:
Only this:
tinman wrote:wvsprintf() is available as a Win32 API function in user32.dll. Where does FormatString() come from?

In PureBasic you don't need to use printf if you don't want to. It's just a combination of numerical conversion, formatting and converting to a string. result$ = "Foo: "+Str(value) and so on

Unless of course the format string is specified externally to your code. Then you might as well just use wsvprintf(). I think, but I'm not sure, that the va_list is just a pointer to a block of memory which contains the values you are converting. For example:

Code: Select all

Import "user32.lib"
    wvsprintf_.l(lpOutput.l, lpFormat.s, arglist.l) As "_wvsprintfW"
EndImport

output.s = Space(1024) ; 1024 is the maximum size
value.l = 23
wvsprintf_(@output, "Hi I'm %ld years old", @value) ; we are only converting value
Debug output
But if you had multiple values to convert then you would point to a sequential block of memory into which all the values you were converting were copied. You could try using wsprintf_(), which is probably an ImportC, which does the same but your va_list is automatically pushed onto the stack instead of you having to allocate memory and copy values there.

Re: import lib and variable number of arguments

Posted: Tue Jun 20, 2017 2:41 pm
by Poplar

Code: Select all

EnableExplicit

Define lib.l = OpenLibrary(#PB_Any, "User32.dll")
If lib > 0
  PrototypeC.i prpto_wvsprintf(lpOutput.s, lpFmt.s, *arglist)
  Global wvsprintf_.prpto_wvsprintf = GetFunction(lib, "wvsprintfW")
  CloseLibrary(lib)
EndIf

; Import "user32.lib"
;   wvsprintf_.l(lpOutput.l, lpFormat.s, arglist.l) As "_wvsprintfW"
; EndImport

Define output.s = Space(1024) ; 1024 is the maximum size
Dim value.l(4)
value(0) = 25
value(1) = 50
value(2) = 80
value(3) = 78
wvsprintf_(output, "Hi I'm %ld years old.Hi I'm %ld years old.Hi I'm %ld years old.Hi I'm %ld years old.", value()) ; we are only converting value
Debug output
__________________________________________________
Code tags added
03.07.2017
RSBasic