VB command line parameters are separated, similar to main (INT argc, char * argv []) in C language.

Source: Internet
Author: User

VB6.0 provides the command () interface to provide support for command line parameters. Therefore, all input parameters can be obtained through the command () function, but it is unfriendly, all the parameters of VB are combined and become a string. When there are multiple parameters, it is not convenient to use them. Therefore, the followingCodeCode, I do not remember where it comes from, if you areArticlePlease let me know .@@

 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 CommandlinetoargvwLib " 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 As Long )

Public Function Splitcmd ( Byref Argc As Long , Byref Argv () As String )
Dim Nnumargs As Long ' // Number of command line parameters
Dim Lpszarglist As Long ' // Command line parameter array address
Dim LpszargAs Long ' // Address of each command line parameter
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 ' // Total number of outputs
Redim Argv (nnumargs- 1 )
Copymemory Byval Varptr (lpszarg ),Byval Lpszarglist, 4 ' // Obtain the argv (0) address.

For I = 0 To Nnumargs- 1
Narglength = lstrlenw (lpszarg)
Redim Szarg (narglength * 2 - 1 )
CopymemoryByval 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 call method is simple. The entry function splitcmd (ByrefArgcAs Long,ByrefArgv ()As String), The first parameter is the input parameter: the total number of parameters, and the second parameter is the parameter array. It must be noted that the return value of this function is the same as that of C, there must be at least one value in argc, that isProgramThe argv (0) is the path of the program. The following describes how to use argv (0.

 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

The result is as follows:

Now, everything goes smoothly.
Girl does not cry (QQ: 191035066) @

Related Article

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.