473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

IsFile, isURL

I have a program that I want to launch links if certain criteria is met, the
problem is how can I determine if a string is a program to launch, here is
an example
with quotes->
"c:\Program Files\Windows Media Player\wmplayer.exe"
"c:\program files\windows media player\mplayer2.exe" /open
"http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
http://myfavoritepages

it won't always be c:\ drive and sometimes it might be a ftp link below is
currently what i'm doing

Function isURL(ByVal kcString As String)
Dim kcFound As String = kcString.IndexOf("://")
If kcFound > 0 Then
Return True
Else
Return False
End If
End Function

Function isFile(ByVal kcString As String)
Dim kcFound As String = kcString.IndexOf(":\")
If kcFound > 0 Then
Return True
Else
Return False
End If
End Function

Nov 21 '05 #1
7 4095
C.A. Kelly,
Have you looked at either the System.IO.Path or System.Uri classes?

I believe just Uri itself can be used. Something like:

Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
"CommandLineToArgvW"
(<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal commandLine
As String, <Out()> ByRef numberArgs As Integer) As IntPtr

Public Shared Function ParseCommandLine(ByVal commandLine As String) As
String()
Dim count As Integer
Dim ptr As IntPtr

ptr = CommandLineToArgv(commandLine, count)

Dim args(count - 1) As String

For index As Integer = 0 To count - 1
Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
IntPtr.Size)
Dim str As String = Marshal.PtrToStringUni(ptrStr)
args(index) = str
Next
Return args
End Function

Public Shared Function IsURL(ByVal kcString As String) As Boolean
Dim kcUri As New Uri(kcString)
Return kcUri.Scheme <> Uri.UriSchemeFile
End Function

Public Shared Function IsFile(ByVal kcString As String) As Boolean
Dim kcUri As New Uri(kcString)
Return kcUri.Scheme = Uri.UriSchemeFile
End Function

Public Shared Sub Main()
Dim commandLines() As String = {"""c:\Program Files\Windows Media
Player\wmplayer.exe""", _
"""c:\program files\windows media player\mplayer2.exe"" /open",
_
"""http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
/play http://myfavoritepages"}

For Each commandLine As String In commandLines
Dim args() As String = ParseCommandLine(commandLine)
Debug.WriteLine(commandLine, "commandLine")
Debug.Indent()
Debug.WriteLine(IsURL(args(0)), "isUrl")
Debug.WriteLine(IsFile(args(0)), "isFile")
For Each arg As String In args
Debug.WriteLine(arg, "arg")
Next
Debug.Unindent()
Next
End Sub

NOTE: I use the CommandLineToArgvW Win32 API along with the ParseCommandLine
function to parse the string into individual arguments.

Hope this helps
Jay

"C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
news:NZ********************@comcast.com...
|I have a program that I want to launch links if certain criteria is met,
the
| problem is how can I determine if a string is a program to launch, here is
| an example
| with quotes->
| "c:\Program Files\Windows Media Player\wmplayer.exe"
| "c:\program files\windows media player\mplayer2.exe" /open
| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
| http://myfavoritepages
|
| it won't always be c:\ drive and sometimes it might be a ftp link below is
| currently what i'm doing
|
| Function isURL(ByVal kcString As String)
| Dim kcFound As String = kcString.IndexOf("://")
| If kcFound > 0 Then
| Return True
| Else
| Return False
| End If
| End Function
|
| Function isFile(ByVal kcString As String)
| Dim kcFound As String = kcString.IndexOf(":\")
| If kcFound > 0 Then
| Return True
| Else
| Return False
| End If
| End Function
|
|
|
Nov 21 '05 #2
Doh!

That's going to have a slow unmanaged memory leak.

I should be calling LocalFree on the return value from CommandLineToArgvW.

| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| "CommandLineToArgvW"
| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
commandLine
| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
|

Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As IntPtr) As
IntPtr
| Public Shared Function ParseCommandLine(ByVal commandLine As String) As
| String()
| Dim count As Integer
| Dim ptr As IntPtr
| ptr = CommandLineToArgv(commandLine, count)
| Dim args(count - 1) As String
| For index As Integer = 0 To count - 1
| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| IntPtr.Size)
| Dim str As String = Marshal.PtrToStringUni(ptrStr)
| args(index) = str
| Next

LocalFree(ptr)

| Return args
| End Function

