C Runtime Library & variable parameters

Just starting out? Need help? Post your questions and find answers here.
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

C Runtime Library & variable parameters

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

trond, can you point mein theright direction ?

cheers

KingLestat
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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)
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

thanks
seems so easy now
reminds me of columbus egg
*sigh*
thanks

KingLestat
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Poplar
User
User
Posts: 16
Joined: Sun Apr 30, 2017 12:27 pm

Re: C Runtime Library & variable parameters

Post 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
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: C Runtime Library & variable parameters

Post 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 ....
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: C Runtime Library & variable parameters

Post 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... :(
Et cetera is my worst enemy
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: C Runtime Library & variable parameters

Post 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.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: C Runtime Library & variable parameters

Post 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.
quidquid Latine dictum sit altum videtur
Post Reply