April 27, 2024, 03:06:15 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


How to get command line arguments

Started by tekrat, June 09, 2008, 07:13:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tekrat

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.

Ionic Wind Support Team

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
Ionic Wind Support Team