Page 1 of 2

C Runtime Library & variable parameters

Posted: Sat Dec 30, 2006 1:44 pm
by kinglestat
I've recently seen a post (which I cant find again!) of an optimized FindString using the C runtime library (MSVCRT.DLL) and prototypes. First of all I want to thank the author(s) for such a splendid example. I adapted for using strchr and strrchr. And working thanks very much!

But

I tried to get sprintf to work.....without any luck

So I have 2 questions

1. Is it possible to make a macro/function in PB with a variable number of parameters ?

2. Is it possible to use the sprintf in the C Runtime library (or any similar functions which use variable number of parameters)

cheers

KingLestat

Posted: Sat Dec 30, 2006 2:43 pm
by Trond
1. Is it possible to make a macro/function in PB with a variable number of parameters ?
Yes, but it may not be possible to call it directly from PB.

Posted: Sat Dec 30, 2006 2:50 pm
by netmaestro
I've recently seen a post (which I cant find again!) of an optimized FindString using the C runtime library (MSVCRT.DLL) and prototypes
http://www.purebasic.fr/english/viewtopic.php?t=24268

Posted: Sat Dec 30, 2006 6:53 pm
by kinglestat
trond, can you point mein theright direction ?

cheers

KingLestat

Posted: Sat Dec 30, 2006 7:33 pm
by Trond
I don't know what you want to do. Just call sprintf?

Code: Select all

OpenLibrary(0, "msvcrt.dll")
sprintf = GetFunction(0, "sprintf")
CloseLibrary(0)

a = 5
n = @"A: %d"

buffer.s = Space(50)

CharsWritten = CallCFunctionFast(sprintf, buffer, n, a)

Debug buffer
Debug CharsWritten
Debug Len(buffer)

Posted: Sat Dec 30, 2006 9:03 pm
by kinglestat
thanks
seems so easy now
reminds me of columbus egg
*sigh*
thanks

KingLestat

Posted: Sat Dec 30, 2006 9:32 pm
by Flype
antoher way :

Code: Select all

ImportC "msvcrt.lib"
  sprintf(Buffer.s, Format.s, Arg1.l = 0, Arg2.l = 0, Arg3.l = 0, Arg4.l = 0) ; Write formatted data to a string.
EndImport

Result$ = Space(10)

If sprintf(Result$, "%d%d%d", 123, 456, 789)
  Debug Result$
EndIf

Posted: Sat Dec 30, 2006 11:09 pm
by Trond
Flype, the point of sprintf is that it has a dynamic number of parameters. That's not the same as passing 0 for the unused ones.

Posted: Sat Dec 30, 2006 11:28 pm
by netmaestro
@Trond: yours doesn't work because you closed the library too soon. (again) Your code will be a lot more reliable if you leave libraries open until you're finished with them. Assuming they'll be available because you're under the impression that they're always loaded is a mistake.

Posted: Sat Dec 30, 2006 11:52 pm
by Trond
netmaestro wrote:@Trond: yours doesn't work because you closed the library too soon. (again) Your code will be a lot more reliable when you learn to leave libraries open until you're finished with them. Assuming they'll be available because you're under the impression that they're always loaded is a mistake.
Umm yes, I misread the import table thinking crt.dll was msvcrt.dll. But kernel32.dll is still always loaded even if it's closed.

Re: C Runtime Library & variable parameters

Posted: Sat Jun 17, 2017 4:18 am
by Poplar

Code: Select all

EnableExplicit
Define lib.l, a.i, n.s, buffer.s, CharsWritten.i
lib = OpenLibrary(#PB_Any, "msvcrt.dll")
PrototypeC.i ProtoSprintf(Buffer.s, Format.s, Arg1.l = 0, Arg2.l = 0, Arg3.l = 0, Arg4.l = 0)
Global sprintf.ProtoSprintf = GetFunction(lib, "swprintf")
CloseLibrary(lib)

a = 5
n = "About: %d"

buffer = Space(50)

CharsWritten = sprintf(buffer, n, a)

Debug buffer
Debug CharsWritten
Debug Len(buffer)
__________________________________________________
Code tags added
17.06.2017
RSBasic

Re: C Runtime Library & variable parameters

Posted: Sat Jun 17, 2017 9:12 am
by Fluid Byte
I have to correct myself, THIS, is the worst case of necro-posting I have ever seen. 11 years, come the fuck on dude ....

And again no code-tags. Just wow ....

Re: C Runtime Library & variable parameters

Posted: Sat Jun 17, 2017 11:35 am
by chi
@Fluid Byte: Dude... chill... It's his/her 5th post and most likely doesn't know better. No reason to be rude!

@Poplar: Maybe use the

Code: Select all

-tag next time. Some people are easily triggered... :(

Re: C Runtime Library & variable parameters

Posted: Sat Jun 17, 2017 12:49 pm
by Fluid Byte
chi wrote:@Fluid Byte: Dude... chill... It's his/her 5th post and most likely doesn't know better. No reason to be rude!
If it's his 5th or 5000th post is irrelevant, your argument is pointless. You don't necro-post 11 year old posts, period. Stop defending behavior like this.
chi wrote:@Poplar: Maybe use the

Code: Select all

-tag next time. Some people are easily triggered... :([/quote]
Triggered? Are you a stereotypical 14 year old meme kid on YouTube? Don't waste peoples time with this bullshit please.

Re: C Runtime Library & variable parameters

Posted: Sat Jun 17, 2017 2:19 pm
by freak
@Fluid Byte:
Whats up with you today? Take it down a notch and stop antagonizing people. If you continue acting like you're in kindergarden, I'll have to give you a time out.

Have a beer and relax a bit dude.