Results 1 to 9 of 9

Thread: [RESOLVED] Show All String Characters

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Resolved [RESOLVED] Show All String Characters

    I'm getting a directory/file string from the Command line which is being passed to VB5 from Explorer. When I try to use the string to Open a file it fails.
    (BTW, the Open works when I hard code the string within the program).

    Is there anyway to show all strings characters to see what I've got
    and /or what type of string it is (BSTR, ends with /0, etc.)

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Show All String Characters

    Code:
    MsgBox Command$
    It will show what is being passed to your application.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Show All String Characters

    baja_yu

    Thanks for response.

    Already putting "Command$" string to Label and displaying it.

    I'm getting a quote showing up at front end of string from "Command$"
    but when string is hard coded into program and also displayed in label,
    any string delimiters (quotes) are removed.

    Obviously something is different with the strings.

    My Understanding -- probably wrong - is that VB uses BSTR strings and Windows uses
    some other string type (probably plain ASCII). Can edit the "Command$" string
    and make it correct, but would like to know why the difference.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Show All String Characters

    Displaying the Command path in a label and comparing it to another label which displays a hard coded string of the Path,
    the string returned from Command path definitely has extra quotes around it.
    Wrote a proc to remove beginning and ending quotes, but program now locks solid when I try and
    use the edited Command path.

    Back to the drawing board.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Show All String Characters

    explorer will append (& prepend) quotes to all filepaths containing spaces (only), these will be in your command string

    if you are passing multiple files, you can not split on space alone (unless you are sure that there are no spaces within the filepath)

    i have posted 2 slightly different codes in the forums, recently, which should be able to return the filenames from the command string, i will see if i can find either

    this example is not specifically for command string, but should work
    vb Code:
    1. q = Chr(34)
    2. d = " "
    3. myarr = Split(Command, d)
    4. For i = 0 To UBound(myarr)
    5.     If i > UBound(myarr) Then Exit For
    6.     If InStr(myarr(i), q) > 0 Then
    7.         j = 1
    8.         Do
    9.             myarr(i) = myarr(i) & d & myarr(i + j)
    10.             If InStr(myarr(i + j), q) > 0 Then
    11.                 myarr(i) = Replace(myarr(i), q, "")  
    12.                 For k = i + 1 To UBound(myarr) - i
    13.                     myarr(k) = myarr(k + j)
    14.                 Next
    15.                 ReDim Preserve myarr(k - 1)
    16.                 Exit Do
    17.             End If
    18.             j = j + 1
    19.         Loop
    20.     End If
    21. Next

    the other i did, was to do exactly what you are wanting and checked for valid files, do search on command
    Last edited by westconn1; Aug 7th, 2010 at 11:55 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Show All String Characters

    Here is a Unicode Command$ variant. It works a bit differently: it returns a number of parameters instead the full string, and you need to pass an empty string array which will receive the parameters.

    Code:
    Option Explicit
    
    Private Declare Function CommandLineToArgvW Lib "shell32" (ByVal lpCmdLine As Long, pNumArgs As Integer) As Long
    Private Declare Function GetCommandLineW Lib "kernel32" () As Long
    Private Declare Sub GetMem4 Lib "msvbvm60" (ByVal Ptr As Long, Value As Long)
    Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As Long, ByVal Value As Long)
    Private Declare Function SysAllocStringLen Lib "oleaut32" (ByVal Ptr As Long, ByVal Length As Long) As Long
    
    Public Function Command(Parameters() As String, Optional EXE As String) As Long
        Dim A As Integer, I As Long, Ptr As Long, Pos As Long
        If Not Not Parameters Then Erase Parameters
        Debug.Assert App.hInstance
        Parameters = VBA.Split(vbNullString)
        Ptr = CommandLineToArgvW(GetCommandLineW, A)
        If Ptr <> 0 And A > 0 Then
            GetMem4 Ptr, Pos
            PutMem4 VarPtr(EXE), SysAllocStringLen(Pos, lstrlenW(Pos))
            If A > 1 Then
                ReDim Parameters(0 To A - 2)
                For I = Ptr + 4 To Ptr + (A - 1) * 4 Step 4
                    GetMem4 I, Pos
                    PutMem4 VarPtr(Parameters(Command)), SysAllocStringLen(Pos, lstrlenW(Pos))
                    Command = Command + 1
                Next I
            End If
            LocalFree Ptr
        End If
    End Function
    The advantage is that quoted filenames are automatically taken into account so you don't need to care about quotes in your own code. It parses the command line parameters as any Windows application should.

    Also note that opening a file using VB6 file functions will fail if the filename contains any character that is not found in the current locale's ANSI character set. You need to use Unicode file API to be able to open filenames with any character.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Show All String Characters

    It parses the command line parameters as any Windows application should.
    i was surprised there was not some simple method to parse the command line, without having to resort to methods like above, but had not previously, found any reference to those APIs
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Show All String Characters

    Yeah, well, it is done the C/C++ way. And from what I've read in C/C++ they have other easier means available as well. Argv gives a challenging return value. Anyway, the good thing is that there is atleast something available that can be used via VB6 as well.

    If you only want the complete string to work with you can use GetCommandLineW, create a BSTR out of it and parse it manually.

    Code:
    Option Explicit
    
    Private Declare Function GetCommandLineW Lib "kernel32" () As Long
    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As Long, ByVal Value As Long)
    Private Declare Function SysAllocStringLen Lib "oleaut32" (ByVal Ptr As Long, ByVal Length As Long) As Long
    
    Public Function CommandW() As String
        Dim Ptr As Long: Ptr = GetCommandLineW
        If Ptr Then
            PutMem4 VarPtr(CommandW), SysAllocStringLen(Ptr, lstrlenW(Ptr))
            If AscW(CommandW) = 34 Then
                CommandW = Mid$(CommandW, InStr(CommandW, """ ") + 2)
            Else
                CommandW = Mid$(CommandW, InStr(CommandW, " ") + 1)
            End If
        End If
    End Function
    If you wonder why I use Mid$ to cut the string it is because the first part of the string contains the full path to the executable. Something we don't want with VB6 compatibility.


    Edit!
    And yes, GetCommandLineW does contain a trailing space even when there are no parameters given at all. I didn't check whether there is always a trailing space character.
    Last edited by Merri; Aug 8th, 2010 at 04:11 AM.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Show All String Characters

    First let me thank everyone for their assistance. Greatly appreciated.

    For what I thought would take me an hour to write a simple program to substitute the
    current folder on my system for the embedded folder associated with a image on
    a downloaded web page has turned into a good reminder of "Murray's Law".

    That said, it appears I may be dealing with two issues.

    1) Getting a file string from Explorer from VB using the Command line function where the file name contains spaces.

    In this instance stripping the quotes from the beginning and end of the file name that is returned on the
    command line "appears" (juries still out) to resolve the issue.


    Code:
       
    
       strExplorerPath = Command$()
       
        
       'NOTE:
       'The Command Line returns a string with quotes on
       'beginning and end.  These need to be removed otherwise
       'fails as string
       'Strip right quote character
       If Right$(strExplorerPath, 1) = Chr$(34) Then
          strExplorerPath = Left$(strExplorerPath, Len(strExplorerPath) - 1)
       End If
       
       'Strip left quote character
       If Left$(strExplorerPath, 1) = Chr$(34) Then
          strExplorerPath = Right$(strExplorerPath, Len(strExplorerPath) - 1)
       End If
    2) The second issue:
    I just happened to download a second web page to use for testing
    that contains some type of scheme to keep a search/replace program from altering the
    htm file (my luck).
    So I need to spend some time figuring out what was down and how to beat it as I'm sure
    it may be used more than just the page I picked.

    Consequently, I am closing this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width