The code will not work?

  • Thread starter Thread starter YXQ
  • Start date Start date
Y

YXQ

I want to get image path(c:\windows\system\regsvr32.exe) from full
path(c:\windows\system\regsvr32.exe /u aaa.dll) using API PathRemoveArgsA,
but the function returned args(/u aaa.dll) but no the image
path(c:\windows\system\regsvr32.exe) on Windows Vista. And the function
PathGetArgs returned also args(/u aaa.dll), why? the code is below:
///////////////////////////////////////////////////////
Private Declare Function PathRemoveArgs Lib "shlwapi.dll" Alias
"PathRemoveArgsA" (ByVal pszPath As String) As String

Private Declare Function PathGetArgs Lib "shlwapi.dll" Alias
"PathGetArgsA" (ByVal pszPath As String) As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strOutBuffer As String = "c:\windows\system\regsvr32.exe /u
aaa.dll"
MessageBox.Show(PathRemoveArgs(strOutBuffer))

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim strOutBuffer As String = "c:\windows\system\regsvr32.exe /u
aaa.dll"
MessageBox.Show(PathGetArgs(strOutBuffer))
End Sub
 
YXQ said:
I want to get image path(c:\windows\system\regsvr32.exe) from full
path(c:\windows\system\regsvr32.exe /u aaa.dll) using API PathRemoveArgsA,

Given that you're asking in a VB[.Net] group, I have to ask - why?
The API is a painful way to do anything, as you've discovered,
especially as, I would argue, you probably don't need to use it in the
first place.

I'm guessing that you're trying to separate Program and Arguments so
that you can feed them into the Process class[es] to actually run this
program. If so, then you need Program and Arguments separately, so is
there no way that you can /store/ them separately? Even, say, a tab
character between the two would be enough.

Dim sCmdLine as String = "C:\windows\system\regsvr32.exe<TAB>/u aaa.dll"

Dim sLines as String() = sCmdLine.Split( "<TAB>" )
Dim starter as new ProcessStartInfo( sLines(0), sLines(1) )
Dim runner as new Process
runner.StartInfo = starter

runner.Start()

HTH,
Phill W.
 
[Address to the OP yxq] Given that you're asking in a
VB[.Net] group, I have to ask - why? The API is a
painful way to do anything, as you've discovered . . .

No wonder Micro$oft's official description of VB.Net is that it is a
programming tool "for beginners and casual programmers" ;-)

Mike
 
yxq said:
The API function is weak, it can not get the arg from the path below.
"C:\Program Files\Adobe\Photoshop CS\Photoshop.exe,57"

The documentation
(http://msdn.microsoft.com/en-us/library/bb773602(VS.85).aspx) says that
PathGetArgs should not be used unless you know the string to be well formed.
That doesn't look like a good commandline.

Perhaps you should look at using Path.GetInvalidFileNameChars and
Path.GetInvalidPathChars to find the end of the usable filename in the string.

Mike
 
The API function is weak, it can not get the arg from the path below.
"C:\Program Files\Adobe\Photoshop CS\Photoshop.exe,57"

That's because "exe,57" is a perfectly valid file extension.

Mike
 
Michael Williams said:
[Address to the OP yxq] Given that you're asking in a
VB[.Net] group, I have to ask - why? The API is a
painful way to do anything, as you've discovered . . .

No wonder Micro$oft's official description of VB.Net is that it is a
programming tool "for beginners and casual programmers" ;-)

Mike

Mike, is it really necessary to come here to stir things up? You do a good
enough job in the classic group that there's no need to expand into this
one.

By you own admission you don't and won't use the product, so you are just
trolling.
 
Hello YXQ,

instead of using an API call, you could do it like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles Button1.Click
'Without quotes and with argument
Const CmdLine1 As String = "C:\Windows\system32\regsvr32.exe testme.dll"

'Without quotes and without argument
Const CmdLine2 As String = "C:\autoexec.bat"

'With Quotes and argument
Const CmdLine3 As String = Chr(34) & _
"C:\Program files\TestProgram\Test\abc.exe" & Chr(34) & " /Quiet"

'With Quotes and without argument
Const CmdLine4 As String = Chr(34) & _
"C:\Program files\TestProgram\Test\def.exe" & Chr(34)

Dim ProgramPath As String = ""
Dim ProgramArguments As String = ""

SeparateProgramArguments(CmdLine1, ProgramPath, ProgramArguments)
MsgBox(ProgramPath & vbCrLf & ProgramArguments, MsgBoxStyle.Information)

SeparateProgramArguments(CmdLine2, ProgramPath, ProgramArguments)
MsgBox(ProgramPath & vbCrLf & ProgramArguments, MsgBoxStyle.Information)

SeparateProgramArguments(CmdLine3, ProgramPath, ProgramArguments)
MsgBox(ProgramPath & vbCrLf & ProgramArguments, MsgBoxStyle.Information)

SeparateProgramArguments(CmdLine4, ProgramPath, ProgramArguments)
MsgBox(ProgramPath & vbCrLf & ProgramArguments, MsgBoxStyle.Information)
End Sub

Private Sub SeparateProgramArguments(ByVal CmdLine As String, _
ByRef ProgramPath As String, ByRef ProgramArguments As String)
'Reset Variables
ProgramPath = ""
ProgramArguments = ""

'Check if the command line contains something
If Len(CmdLine) > 0 Then

'Look for quotes (paths with spaces need them)
Dim PosiQuotes As Integer = InStrRev(CmdLine, Chr(34))

'Look for last space
Dim PosiSpace As Integer = InStrRev(CmdLine, " ")

'If there is only a quote at first position, it is an
'invalid path.
If PosiQuotes > 1 Then
'Valid path with quotes: Check if there is a space
'following the quote (=argument present).
If PosiQuotes < PosiSpace Then
ProgramPath = Strings.Left(CmdLine, PosiQuotes)
ProgramArguments = Strings.Right(CmdLine, _
Len(CmdLine) - PosiQuotes)
Else

'No space? Then there is no argument.
ProgramPath = CmdLine
End If
Else

'No quotes? Then this is a path without spaces in it.
Dim TempCmdLine As String = CmdLine

'Remove all strings following a space until
'there are no more left.
While PosiSpace > 0
TempCmdLine = Strings.Left(TempCmdLine, _
PosiSpace - 1)
PosiSpace = InStrRev(TempCmdLine, " ")
End While

'The remaining string is the Path to the program.
ProgramPath = TempCmdLine

'If anything remains, it must be the arguments.
ProgramArguments = Strings.Right(CmdLine, _
Len(CmdLine) - Len(TempCmdLine))
End If

'LTrim arguments as they should have no preceding space.
ProgramArguments = LTrim(ProgramArguments)
End If
End Sub



Best wishes,

Martin
 
Mike, is it really necessary to come here to stir things up?

No. In fact it's not necessary for me to be here at all, no more than it is
necessary for Bill McCarthy and Cor Ligthert to troll the Classic VB
newsgroup.

Mike
 
Michael Williams said:
No. In fact it's not necessary for me to be here at all, no more than it
is necessary for Bill McCarthy and Cor Ligthert to troll the Classic VB
newsgroup.

Mike

Well, then I give you credit for admitting that you are here to troll. Kind
of puts you in the same class as those you rail against, though.
 
Well, then I give you credit for admitting that
you are here to troll.

I didn't admit that at all. You merely inferred it from what I did say, and
of course you are free to infer whatever you wish.
Kind of puts you in the same class as those
you rail against, though.

Well, then I give you credit for admitting that Bill McCarthy and Cor
Ligthert are trolls.

Mike
 
Back
Top