Solved

CommandLineToArgv

Posted on 1998-08-06
6
2,181 Views
Last Modified: 2008-02-01
I'm trying to use the function CommandLineToArgv as defined in the API Loader utility. I've used CommandLineToArgvW in C++ and it was just a matter of using pointers to go from a single string to an array of strings, but how would it work in VB? I'm looking for a working code example.
0
Comment
Question by:mnichols
  • 4
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 1468170
Below is a straight excerpt from the VB helpfile. It shows you how to retrieve the various arguments from the commandline and how to put them into an array. If you need more help on this, I'll be glad helping you further.

This example uses the Command function to get the command line arguments in a function that returns them in a Variant containing an array.

Function GetCommandLine(Optional MaxArgs)
      'Declare variables.
      Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
      'See if MaxArgs was provided.
      If IsMissing(MaxArgs) Then MaxArgs = 10
      'Make array of the correct size.
      ReDim ArgArray(MaxArgs)
      NumArgs = 0: InArg = False
      'Get command line arguments.
      CmdLine = Command()
      CmdLnLen = Len(CmdLine)
      'Go thru command line one character
      'at a time.
      For I = 1 To CmdLnLen
            C = Mid(CmdLine, I, 1)

'Test for space or tab.
            If (C <> " " And C <> vbTab) Then
                  'Neither space nor tab.
                  'Test if already in argument.
                  If Not InArg Then
                  'New argument begins.
                  'Test for too many arguments.
                        If NumArgs = MaxArgs Then Exit For
                        NumArgs = NumArgs + 1
                        InArg = True
                  End If
                  'Concatenate character to current argument.
                  ArgArray(NumArgs) = ArgArray(NumArgs) & C
            Else
                  'Found a space or tab.

'Set InArg flag to False.
                  InArg = False
            End If
      Next I
      'Resize array just enough to hold arguments.
      ReDim Preserve ArgArray(NumArgs)
      'Return Array in Function name.
      GetCommandLine = ArgArray()
End Function
0
 
LVL 39

Expert Comment

by:abel
ID: 1468171
I'm sorry, the layout got lost. I was cut 'n' pasting real tabs and those apparently get deleted by the parser of EE.

Regards, Abel
0
 
LVL 1

Author Comment

by:mnichols
ID: 1468172
Thanks for the answer abel, but that example does not do true command line parsing because it does not account for double quotes around parameters with spaces. This is why I asked for an example of CommandLineToArgv.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:abel
ID: 1468173
Mnichols,

I got your point. I think you have two options:
1. Do all the "dirty" work in VB (you will have to write the while CommandLineToArgvW by yourself)
2. Make a tiny DLL in C/C++ which you can call from VB, with CommandLineToArgvW in it.

When you use CommandLineToArvW in VB you face a problem which (as far as I know) can't be handled by VB. The function returns a pointer to an array of unicode strings with the commandline arguments in it (LPWSTR *). In VB the function just returns a Long. That value will probably point to the Unicode array, but from within VB it's impossible to convert a pointer to an array to a normal VB-array. Even with the hidden functions of VB you cannot do that. The things you can do, though, are retrieving the Unicode commandline string with GetCommandLineW. You can convert that string within VB with:
StrConv(GetCommandLineW & Chr$(0), vbFromUnicode)
and you can use GetCommandLineW to pass the full commandline to the DLL (although you should be able to use GetCommandLineW in the DLL as well, because your DLL will operate in the address space of VB).
When you are going to write such a dll in C/C++ you will be having some troubles with passing the array containing the (preferably non-unicode) strings back to VB. On the VB cdrom there's an article (in the DOCS directory) that describes the pitfalls in detail.

In my opinion the most beautifull way is creating and using a selfmade DLL. But the easiest way to follow is - <g> - doing it in VB, though you may end up with a lot of code doing the complete parsing-part.

Hope this helps you a bit more on your way.

Regards, Abel
0
 
LVL 9

Accepted Solution

by:
cymbolic earned 100 total points
ID: 1468174
Here's a quick hack.  Try this:
Public Sub CmdArray(cmdary$())
Dim x$, sep$, y$, i As Integer, j As Integer, k As Integer
sep$ = ", " + Chr$(34) 'chr$(034)= "
x$ = Command$
ReDim cmdary$(0)
While Len(x$) > 0
 x$ = LTrim$(x$)
 i = Len(x$)
 For j = 1 To Len(sep$) 'get the first separator of any type
  If InStr(x$, Mid$(sep$, j, 1)) < i Then
   i = InStr(x$, Mid$(sep$, j, 1))
   k = j
  End If
 Next j
 If i = Len(x$) Then 'no seps left
  ReDim Preserve cmdary$(UBound(cmdary$) + 1)
  cmdary$(UBound(cmdary$)) = x$
  x$ = ""
 Else
  ReDim Preserve cmdary$(UBound(cmdary$) + 1)
  cmdary$(UBound(cmdary$)) = toke$(x$, Mid$(sep$, k, 1))
  x$ = LTrim$(x$) 'remove those annoying paces put there for eyeball humor
  If Left$(x$, 1) = Chr$(34) Then 'uhoh, using a " to delineate phrase
   x$ = Mid$(x$, 2)
   ReDim Preserve cmdary$(UBound(cmdary$) + 1)
   cmdary$(UBound(cmdary$)) = toke$(x$, Chr$(34))
  End If
 End If
Wend

End Sub
Public Function toke$(Inst$, sep$)
'takes off first substring in inst$, shortening inst$ each time
Dim i As Integer
i = InStr(Inst$, sep$)
If i = 0 Then
  toke$ = Inst$
  Inst$ = ""
Else
  toke$ = Left$(Inst$, i - 1)
  Inst$ = Mid$(Inst$, i + Len(sep$))
End If
 End Function

Private Sub Form_Load()
Dim cmd$(), i As Integer

CmdArray cmd$()
For i = 1 To UBound(cmd$)
 Debug.Print cmd$(i)
Next i
End
End Sub

0
 
LVL 39

Expert Comment

by:abel
ID: 1468175
That's a rich ($) function, Cymbolic :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now