Retrieving the command line parameters with __getmainargs

Windows specific questions.
Vortex
Posts: 57
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: 3845
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: 2170
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: 730
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.
Pierre Bellisle
Posts: 44
Joined: Dec 11, 2016 17:22

Re: Retrieving the command line parameters with __getmainargs

Postby Pierre Bellisle » Jun 23, 2021 5:47

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
 

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 4 guests