Retrieving the command line parameters with __getmainargs

Windows specific questions.
Post Reply
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Retrieving the command line parameters with __getmainargs

Post by Vortex »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Retrieving the command line parameters with __getmainargs

Post by MrSwiss »

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: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Retrieving the command line parameters with __getmainargs

Post by jj2007 »

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

Re: Retrieving the command line parameters with __getmainargs

Post by UEZ »

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.
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: Retrieving the command line parameters with __getmainargs

Post by Pierre Bellisle »

Some APIs...

Code: Select all

 'Show every arguments
 Dim ArgCount As LONG
 Dim pArgArray As LPWSTR Pointer = CommandLineToArgvW(GetCommandLineW(), @ArgCount)
 For index As LONG = 0 TO ArgCount - 1
   Print Str(index) & ") [" & *pArgArray[index] & "]"
 Next : Print
 LocalFree(pArgArray) 

 'Show full command line
 Dim pCommand As LPWSTR = GetCommandLineW()
 Print "GetCommandLineW [" & *pCommand & "]" : Print

 'Show full command line without path name
 Dim pArgument As LPWSTR = PathGetArgsW(pCommand)
 Print "PathGetArgsW [" & *pArgument & "]" : Print

 'Show full path name without arguments
 PathRemoveArgsW(pCommand)
 Print "PathRemoveArgsW [" & *pCommand & "]" : Print 
 
Post Reply