Retrieving the command line parameters with __getmainargs

Windows specific questions.
Vortex
Posts: 48
Joined: Sep 19, 2005 9:50

Retrieving the command line parameters with __getmainargs

Postby Vortex » May 29, 2021 15:55

The function __getmainargs exported by msvcrt.dll returns also the number of the command line parameters. Here is a quick example :

Code: Select all

#include "windows.bi"

Declare Function __getmainargs Cdecl Alias "__getmainargs" (Byval As Integer Ptr,_
                 Byval As Zstring Ptr Ptr Ptr,Byval As Zstring Ptr Ptr Ptr,_
                 Byval As Integer,Byval As LPSTARTUPINFO) As Integer

Dim As Integer argc
Dim As ZString Ptr Ptr argv
Dim As ZString Ptr Ptr env
Dim As Integer sinfo
Dim As Integer i

sinfo=0

__getmainargs(@argc,@argv,@env,0,Cast(LPSTARTUPINFO,@sinfo))

For i=0 To argc-1

    Print "Cmdline parameter " & i & " = " & **argv

    argv=argv+1

Next i
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Retrieving the command line parameters with __getmainargs

Postby MrSwiss » May 29, 2021 16:14

Another method using pure FB (internal defines):

Code: Select all

' ARGC_ARGV-test.bas -- (c) 2020-05-13, MrSwiss
'
' compile: -s console   ' aka: default
'
Dim As ULong            argc = __FB_ARGC__
Dim As ZString Ptr Ptr  argv = __FB_ARGV__

For i As UInteger = 0 To argc - 1
    ? "argc = "; i, "*argv["; i; "] = "; *argv[i]
Next

? : ? : ? "press a key to exit program ... ";
Sleep
' ----- EOF -----
jj2007
Posts: 2142
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Retrieving the command line parameters with __getmainargs

Postby jj2007 » May 30, 2021 0:22

UEZ
Posts: 729
Joined: May 05, 2017 19:59
Location: Germany

Re: Retrieving the command line parameters with __getmainargs

Postby UEZ » May 30, 2021 17:18

Vortex wrote:The function __getmainargs exported by msvcrt.dll returns also the number of the command line parameters. Here is a quick example :

Code: Select all

#include "windows.bi"

Declare Function __getmainargs Cdecl Alias "__getmainargs" (Byval As Integer Ptr,_
                 Byval As Zstring Ptr Ptr Ptr,Byval As Zstring Ptr Ptr Ptr,_
                 Byval As Integer,Byval As LPSTARTUPINFO) As Integer

Dim As Integer argc
Dim As ZString Ptr Ptr argv
Dim As ZString Ptr Ptr env
Dim As Integer sinfo
Dim As Integer i

sinfo=0

__getmainargs(@argc,@argv,@env,0,Cast(LPSTARTUPINFO,@sinfo))

For i=0 To argc-1

    Print "Cmdline parameter " & i & " = " & **argv

    argv=argv+1

Next i


Thanks for sharing. It's always good to have alternatives.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 4 guests