IonicWind Software

IWBasic => The Roundtable => Topic started by: tekrat on June 09, 2008, 07:13:43 PM

Title: How to get command line arguments
Post by: tekrat on June 09, 2008, 07:13:43 PM
Theres been a ton of questions on how to get the command line arguments.  Here's how:


$USE "wininet.lib"

DECLARE "wininet",GetCommandLineA(),STRING

print GetCommandLineA()

input ""


One thing to remember is that this will return what is typed (or shelled from Windows) to run the file is returned.  So say you compiled this file to a program name "cli.exe".  Then you typed from a shell or run the program from a batch like this:


cli This is my message.


The GetCommandLineA() function will return:


cli This is my message.


Now if you put the cli.exe in a folder called "c:\Program Files\My CLI Test" and then create a Windows shortcut to your "c:\Program Files\My CLI Test\cli.exe" this what GetCommandLineA() function will return:


"c:\Program Files\My CLI Test\cli.exe"


Note the quote marks.  So you will have to be careful on parsing the quote marks.
Title: Re: How to get command line arguments
Post by: Ionic Wind Support Team on June 10, 2008, 09:20:54 AM
Here is a little subroutine I use that among other things extracts all of the command line parameters.  You should be able to modify it for your own use.


DECLARE IMPORT, CommandLineToArgvW(lpCmdLine as WSTRING, pNumArgs as POINTER),POINTER
DECLARE IMPORT, GlobalFree(hMem as UINT),INT
....
Sub RunProcess(string _command)
pointer _pszArglist
word _nArgs
int _i
string _exe,_params:_params = ""
   _pszArglist = CommandLineToArgvW(s2w(_command), &_nArgs)
if(_nArgs<>0)
_exe = w2s(*<WSTRING>*<POINTER>_pszArglist)
for _i=1 to _nArgs-1
_params += w2s(*<WSTRING>*<POINTER>(_pszArglist+(_i*4))) + " "
next _i
System _exe,_params
endif
if(_pszArglist) then GlobalFree(_pszArglist)
Endsub