Re: IsFile, isURL
- From: "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxx>
- Date: Sun, 22 May 2005 11:34:16 -0500
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]" <Jay_Harlow_MVP@xxxxxxx> wrote in message
news:uB5DApuXFHA.2932@xxxxxxxxxxxxxxxxxxxxxxx
| 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" <cakelly@xxxxxxxxxxxxx> wrote in message
| news:NZudnfS5BLx9FA3fRVn-iQ@xxxxxxxxxxxxxx
||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
||
||
||
|
|
.
- Follow-Ups:
- Re: IsFile, isURL
- From: Jay B. Harlow [MVP - Outlook]
- Re: IsFile, isURL
- References:
- IsFile, isURL
- From: C. A. Kelly
- Re: IsFile, isURL
- From: Jay B. Harlow [MVP - Outlook]
- IsFile, isURL
- Prev by Date: Check where library is called from
- Next by Date: Re: IsFile, isURL
- Previous by thread: Re: IsFile, isURL
- Next by thread: Re: IsFile, isURL
- Index(es):
Relevant Pages
|