import lib and variable number of arguments

Everything else that doesn't fall into one of the other PB categories.
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

import lib and variable number of arguments

Post 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 ?
["1:0>1"]
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post 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)
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post 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:
["1:0>1"]
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Poplar
User
User
Posts: 16
Joined: Sun Apr 30, 2017 12:27 pm

Re: import lib and variable number of arguments

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