Re: IsFile, isURL
- From: "Bob Altman" <rda@xxxxxxxxxx>
- Date: Tue, 24 May 2005 12:30:29 -0700
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]" <Jay_Harlow_MVP@xxxxxxx> wrote in message
news:OCfEFzuXFHA.3584@xxxxxxxxxxxxxxxxxxxxxxx
> 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]" <Jay_Harlow_MVP@xxxxxxx> wrote in message
> news:ey%23LewuXFHA.3488@xxxxxxxxxxxxxxxxxxxxxxx
> | 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]
- Re: IsFile, isURL
- From: Jay B. Harlow [MVP - Outlook]
- Re: IsFile, isURL
- From: Jay B. Harlow [MVP - Outlook]
- IsFile, isURL
- Prev by Date: Re: file downloaded using FileStream "in use by another process"
- Next by Date: Re: Threading Question - Newbie
- Previous by thread: Re: IsFile, isURL
- Next by thread: Re: IsFile, isURL
- Index(es):
Relevant Pages
|