[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.)
Re: Show All String Characters
It will show what is being passed to your application.
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.
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.
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:
q = Chr(34)
d = " "
myarr = Split(Command, d)
For i = 0 To UBound(myarr)
If i > UBound(myarr) Then Exit For
If InStr(myarr(i), q) > 0 Then
j = 1
Do
myarr(i) = myarr(i) & d & myarr(i + j)
If InStr(myarr(i + j), q) > 0 Then
myarr(i) = Replace(myarr(i), q, "")
For k = i + 1 To UBound(myarr) - i
myarr(k) = myarr(k + j)
Next
ReDim Preserve myarr(k - 1)
Exit Do
End If
j = j + 1
Loop
End If
Next
the other i did, was to do exactly what you are wanting and checked for valid files, do search on command
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.
Re: Show All String Characters
Quote:
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
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.
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.