I would consider wrapping the For Loop in a Try/Finally block to ensure that
LocalFree was called even in an exception occurred...

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
| C.A. Kelly,
| Have you looked at either the System.IO.Path or System.Uri classes?
|
| I believe just Uri itself can be used. Something like:
|
| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| "CommandLineToArgvW"
| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
commandLine
| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
|
| Public Shared Function ParseCommandLine(ByVal commandLine As String) As
| String()
| Dim count As Integer
| Dim ptr As IntPtr
|
| ptr = CommandLineToArgv(commandLine, count)
|
| Dim args(count - 1) As String
|
| For index As Integer = 0 To count - 1
| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| IntPtr.Size)
| Dim str As String = Marshal.PtrToStringUni(ptrStr)
| args(index) = str
| Next
| Return args
| End Function
|
| Public Shared Function IsURL(ByVal kcString As String) As Boolean
| Dim kcUri As New Uri(kcString)
| Return kcUri.Scheme <> Uri.UriSchemeFile
| End Function
|
| Public Shared Function IsFile(ByVal kcString As String) As Boolean
| Dim kcUri As New Uri(kcString)
| Return kcUri.Scheme = Uri.UriSchemeFile
| End Function
|
| Public Shared Sub Main()
| Dim commandLines() As String = {"""c:\Program Files\Windows Media
| Player\wmplayer.exe""", _
| """c:\program files\windows media player\mplayer2.exe"" /open",
| _
|
"""http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
| /play http://myfavoritepages"}
|
| For Each commandLine As String In commandLines
| Dim args() As String = ParseCommandLine(commandLine)
| Debug.WriteLine(commandLine, "commandLine")
| Debug.Indent()
| Debug.WriteLine(IsURL(args(0)), "isUrl")
| Debug.WriteLine(IsFile(args(0)), "isFile")
| For Each arg As String In args
| Debug.WriteLine(arg, "arg")
| Next
| Debug.Unindent()
| Next
| End Sub
|
| NOTE: I use the CommandLineToArgvW Win32 API along with the
ParseCommandLine
| function to parse the string into individual arguments.
|
| Hope this helps
| Jay
|
| "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
| news:NZ********************@comcast.com...
||I have a program that I want to launch links if certain criteria is met,
| the
|| problem is how can I determine if a string is a program to launch, here
is
|| an example
|| with quotes->
|| "c:\Program Files\Windows Media Player\wmplayer.exe"
|| "c:\program files\windows media player\mplayer2.exe" /open
|| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
|| http://myfavoritepages
||
|| it won't always be c:\ drive and sometimes it might be a ftp link below
is
|| currently what i'm doing
||
|| Function isURL(ByVal kcString As String)
|| Dim kcFound As String = kcString.IndexOf("://")
|| If kcFound > 0 Then
|| Return True
|| Else
|| Return False
|| End If
|| End Function
||
|| Function isFile(ByVal kcString As String)
|| Dim kcFound As String = kcString.IndexOf(":\")
|| If kcFound > 0 Then
|| Return True
|| Else
|| Return False
|| End If
|| End Function
||
||
||
|
|
Nov 21 '05 #3
Hmm...

FYI:

Reading Adam Nathan's ".NET and COM - The Complete Interoperability Guide",
I believe I could have simply used Marshal.FreeHGlobal, instead of declaring
the LocalFree function. As FreeHGlobal calls the GlobalFree Win32 API, which
under Win32 does the "same thing" as LocalFree Win32 API...

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey****************@tk2msftngp13.phx.gbl...
| Doh!
|
| That's going to have a slow unmanaged memory leak.
|
| I should be calling LocalFree on the return value from CommandLineToArgvW.
|
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|
| Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As IntPtr)
As
| IntPtr
|
|
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
|| ptr = CommandLineToArgv(commandLine, count)
|| Dim args(count - 1) As String
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|
| LocalFree(ptr)
|
|| Return args
|| End Function
|
| I would consider wrapping the For Loop in a Try/Finally block to ensure
that
| LocalFree was called even in an exception occurred...
|
| Hope this helps
| Jay
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:uB**************@TK2MSFTNGP10.phx.gbl...
|| C.A. Kelly,
|| Have you looked at either the System.IO.Path or System.Uri classes?
||
|| I believe just Uri itself can be used. Something like:
||
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
||
|| ptr = CommandLineToArgv(commandLine, count)
||
|| Dim args(count - 1) As String
||
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|| Return args
|| End Function
||
|| Public Shared Function IsURL(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme <> Uri.UriSchemeFile
|| End Function
||
|| Public Shared Function IsFile(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme = Uri.UriSchemeFile
|| End Function
||
|| Public Shared Sub Main()
|| Dim commandLines() As String = {"""c:\Program Files\Windows Media
|| Player\wmplayer.exe""", _
|| """c:\program files\windows media player\mplayer2.exe""
/open",
|| _
||
| """http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
|| /play http://myfavoritepages"}
||
|| For Each commandLine As String In commandLines
|| Dim args() As String = ParseCommandLine(commandLine)
|| Debug.WriteLine(commandLine, "commandLine")
|| Debug.Indent()
|| Debug.WriteLine(IsURL(args(0)), "isUrl")
|| Debug.WriteLine(IsFile(args(0)), "isFile")
|| For Each arg As String In args
|| Debug.WriteLine(arg, "arg")
|| Next
|| Debug.Unindent()
|| Next
|| End Sub
||
|| NOTE: I use the CommandLineToArgvW Win32 API along with the
| ParseCommandLine
|| function to parse the string into individual arguments.
||
|| Hope this helps
|| Jay
||
|| "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
|| news:NZ********************@comcast.com...
|||I have a program that I want to launch links if certain criteria is met,
|| the
||| problem is how can I determine if a string is a program to launch, here
| is
||| an example
||| with quotes->
||| "c:\Program Files\Windows Media Player\wmplayer.exe"
||| "c:\program files\windows media player\mplayer2.exe" /open
||| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
||| http://myfavoritepages
|||
||| it won't always be c:\ drive and sometimes it might be a ftp link below
| is
||| currently what i'm doing
|||
||| Function isURL(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf("://")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
||| Function isFile(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf(":\")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
|||
|||
||
||
|
|
Nov 21 '05 #4
Thank you for your input, I will be trying yours and a couple of other
techniques to see what works best.
Thank you for your comments.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OC**************@TK2MSFTNGP14.phx.gbl...
Hmm...

FYI:

Reading Adam Nathan's ".NET and COM - The Complete Interoperability Guide",
I believe I could have simply used Marshal.FreeHGlobal, instead of declaring
the LocalFree function. As FreeHGlobal calls the GlobalFree Win32 API, which
under Win32 does the "same thing" as LocalFree Win32 API...

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey****************@tk2msftngp13.phx.gbl...
| Doh!
|
| That's going to have a slow unmanaged memory leak.
|
| I should be calling LocalFree on the return value from CommandLineToArgvW.
|
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|
| Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As IntPtr)
As
| IntPtr
|
|
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
|| ptr = CommandLineToArgv(commandLine, count)
|| Dim args(count - 1) As String
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|
| LocalFree(ptr)
|
|| Return args
|| End Function
|
| I would consider wrapping the For Loop in a Try/Finally block to ensure
that
| LocalFree was called even in an exception occurred...
|
| Hope this helps
| Jay
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:uB**************@TK2MSFTNGP10.phx.gbl...
|| C.A. Kelly,
|| Have you looked at either the System.IO.Path or System.Uri classes?
||
|| I believe just Uri itself can be used. Something like:
||
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
||
|| ptr = CommandLineToArgv(commandLine, count)
||
|| Dim args(count - 1) As String
||
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|| Return args
|| End Function
||
|| Public Shared Function IsURL(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme <> Uri.UriSchemeFile
|| End Function
||
|| Public Shared Function IsFile(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme = Uri.UriSchemeFile
|| End Function
||
|| Public Shared Sub Main()
|| Dim commandLines() As String = {"""c:\Program Files\Windows Media
|| Player\wmplayer.exe""", _
|| """c:\program files\windows media player\mplayer2.exe""
/open",
|| _
||
| """http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
|| /play http://myfavoritepages"}
||
|| For Each commandLine As String In commandLines
|| Dim args() As String = ParseCommandLine(commandLine)
|| Debug.WriteLine(commandLine, "commandLine")
|| Debug.Indent()
|| Debug.WriteLine(IsURL(args(0)), "isUrl")
|| Debug.WriteLine(IsFile(args(0)), "isFile")
|| For Each arg As String In args
|| Debug.WriteLine(arg, "arg")
|| Next
|| Debug.Unindent()
|| Next
|| End Sub
||
|| NOTE: I use the CommandLineToArgvW Win32 API along with the
| ParseCommandLine
|| function to parse the string into individual arguments.
||
|| Hope this helps
|| Jay
||
|| "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
|| news:NZ********************@comcast.com...
|||I have a program that I want to launch links if certain criteria is met,
|| the
||| problem is how can I determine if a string is a program to launch, here
| is
||| an example
||| with quotes->
||| "c:\Program Files\Windows Media Player\wmplayer.exe"
||| "c:\program files\windows media player\mplayer2.exe" /open
||| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
||| http://myfavoritepages
|||
||| it won't always be c:\ drive and sometimes it might be a ftp link below
| is
||| currently what i'm doing
|||
||| Function isURL(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf("://")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
||| Function isFile(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf(":\")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
|||
|||
||
||
|
|

Nov 21 '05 #5
I didn't spend enough time staring at the code to understand why you go to
all the work of calling the base Windows API to fetch the command line
arguments rather than simply calling Environment.GetCommandLineArgs, but...
I'm intrigued by the comment about LocalFree vs. Marshal.FreeHGlobal. I
have code that calls the Windows FormatMessage function with arguments that
cause FormatMessage to allocate a buffer on my behalf. The docs state that
I should call LocalFree to free the buffer.

Can someone please confirm that Marshal.FreeHGlobal is guranteed to work in
place of a p/invoke call to LocalFree. TIA!

- Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OC**************@TK2MSFTNGP14.phx.gbl...
Hmm...

FYI:

Reading Adam Nathan's ".NET and COM - The Complete Interoperability Guide", I believe I could have simply used Marshal.FreeHGlobal, instead of declaring the LocalFree function. As FreeHGlobal calls the GlobalFree Win32 API, which under Win32 does the "same thing" as LocalFree Win32 API...

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ey****************@tk2msftngp13.phx.gbl...
| Doh!
|
| That's going to have a slow unmanaged memory leak.
|
| I should be calling LocalFree on the return value from CommandLineToArgvW. |
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|
| Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As IntPtr)
As
| IntPtr
|
|
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
|| ptr = CommandLineToArgv(commandLine, count)
|| Dim args(count - 1) As String
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|
| LocalFree(ptr)
|
|| Return args
|| End Function
|
| I would consider wrapping the For Loop in a Try/Finally block to ensure
that
| LocalFree was called even in an exception occurred...
|
| Hope this helps
| Jay
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message | news:uB**************@TK2MSFTNGP10.phx.gbl...
|| C.A. Kelly,
|| Have you looked at either the System.IO.Path or System.Uri classes?
||
|| I believe just Uri itself can be used. Something like:
||
|| Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
|| "CommandLineToArgvW"
|| (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| commandLine
|| As String, <Out()> ByRef numberArgs As Integer) As IntPtr
||
|| Public Shared Function ParseCommandLine(ByVal commandLine As String)
As
|| String()
|| Dim count As Integer
|| Dim ptr As IntPtr
||
|| ptr = CommandLineToArgv(commandLine, count)
||
|| Dim args(count - 1) As String
||
|| For index As Integer = 0 To count - 1
|| Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
|| IntPtr.Size)
|| Dim str As String = Marshal.PtrToStringUni(ptrStr)
|| args(index) = str
|| Next
|| Return args
|| End Function
||
|| Public Shared Function IsURL(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme <> Uri.UriSchemeFile
|| End Function
||
|| Public Shared Function IsFile(ByVal kcString As String) As Boolean
|| Dim kcUri As New Uri(kcString)
|| Return kcUri.Scheme = Uri.UriSchemeFile
|| End Function
||
|| Public Shared Sub Main()
|| Dim commandLines() As String = {"""c:\Program Files\Windows Media || Player\wmplayer.exe""", _
|| """c:\program files\windows media player\mplayer2.exe""
/open",
|| _
||
| """http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
|| /play http://myfavoritepages"}
||
|| For Each commandLine As String In commandLines
|| Dim args() As String = ParseCommandLine(commandLine)
|| Debug.WriteLine(commandLine, "commandLine")
|| Debug.Indent()
|| Debug.WriteLine(IsURL(args(0)), "isUrl")
|| Debug.WriteLine(IsFile(args(0)), "isFile")
|| For Each arg As String In args
|| Debug.WriteLine(arg, "arg")
|| Next
|| Debug.Unindent()
|| Next
|| End Sub
||
|| NOTE: I use the CommandLineToArgvW Win32 API along with the
| ParseCommandLine
|| function to parse the string into individual arguments.
||
|| Hope this helps
|| Jay
||
|| "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
|| news:NZ********************@comcast.com...
|||I have a program that I want to launch links if certain criteria is met, || the
||| problem is how can I determine if a string is a program to launch, here | is
||| an example
||| with quotes->
||| "c:\Program Files\Windows Media Player\wmplayer.exe"
||| "c:\program files\windows media player\mplayer2.exe" /open
||| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364" /play
||| http://myfavoritepages
|||
||| it won't always be c:\ drive and sometimes it might be a ftp link below | is
||| currently what i'm doing
|||
||| Function isURL(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf("://")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
||| Function isFile(ByVal kcString As String)
||| Dim kcFound As String = kcString.IndexOf(":\")
||| If kcFound > 0 Then
||| Return True
||| Else
||| Return False
||| End If
||| End Function
|||
|||
|||
||
||
|
|

Nov 21 '05 #6
Bob,
|I didn't spend enough time staring at the code to understand why you go to
| all the work of calling the base Windows API to fetch the command line
| arguments rather than simply calling Environment.GetCommandLineArgs,
Rather then user Environment.GetCommandLineArgs I normally simply add
parameters to my Sub Main, something like:

Public Sub Main(ByVal args() as String)

I was using the Windows API as the original poster stated "launch links if
certain criteria is met", notice the plural "links", which to me suggests
that he is getting the "links" aka "command lines" from someplace other then
the command line to the program itself. Such as reading them from a file.

| Can someone please confirm that Marshal.FreeHGlobal is guranteed to work
in
| place of a p/invoke call to LocalFree. TIA!
I will see what I can find out.

Hope this helps
Jay
"Bob Altman" <rd*@nospam.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl...
|I didn't spend enough time staring at the code to understand why you go to
| all the work of calling the base Windows API to fetch the command line
| arguments rather than simply calling Environment.GetCommandLineArgs,
but...
| I'm intrigued by the comment about LocalFree vs. Marshal.FreeHGlobal. I
| have code that calls the Windows FormatMessage function with arguments
that
| cause FormatMessage to allocate a buffer on my behalf. The docs state
that
| I should call LocalFree to free the buffer.
|
| Can someone please confirm that Marshal.FreeHGlobal is guranteed to work
in
| place of a p/invoke call to LocalFree. TIA!
|
| - Bob
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:OC**************@TK2MSFTNGP14.phx.gbl...
| > Hmm...
| >
| > FYI:
| >
| > Reading Adam Nathan's ".NET and COM - The Complete Interoperability
| Guide",
| > I believe I could have simply used Marshal.FreeHGlobal, instead of
| declaring
| > the LocalFree function. As FreeHGlobal calls the GlobalFree Win32 API,
| which
| > under Win32 does the "same thing" as LocalFree Win32 API...
| >
| > Jay
| >
| > "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
| > news:ey****************@tk2msftngp13.phx.gbl...
| > | Doh!
| > |
| > | That's going to have a slow unmanaged memory leak.
| > |
| > | I should be calling LocalFree on the return value from
| CommandLineToArgvW.
| > |
| > || Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| > || "CommandLineToArgvW"
| > || (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| > | commandLine
| > || As String, <Out()> ByRef numberArgs As Integer) As IntPtr
| > ||
| > |
| > | Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As
IntPtr)
| > As
| > | IntPtr
| > |
| > |
| > || Public Shared Function ParseCommandLine(ByVal commandLine As
String)
| > As
| > || String()
| > || Dim count As Integer
| > || Dim ptr As IntPtr
| > || ptr = CommandLineToArgv(commandLine, count)
| > || Dim args(count - 1) As String
| > || For index As Integer = 0 To count - 1
| > || Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| > || IntPtr.Size)
| > || Dim str As String = Marshal.PtrToStringUni(ptrStr)
| > || args(index) = str
| > || Next
| > |
| > | LocalFree(ptr)
| > |
| > || Return args
| > || End Function
| > |
| > | I would consider wrapping the For Loop in a Try/Finally block to
ensure
| > that
| > | LocalFree was called even in an exception occurred...
| > |
| > | Hope this helps
| > | Jay
| > |
| > | "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
| message
| > | news:uB**************@TK2MSFTNGP10.phx.gbl...
| > || C.A. Kelly,
| > || Have you looked at either the System.IO.Path or System.Uri classes?
| > ||
| > || I believe just Uri itself can be used. Something like:
| > ||
| > || Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| > || "CommandLineToArgvW"
| > || (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| > | commandLine
| > || As String, <Out()> ByRef numberArgs As Integer) As IntPtr
| > ||
| > || Public Shared Function ParseCommandLine(ByVal commandLine As
String)
| > As
| > || String()
| > || Dim count As Integer
| > || Dim ptr As IntPtr
| > ||
| > || ptr = CommandLineToArgv(commandLine, count)
| > ||
| > || Dim args(count - 1) As String
| > ||
| > || For index As Integer = 0 To count - 1
| > || Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| > || IntPtr.Size)
| > || Dim str As String = Marshal.PtrToStringUni(ptrStr)
| > || args(index) = str
| > || Next
| > || Return args
| > || End Function
| > ||
| > || Public Shared Function IsURL(ByVal kcString As String) As Boolean
| > || Dim kcUri As New Uri(kcString)
| > || Return kcUri.Scheme <> Uri.UriSchemeFile
| > || End Function
| > ||
| > || Public Shared Function IsFile(ByVal kcString As String) As Boolean
| > || Dim kcUri As New Uri(kcString)
| > || Return kcUri.Scheme = Uri.UriSchemeFile
| > || End Function
| > ||
| > || Public Shared Sub Main()
| > || Dim commandLines() As String = {"""c:\Program Files\Windows
| Media
| > || Player\wmplayer.exe""", _
| > || """c:\program files\windows media player\mplayer2.exe""
| > /open",
| > || _
| > ||
| > | """http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
| > || /play http://myfavoritepages"}
| > ||
| > || For Each commandLine As String In commandLines
| > || Dim args() As String = ParseCommandLine(commandLine)
| > || Debug.WriteLine(commandLine, "commandLine")
| > || Debug.Indent()
| > || Debug.WriteLine(IsURL(args(0)), "isUrl")
| > || Debug.WriteLine(IsFile(args(0)), "isFile")
| > || For Each arg As String In args
| > || Debug.WriteLine(arg, "arg")
| > || Next
| > || Debug.Unindent()
| > || Next
| > || End Sub
| > ||
| > || NOTE: I use the CommandLineToArgvW Win32 API along with the
| > | ParseCommandLine
| > || function to parse the string into individual arguments.
| > ||
| > || Hope this helps
| > || Jay
| > ||
| > || "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
| > || news:NZ********************@comcast.com...
| > |||I have a program that I want to launch links if certain criteria is
| met,
| > || the
| > ||| problem is how can I determine if a string is a program to launch,
| here
| > | is
| > ||| an example
| > ||| with quotes->
| > ||| "c:\Program Files\Windows Media Player\wmplayer.exe"
| > ||| "c:\program files\windows media player\mplayer2.exe" /open
| > ||| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364"
/play
| > ||| http://myfavoritepages
| > |||
| > ||| it won't always be c:\ drive and sometimes it might be a ftp link
| below
| > | is
| > ||| currently what i'm doing
| > |||
| > ||| Function isURL(ByVal kcString As String)
| > ||| Dim kcFound As String = kcString.IndexOf("://")
| > ||| If kcFound > 0 Then
| > ||| Return True
| > ||| Else
| > ||| Return False
| > ||| End If
| > ||| End Function
| > |||
| > ||| Function isFile(ByVal kcString As String)
| > ||| Dim kcFound As String = kcString.IndexOf(":\")
| > ||| If kcFound > 0 Then
| > ||| Return True
| > ||| Else
| > ||| Return False
| > ||| End If
| > ||| End Function
| > |||
| > |||
| > |||
| > ||
| > ||
| > |
| > |
| >
| >
|
|
Nov 21 '05 #7
Reflector to the rescue! I looked at Marshal.FreeHGlobal using Lutz
Roeder's most excellent tool, and, sure enough, it just calls the native
LocalFree function.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...
Bob,
|I didn't spend enough time staring at the code to understand why you go to | all the work of calling the base Windows API to fetch the command line
| arguments rather than simply calling Environment.GetCommandLineArgs,
Rather then user Environment.GetCommandLineArgs I normally simply add
parameters to my Sub Main, something like:

Public Sub Main(ByVal args() as String)

I was using the Windows API as the original poster stated "launch links if
certain criteria is met", notice the plural "links", which to me suggests
that he is getting the "links" aka "command lines" from someplace other then the command line to the program itself. Such as reading them from a file.

| Can someone please confirm that Marshal.FreeHGlobal is guranteed to work
in
| place of a p/invoke call to LocalFree. TIA!
I will see what I can find out.

Hope this helps
Jay
"Bob Altman" <rd*@nospam.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl...
|I didn't spend enough time staring at the code to understand why you go to | all the work of calling the base Windows API to fetch the command line
| arguments rather than simply calling Environment.GetCommandLineArgs,
but...
| I'm intrigued by the comment about LocalFree vs. Marshal.FreeHGlobal. I
| have code that calls the Windows FormatMessage function with arguments
that
| cause FormatMessage to allocate a buffer on my behalf. The docs state
that
| I should call LocalFree to free the buffer.
|
| Can someone please confirm that Marshal.FreeHGlobal is guranteed to work
in
| place of a p/invoke call to LocalFree. TIA!
|
| - Bob
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message | news:OC**************@TK2MSFTNGP14.phx.gbl...
| > Hmm...
| >
| > FYI:
| >
| > Reading Adam Nathan's ".NET and COM - The Complete Interoperability
| Guide",
| > I believe I could have simply used Marshal.FreeHGlobal, instead of
| declaring
| > the LocalFree function. As FreeHGlobal calls the GlobalFree Win32 API,
| which
| > under Win32 does the "same thing" as LocalFree Win32 API...
| >
| > Jay
| >
| > "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
| > news:ey****************@tk2msftngp13.phx.gbl...
| > | Doh!
| > |
| > | That's going to have a slow unmanaged memory leak.
| > |
| > | I should be calling LocalFree on the return value from
| CommandLineToArgvW.
| > |
| > || Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| > || "CommandLineToArgvW"
| > || (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| > | commandLine
| > || As String, <Out()> ByRef numberArgs As Integer) As IntPtr
| > ||
| > |
| > | Declare Auto Function LocalFree Lib "kernel32" (ByVal hMem As
IntPtr)
| > As
| > | IntPtr
| > |
| > |
| > || Public Shared Function ParseCommandLine(ByVal commandLine As
String)
| > As
| > || String()
| > || Dim count As Integer
| > || Dim ptr As IntPtr
| > || ptr = CommandLineToArgv(commandLine, count)
| > || Dim args(count - 1) As String
| > || For index As Integer = 0 To count - 1
| > || Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| > || IntPtr.Size)
| > || Dim str As String = Marshal.PtrToStringUni(ptrStr)
| > || args(index) = str
| > || Next
| > |
| > | LocalFree(ptr)
| > |
| > || Return args
| > || End Function
| > |
| > | I would consider wrapping the For Loop in a Try/Finally block to
ensure
| > that
| > | LocalFree was called even in an exception occurred...
| > |
| > | Hope this helps
| > | Jay
| > |
| > | "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
| message
| > | news:uB**************@TK2MSFTNGP10.phx.gbl...
| > || C.A. Kelly,
| > || Have you looked at either the System.IO.Path or System.Uri classes?
| > ||
| > || I believe just Uri itself can be used. Something like:
| > ||
| > || Declare Unicode Function CommandLineToArgv Lib "shell32" Alias
| > || "CommandLineToArgvW"
| > || (<MarshalAs(Runtime.InteropServices.UnmanagedType. LPWStr)> ByVal
| > | commandLine
| > || As String, <Out()> ByRef numberArgs As Integer) As IntPtr
| > ||
| > || Public Shared Function ParseCommandLine(ByVal commandLine As
String)
| > As
| > || String()
| > || Dim count As Integer
| > || Dim ptr As IntPtr
| > ||
| > || ptr = CommandLineToArgv(commandLine, count)
| > ||
| > || Dim args(count - 1) As String
| > ||
| > || For index As Integer = 0 To count - 1
| > || Dim ptrStr As IntPtr = Marshal.ReadIntPtr(ptr, index *
| > || IntPtr.Size)
| > || Dim str As String = Marshal.PtrToStringUni(ptrStr)
| > || args(index) = str
| > || Next
| > || Return args
| > || End Function
| > ||
| > || Public Shared Function IsURL(ByVal kcString As String) As Boolean | > || Dim kcUri As New Uri(kcString)
| > || Return kcUri.Scheme <> Uri.UriSchemeFile
| > || End Function
| > ||
| > || Public Shared Function IsFile(ByVal kcString As String) As Boolean | > || Dim kcUri As New Uri(kcString)
| > || Return kcUri.Scheme = Uri.UriSchemeFile
| > || End Function
| > ||
| > || Public Shared Sub Main()
| > || Dim commandLines() As String = {"""c:\Program Files\Windows
| Media
| > || Player\wmplayer.exe""", _
| > || """c:\program files\windows media player\mplayer2.exe""
| > /open",
| > || _
| > ||
| > | """http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364""
| > || /play http://myfavoritepages"}
| > ||
| > || For Each commandLine As String In commandLines
| > || Dim args() As String = ParseCommandLine(commandLine)
| > || Debug.WriteLine(commandLine, "commandLine")
| > || Debug.Indent()
| > || Debug.WriteLine(IsURL(args(0)), "isUrl")
| > || Debug.WriteLine(IsFile(args(0)), "isFile")
| > || For Each arg As String In args
| > || Debug.WriteLine(arg, "arg")
| > || Next
| > || Debug.Unindent()
| > || Next
| > || End Sub
| > ||
| > || NOTE: I use the CommandLineToArgvW Win32 API along with the
| > | ParseCommandLine
| > || function to parse the string into individual arguments.
| > ||
| > || Hope this helps
| > || Jay
| > ||
| > || "C. A. Kelly" <ca*****@kellcomnet.us> wrote in message
| > || news:NZ********************@comcast.com...
| > |||I have a program that I want to launch links if certain criteria is
| met,
| > || the
| > ||| problem is how can I determine if a string is a program to launch,
| here
| > | is
| > ||| an example
| > ||| with quotes->
| > ||| "c:\Program Files\Windows Media Player\wmplayer.exe"
| > ||| "c:\program files\windows media player\mplayer2.exe" /open
| > ||| "http://radio.msn.com/asx/generate.aspx?type=genre&id=10002364"
/play
| > ||| http://myfavoritepages
| > |||
| > ||| it won't always be c:\ drive and sometimes it might be a ftp link
| below
| > | is
| > ||| currently what i'm doing
| > |||
| > ||| Function isURL(ByVal kcString As String)
| > ||| Dim kcFound As String = kcString.IndexOf("://")
| > ||| If kcFound > 0 Then
| > ||| Return True
| > ||| Else
| > ||| Return False
| > ||| End If
| > ||| End Function
| > |||
| > ||| Function isFile(ByVal kcString As String)
| > ||| Dim kcFound As String = kcString.IndexOf(":\")
| > ||| If kcFound > 0 Then
| > ||| Return True
| > ||| Else
| > ||| Return False
| > ||| End If
| > ||| End Function
| > |||
| > |||
| > |||
| > ||
| > ||
| > |
| > |
| >
| >
|
|

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Flix | last post by:
Is there any function to detect if a valid path is a file or a directory ? I've tried with (System.IO.Path.GetExtension(path)==""), but it doesn't work, since some files haven't any extension and...
5
by: achraf.b | last post by:
new javascript package to validate data # hasValidChars(string, characters, caseSensitive) # isIP(ip) # isAlpha(string) # isLetter(string) # isNotEmpty(string) #...
1
by: Gary Wessle | last post by:
Hi could someone help me to find out whats wrong with this code? **************** code **************** import os, sys if len(sys.argv) < 2: sys.exit("please enter a suitable directory.")
6
by: NickP | last post by:
Hi there, The following returns "False"... MsgBox(New Uri("ftp://me.pop.com/pop.bmp").IsFile.ToString) Is there any particular reason? It seems like a pretty poor function to me as that is...
5
by: 7stud | last post by:
Here's the code: ------------ import os, os.path, pprint mydir = "/Users/me/2testing" files = pprint.pprint(files) print os.path.join(mydir, "helloWorld.py")
10
by: ppaterson | last post by:
Can os.path.isfile(x) ever return True after os.remove(x) has successfully completed? (Windows 2003, Python 2.3) We had a couple of failures in a server application that we cannot yet reproduce...
2
by: 7stud | last post by:
Here is a program to print out the files in a directory: ----------- import os myfiles = os.listdir("../") print myfiles for afile in myfiles: print afile
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.