Results 1 to 21 of 21

Thread: Checking if file handler is installed

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7

    Checking if file handler is installed

    Hi

    I'm wondering if there is any way to determine if there is a program installed that can handle a given file type and they use it to open a given document.

    E.g I want to be able to execute a PDF or a DXW file from within my vbNet application but only if a PDF or DXW viewer is installed and registiered otherwise I want to trap the fact that a handler isn't installed and bring up a message box.

    Thanks,

    Sarah

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    [RESOLVED]

    Just in case someone else has a similar problem in the future.

    I found some old code on PlanetSourcecode and updated it to work with VB.NET. I also added a new error message in the appropriate place with regard to file associations.

    Sarah

    '**************************************
    'Windows API/Global Declarations for :Execute_Program
    '**************************************
    'Declares for Execute_Program
    Declare Sub BringWindowToTop Lib "user" (ByVal hWnd As Integer)
    Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
    Const WM_CLOSE = &H10
    Declare Sub SetWindowPos Lib "user" (ByVal hWnd%, ByVal hwndAfter%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
    Const HWND_TOP = 0
    Const HWND_BOTTOM = 1
    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2
    Const GWL_ID = (-12)
    Const GW_HWNDNEXT = 2
    Const GW_CHILD = 5

    Const FWP_STARTSWITH = 0
    Const FWP_CONTAINS = 1
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer, ByVal lpszOp As String, ByVal lpszFile As String, ByVal spszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Integer) As Integer
    Const SW_SHOW = 5
    '**************************************
    ' Name: Execute_Program
    ' Description:Runs a program and handles all possible errors (such as running out
    ' of memory, file can't be opened, etc.) Also, unlike the VB Shell command, it allows
    ' you to specify a 'default working directory'!
    ' Also, allows you to run a file that is only an association--example:you can run a .txt file with this function!
    ' By: Ian Ippolito (RAC)
    '
    ' Minor upadates to allow integration with 32 bit environments and to allow error message "No association for this file."
    ' By Sarah Connor
    '
    ' Inputs:strFilePath--program to run
    ' strParms--program command line parms (if any)
    ' strDir--default working directory
    '
    ' Returns:returns TRUE=successful
    ' FALSE=failed
    '
    ' Assumes:None
    '
    ' Side Effects:None
    ' This code is copyrighted and has limited warranties.
    ' Please see http://www.Planet-Source-Code.com/xq...s/ShowCode.htm for details.
    '**************************************
    Function Execute_Program(ByVal strFilePath As String, ByVal strParms As String, ByVal strDir As String) As Integer
    'run program
    Dim hwndProgram As Integer
    hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, SW_SHOW)
    'evaluate errors
    Select Case (hwndProgram)
    Case 0
    MsgBox("Insufficent system memory or corrupt program file.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 2
    MsgBox("File not found.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 3
    MsgBox("Invalid path.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 5
    MsgBox("Sharing or Protection Error.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 6
    MsgBox("Seperate data segments are required for each task.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 8
    MsgBox("Insufficient memory to run the program.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 10
    MsgBox("Incorrect Windows version.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 11
    MsgBox("Invalid program file.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 12
    MsgBox("Program file requires a different operating system.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 13
    MsgBox("Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 14
    MsgBox("Unknown program file type.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 15
    MsgBox("Windows program does not support protected memory mode.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 16
    MsgBox("Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 19
    MsgBox("Attempt to run a compressed program file.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 20
    MsgBox("Invalid dynamic link library.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 21
    MsgBox("Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath)
    Execute_Program = False
    Exit Function
    Case 31
    MsgBox("No association for this file", vbOKOnly + vbInformation, strFilePath)
    Exit Function
    End Select
    Execute_Program = True
    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' To open an INI file with the associated file handler (generally NOTEPAD.EXE) use:
    Execute_Program("c:\windows\win.ini", "", "")

    ' To open an JPG image with the associated file handler use:
    Execute_Program("c:\mypic.jpg", "", "")
    End Sub

  3. #3
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Just in case someone is interested in reading it.
    VB Code:
    1. '**************************************
    2. 'Windows API/Global Declarations for :Execute_Program
    3. '**************************************
    4. 'Declares for Execute_Program
    5. Declare Sub BringWindowToTop Lib "user" (ByVal hWnd As Integer)
    6. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
    7. Const WM_CLOSE = &H10
    8. Declare Sub SetWindowPos Lib "user" (ByVal hWnd%, ByVal hwndAfter%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
    9. Const HWND_TOP = 0
    10. Const HWND_BOTTOM = 1
    11. Const HWND_TOPMOST = -1
    12. Const HWND_NOTOPMOST = -2
    13. Const GWL_ID = (-12)
    14. Const GW_HWNDNEXT = 2
    15. Const GW_CHILD = 5
    16.  
    17. Const FWP_STARTSWITH = 0
    18. Const FWP_CONTAINS = 1
    19. Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer, ByVal lpszOp As String, ByVal lpszFile As String, ByVal spszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Integer) As Integer
    20. Const SW_SHOW = 5
    21. '**************************************
    22. ' Name: Execute_Program
    23. ' Description:Runs a program and handles all possible errors (such as running out
    24. ' of memory, file can't be opened, etc.) Also, unlike the VB Shell command, it allows
    25. ' you to specify a 'default working directory'!
    26. ' Also, allows you to run a file that is only an association--example:you can run a .txt file with this function!
    27. ' By: Ian Ippolito (RAC)
    28. '
    29. ' Minor upadates to allow integration with 32 bit environments and to allow error message "No association for this file."
    30. ' By Sarah Connor
    31. '
    32. ' Inputs:strFilePath--program to run
    33. ' strParms--program command line parms (if any)
    34. ' strDir--default working directory
    35. '
    36. ' Returns:returns TRUE=successful
    37. ' FALSE=failed
    38. '
    39. ' Assumes:None
    40. '
    41. ' Side Effects:None
    42. ' This code is copyrighted and has limited warranties.
    43. ' Please see [url]http://www.Planet-Source-Code.com/x...ts/ShowCode.htm[/url] for details.
    44. '**************************************
    45. Function Execute_Program(ByVal strFilePath As String, ByVal strParms As String, ByVal strDir As String) As Integer
    46. 'run program
    47. Dim hwndProgram As Integer
    48. hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, SW_SHOW)
    49. 'evaluate errors
    50. Select Case (hwndProgram)
    51. Case 0
    52. MsgBox("Insufficent system memory or corrupt program file.", 0, "Error running " & strFilePath)
    53. Execute_Program = False
    54. Exit Function
    55. Case 2
    56. MsgBox("File not found.", 0, "Error running " & strFilePath)
    57. Execute_Program = False
    58. Exit Function
    59. Case 3
    60. MsgBox("Invalid path.", 0, "Error running " & strFilePath)
    61. Execute_Program = False
    62. Exit Function
    63. Case 5
    64. MsgBox("Sharing or Protection Error.", 0, "Error running " & strFilePath)
    65. Execute_Program = False
    66. Exit Function
    67. Case 6
    68. MsgBox("Seperate data segments are required for each task.", 0, "Error running " & strFilePath)
    69. Execute_Program = False
    70. Exit Function
    71. Case 8
    72. MsgBox("Insufficient memory to run the program.", 0, "Error running " & strFilePath)
    73. Execute_Program = False
    74. Exit Function
    75. Case 10
    76. MsgBox("Incorrect Windows version.", 0, "Error running " & strFilePath)
    77. Execute_Program = False
    78. Exit Function
    79. Case 11
    80. MsgBox("Invalid program file.", 0, "Error running " & strFilePath)
    81. Execute_Program = False
    82. Exit Function
    83. Case 12
    84. MsgBox("Program file requires a different operating system.", 0, "Error running " & strFilePath)
    85. Execute_Program = False
    86. Exit Function
    87. Case 13
    88. MsgBox("Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath)
    89. Execute_Program = False
    90. Exit Function
    91. Case 14
    92. MsgBox("Unknown program file type.", 0, "Error running " & strFilePath)
    93. Execute_Program = False
    94. Exit Function
    95. Case 15
    96. MsgBox("Windows program does not support protected memory mode.", 0, "Error running " & strFilePath)
    97. Execute_Program = False
    98. Exit Function
    99. Case 16
    100. MsgBox("Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath)
    101. Execute_Program = False
    102. Exit Function
    103. Case 19
    104. MsgBox("Attempt to run a compressed program file.", 0, "Error running " & strFilePath)
    105. Execute_Program = False
    106. Exit Function
    107. Case 20
    108. MsgBox("Invalid dynamic link library.", 0, "Error running " & strFilePath)
    109. Execute_Program = False
    110. Exit Function
    111. Case 21
    112. MsgBox("Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath)
    113. Execute_Program = False
    114. Exit Function
    115. Case 31
    116. MsgBox("No association for this file", vbOKOnly + vbInformation, strFilePath)
    117. Exit Function
    118. End Select
    119. Execute_Program = True
    120. End Function
    121.  
    122.  
    123. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    124. ' To open an INI file with the associated file handler (generally NOTEPAD.EXE) use:
    125. Execute_Program("c:\windows\win.ini", "", "")
    126.  
    127. ' To open an JPG image with the associated file handler use:
    128. Execute_Program("c:\mypic.jpg", "", "")
    129. End Sub

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is there any .NET way ?? this looks messy at least for me

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    Appologies for not posting it in colour, it was my first post with code in it. Do you just set the HTML toggle to on to do that?

    Yes it is rather messy, I was rather hoping someone would offer a more elegant solution. I've only been using vb.net for 2 weeks and I'm still trying to get my head around the whole thing.

    Sarah

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Simply do this :

    VB Code:
    1. 'your code

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    hmmm didn't show the formula I sent !
    click code button , then type vb between " [Highlight=VB] and this [/vbcode ] "
    hope it works this time !

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Dont you think that searching the registry for file type association and then finding that program and initializing a new process of that viewer will be a very easy and neat .NET way of this?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    To be more accurate imagine you want to find what program is the viewer of pdf files on the target computer

    1. In the HKEY_CLASSES_ROOT of the registry you will find a key called .jpg
    2. That key has a String (REG_SZ) value called (deafult) that shows the file type, in this case for example ACDSee.JPG
    3. Again in HKEY_CLASSES_ROOT will be another key called ACDSee.JPG (the value from (defalut) entry of previous key)
    4. If you dig this key you will find:shell, open and command:

    HKEY_CLASSES_ROOT\ACDSee.JPG\shell\Open\Command

    5. Again the (default) value of command Key is the path to the file that opens this kind of file.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    Lunatic, to be honest that doesn't seem like a nicer or easier way of doing things. My (and I use that loosly) way calls the associated app through a system API call with built-in error handling. It also lets you use one small-ish piece of code to execute any registered file type.

    If I'm reading your idea correctly, and I'm pretty new at VB.net so forgive me if I'm not, you suggest checking the registry for every file type you want to execute and then manually traping the potential errors. That sounds like a lot of un-necesary programming when the wheel has already been invented.

    Just my 0.02c

    Sarah

  11. #11
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You may be right, but all you need to do is writing few lines of pure .NET code. I thought generally it would be a good idea to avoid non native .NET code, although its not possible all the time.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    Originally posted by Lunatic3
    You may be right, but all you need to do is writing few lines of pure .NET code. I thought generally it would be a good idea to avoid non native .NET code, although its not possible all the time.
    Hi Lunatic3

    I'd be thrilled to see an example in pure .NET code, can you show me how this is achieved with some example code.

    Sarah

  13. #13
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Sarah

    If you just want to open a file with its default viewer then this will be easy i guess, you can let windows handle that. That registry stuff was about to check what will open a given file type.

    VB Code:
    1. Dim p As New Process(), s As String
    2. Try
    3.     s = "C:\myfile.pdf"
    4.     p.Start(s)
    5. Catch ex As System.Exception
    6.     MessageBox.Show(ex.Message)
    7. End Try

    If u still want the code about reading the registry i will post it.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    Originally posted by Lunatic3
    Sarah

    If you just want to open a file with its default viewer then this will be easy i guess, you can let windows handle that. That registry stuff was about to check what will open a given file type.

    VB Code:
    1. Dim p As New Process(), s As String
    2. Try
    3.     s = "C:\myfile.pdf"
    4.     p.Start(s)
    5. Catch ex As System.Exception
    6.     MessageBox.Show(ex.Message)
    7. End Try

    If u still want the code about reading the registry i will post it.
    This is the answer to the original question I asked! Oh well, I guess I learned something about API calls at least.

    The registry code may well come in usuful, I'm here to learn after all. Please post it.

    Sarah

  15. #15
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Sarah

    Here is the code to read the registry for the file that opens the specified extension.
    VB Code:
    1. Imports Microsoft.Win32
    2. '
    3. '
    4. '
    5. '
    6. '
    7. Dim rgkey As RegistryKey
    8. Dim ext As String = ".pdf"
    9. Try
    10.       rgkey = Registry.ClassesRoot.OpenSubKey(ext)
    11.       If rgkey Is Nothing Then
    12.           MessageBox.Show("The file extention is unknown!")
    13.           Exit Sub
    14.        End If
    15.        ext = rgkey.GetValue("")
    16.        rgkey = Registry.ClassesRoot.OpenSubKey(ext).OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command")
    17.        If rgkey Is Nothing Then
    18.             MessageBox.Show("No 'Open' event is associated with this extension!")
    19.              Exit Sub
    20.        End If
    21.        ext = rgkey.GetValue("")
    22.         ext = ext.Split(Chr(34))(1)
    23.         MessageBox.Show(ext)
    24.         Catch ex As Exception
    25.             MessageBox.Show(ex.Message)
    26. End Try
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    7
    That's a great piece if code Lunatic3. I'll be sure to place it in my reference library as I'm bound to have a need for registry look-up stuff in the future.

    Thank you for sharing.

    Sarah

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    .NET Framework has a class named Registery.It's more powerful and efficient than those APIs . It contains Security stuff that existed in XP , WIN2000 , and .NET OS .So if you use it , it should be forward compatible.

  18. #18
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    A class more powerful than an API? A bit hard to believe...

  19. #19
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Its not more powerful, but wraps up all the api's into easy to use classes.

  20. #20
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Zealot
    A class more powerful than an API? A bit hard to believe...
    at least , it might be in this case and it's .NET way .These classes are the replacement of those APIs .

  21. #21
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175
    I've run into a similar problem and was wondering, is there a way to invoke the Windows unknown file dialog? ie. the one that appears whenever you double click on a file with an extension it doesn't know how to handle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width