Re: IsFile, isURL

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
||
||
||
|
|


.



Relevant Pages

  • Re: IsFile, isURL
    ... I'm intrigued by the comment about LocalFree vs. Marshal.FreeHGlobal. ... > || Public Shared Function ParseCommandLine(ByVal commandLine As String) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: IsFile, isURL
    ... | arguments rather than simply calling Environment.GetCommandLineArgs, ... Public Sub Mainas String) ... I was using the Windows API as the original poster stated "launch links if ... |> || Public Shared Function ParseCommandLine(ByVal commandLine As ...
    (microsoft.public.dotnet.languages.vb)
  • Re: IsFile, isURL
    ... || Public Shared Function ParseCommandLine(ByVal commandLine As String) ... || Public Shared Function IsFile(ByVal kcString As String) As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: IsFile, isURL
    ... | I should be calling LocalFree on the return value from CommandLineToArgvW. ... | Declare Auto Function LocalFree Lib "kernel32" ... || Public Shared Function ParseCommandLine(ByVal commandLine As String) ...
    (microsoft.public.dotnet.languages.vb)