VB shell function on XP

Networking/Security Forums -> Programming and More

Author: WombatLocation: Canberra PostPosted: Wed Jul 24, 2002 4:09 am    Post subject: VB shell function on XP
    ----
Hi everyone,

Ages ago I wrote a little VB program to list all files and folders in a folder (in an indented heirarchical format). The list was written to a text file, and the app then used the shell function to launch the text file.

All worked fine on '98, but a while ago I got XP. I went to use my app just now, and it won't launch the text file. At the time it was just an exercise in using the FileSystemObject, but now I need to use the shell function in another app to launch a file in the same way. I've searched MSDN and the web but got reams of nothing.


Code:
Shell ("start " & Chr(34) & "C:\Folder Contents.txt" & Chr(34)), vbHide


Does anyone know why it won't work in XP?

Thanks in advance!

Author: TheKingsterLocation: UK PostPosted: Wed Jul 24, 2002 12:04 pm    Post subject:
    ----
You need to use an API call

Author: WombatLocation: Canberra PostPosted: Fri Aug 16, 2002 2:26 am    Post subject:
    ----
Sorry for the huge delay replying but I've been tied up on other projects.

Thanks very much Kingster that did the trick. For anyone else wanting to do the same, here's the code to copy'n'paste (it looks like a lot but there's only one line to change for your file Wink ).

In General Declarations:



Code:
' API stuff for launching the file:
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
      "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
      String, ByVal lpszFile As String, ByVal lpszParams As String, _
      ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&


Then a new function:

Code:
Function StartDoc(DocName As String) As Long
    Dim Scr_hDC As Long
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
    "", "C:\", SW_SHOWNORMAL)
End Function


Then where you want to open the file:

Code:
    r = StartDoc("C:\myFile.txt") ' Edit this line!
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                strMessage = "File not found"
            Case SE_ERR_PNF
                strMessage = "Path not found"
            Case SE_ERR_ACCESSDENIED
                strMessage = "Access denied"
            Case SE_ERR_OOM
                strMessage = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                strMessage = "DLL not found"
            Case SE_ERR_SHARE
                strMessage = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                strMessage = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                strMessage = "DDE Time out"
            Case SE_ERR_DDEFAIL
                strMessage = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                strMessage = "DDE busy"
            Case SE_ERR_NOASSOC
                strMessage = "No association for file extension"
            Case ERROR_BAD_FORMAT
                strMessage = "Invalid EXE file or error in EXE image"
            Case Else
                strMessage = "Unknown error"
        End Select
        MsgBox strMessage
    End If



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group