December 11, 2024, 09:11:21 PM

News:

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


command line parameters

Started by DennisL, April 18, 2008, 03:35:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DennisL

Is it possible to retrieve the command line parameters that a console program was started with like in VB6 had Command$?

DennisL

thanks for the link, but it says that i do not have access to view the topic...  ::)

i really don't want to sign up to another forum (i've lost track of the number that i'm a member of), so can someone either make this available from somewhere public (maybe here?), or provide a means using the functionality/commands available from EBasic itself?

thanks in advance for any reply.  :)

Ionic Wind Support Team

Use the Windows API function GetCommandLineA

Paul.
Ionic Wind Support Team

DennisL

thanks for the quick response and the great (and simple) solution paul!!  ;D

i should have known that there was an API function to do this, but was just too lazy to look for it.  :-[

have a good one!

Ionic Wind Support Team

Here is another example.


DECLARE "shell32.dll", CommandLineToArgv alias CommandLineToArgvW(lpCmdLine:STRING, pNumArgs:WORD),POINTER
DECLARE "kernel32.dll",GetCommandLine alias GetCommandLineW(),STRING
DECLARE "kernel32.dll",GlobalFree(hMem:UINT),INT
DECLARE IMPORT,WideCharToMultiByte(codepage:UINT,dwFlags:UINT,lpWideStr:POINTER,cchWideStr:INT,lpMultiByteStr:POINTER,cchMultiByte:INT,lpDefaultChar:POINTER,lpUsedDefault:POINTER),INT
CONST CP_ACP = 0

OPENCONSOLE
CLS

pointer pszArglist
STRING strTemp
WORD nArgs
INT i
   pszArglist = CommandLineToArgv(GetCommandLine(), &nArgs)
   print "Number of args = " + str$(nArgs)
   if(NULL = pszArglist)
      print("CommandLineToArgv failed.")
   else
      for i=0 to nArgs-1
WideCharToMultiByte(CP_ACP,0,*<STRING>*<POINTER>(pszArglist+(i*4)),-1,strTemp,254,NULL,NULL)
         print strTemp
      next i
   endif
'// Free memory allocated for CommandLineToArgv arguments.
   GlobalFree(pszArglist)

DO: UNTIL INKEY$ <> ""
CLOSECONSOLE
END
Ionic Wind Support Team

Peter

Sorry for bumping, but there might be a way to get the filename (without path?) or the commandline arguments without that much overhead.
When the program is executed, isn't the stack filled with parameters, such as commandline etc? If so, wouldn't it be possible to pick out the filename as the first argument?

smthg like (excuse me but I still haven't figured out how to react between variables/ints and inline asm):
esp+4 for amount of commandline arguments
esp+8 for the first argument
and so on?

Ionic Wind Support Team

Where did you get that idea?

The command line is an environment variable set by the OS after it creates your virtual process space and before it jumps to the entry point of the executable.  There is nothing on the stack on entry.

There is no overhead in what I did above, and it is more or less the standard way to parse the command line string that Windows provides.  You can just use GetCommandLineA and parse it yourself if you wish.   GetCommandLineA calls GetCommandLineW/WideCharToMultByte according to MSDN.

Paul.
Ionic Wind Support Team

Peter

Excuse me Paul, I probably didn't count the bytes right then. I thought that it would increase the size more using the import library for the appropriate dll for calling GetCommandLineA compared to using asm. My asm skills are terrible and I thought that esp was a "stack pointer" and therefor something regarding the stack. Would this method (using esp+# that is) at all be possible then if I may ask?

Ionic Wind Support Team

You misunderstood me.   Of course there is a stack, it is just that Windows doesn't put the command line there when your executable is ran.  The command line is a string.

Kernel32.DLL is always part of your program so functions like GetCommandLineA are imported and available whether you use them or not.  Your program can't run without user32.DLL and Kernel32.DLL, even if it is a console applicaiton.

Paul.
Ionic Wind Support Team

Peter

I know there is a stack, and what I thought was that a pointer to a string containing the filename of the running executable as well as a pointer to a string containing the commandline was pushed onto the stack upon execution. What I wanted was a method to figure out the name of the running executable without having to parse the whole commandline etc, and I thought this would be a way to both find out the filename as well as the commandline separate from eachother in a way that would require less code.

Is there a chance that INSTR() will be rewritten slightly, or complemented by another command, to be able to make it search from right to left instead of the opposite? Maybe using negative startvalues? Your code would be way more optimized than anything I would write.

Thanks.

LarryMc

Quote from: Peter on April 24, 2008, 12:40:57 PM
Is there a chance that INSTR() will be rewritten slightly, or complemented by another command, to be able to make it search from right to left instead of the opposite?

One exists in the addon Pak that can just be installed as a library.

Find it here: http://www.ionicwind.com/forums/index.php/topic,1175.msg11126.html#msg11126

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Peter

Thanks a bunch Larry! Exactly what I needed. Gotta look through this thing and see if there's anything more I might need. Cheers!