vb command line arguments are delimited, similar to the C language of main (int argc, char* argv[])

Source: Internet
Author: User

VB6.0 in order to provide command-line arguments support, provided the commands () interface, so through the command () function can get all the incoming parameters, but very unfriendly, VB all the parameters are closed together, become a string, when there are multiple parameters, To use it is not so convenient, so, then there is the following code, code I do not remember where it came from, if you are the author of the article, please let me know [email protected]@

Option Explicit

Private Declare Function getcommandlinew Lib "kernel32" () as Long
Private Declare Function lstrlenw Lib "kernel32" (ByVal lpstring as Long) as Long
Private Declare Function commandlinetoargvw Lib "Shell32" (ByVal lpcmdline as Long, Pnnumargs as long) as Long
Private Declare Function localfree Lib "kernel32" (ByVal Hmem as Long) as Long
Private Declare Sub copymemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as any, Source as any, ByVal Length A s Long)

Public Function Splitcmd (ByRef Argc as Long, ByRef Argv () as String)
Dim Nnumargs as Long '//command-line argument number
Dim lpszarglist as Long '//command-line parameter array address
Dim Lpszarg as Long '//command line parameter address
Dim narglength as Long '//command line parameter length
Dim Szarg () as Byte '//command line Parameters
Dim I as Long

lpszarglist = COMMANDLINETOARGVW (Getcommandlinew (), Nnumargs)
If lpszarglist Then
ARGC = Nnumargs '//output total number
ReDim Argv (nNumArgs-1)
copymemory ByVal varptr (lpszarg), ByVal lpszarglist, 4 '//Get argv (0) address

For i = 0 to nNumArgs-1
narglength = Lstrlenw (lpszarg)
ReDim szarg (narglength * 2-1)
copymemory ByVal varptr (szarg (0)), ByVal Lpszarg, Narglength * 2
Argv (i) = CStr (Szarg)
Lpszarg = Lpszarg + narglength * 2 + 2
Next

Erase Szarg
Call LocalFree (lpszarglist)
End If
End Function


The invocation is simple, the entry function Splitcmd (ByRef Argc as Long, ByRef Argv () as String), the first parameter is the passed parameter: the total number of parameters, the second parameter is the parameter array, it is necessary to note that the return value of this function is the same as the C language, ARGC at least one value, that is the path of the program itself, ARGV (0) is the path of the program itself, the following provides a way to use.

Option Explicit

Private Sub Form_Load ()
Dim ARGC as Long, ARGV () as String
Dim I as Integer, szcmd as String

Call Splitcmd (ARGC, ARGV ())
For i = 0 to Argc-1
Szcmd = szcmd & I & VbTab & ARGV (i) & VbCrLf
Next
MsgBox Szcmd
End Sub

In this way, you will get a result similar to the following:


Here, everything goes well.
Girl does not Cry (qq:191035066) @2011-11-13 16:02



 Category: VB6.0

vb command line arguments are delimited, similar to the C language of main (int argc, char* argv[])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.