Results 1 to 12 of 12

Thread: Registering file type and Open the file **Resolved**

  1. #1

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Talking Registering file type and Open the file **Resolved**

    Hello all
    looking for "Registering an extension in windows", I came across this :
    VB Code:
    1. Public Sub AssociateFileExtension(Extension As String, _
    2. PathToExecute As String, ApplicationName As String)
    3. Dim sKeyName As String 'Holds Key Name in registry.
    4. Dim sKeyValue As String 'Holds Key Value in registry.
    5. Dim Ret&
    6. Dim lphKey&
    7. Ret& = InStr(1, Extension, ".")
    8. If Ret& <> 0 Then
    9. MsgBox "Extension has . in it. Remove and try again."
    10. Exit Sub
    11. End If
    12.  
    13. sKeyName = ApplicationName
    14. sKeyValue = ApplicationName
    15. Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
    16. Ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
    17.  
    18. sKeyName = "." & Extension
    19. sKeyValue = ApplicationName
    20. Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
    21. lphKey&)
    22. Ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
    23.  
    24. sKeyName = ApplicationName
    25. sKeyValue = PathToExecute & " %1"
    26. Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
    27. lphKey&)
    28. Ret& = RegSetValue&(lphKey&, "shell\open\command", _
    29. REG_SZ, sKeyValue, MAX_PATH)
    30.  
    31. sKeyName = ApplicationName
    32. sKeyValue = App.Path & "\" & App.EXEName & ".exe,0"
    33. Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
    34. lphKey&)
    35. Ret& = RegSetValue&(lphKey&, "DefaultIcon", REG_SZ, _
    36. sKeyValue, MAX_PATH)
    37.  
    38. SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
    39. End Sub
    40.  
    41. 'This will remove the file associations created above:
    42. Public Sub UnAssociateFileExtension(Extension As String, _
    43. ApplicationName As String)
    44. Dim sKeyName As String 'Finds Key Name in registry.
    45. Dim sKeyValue As String 'Finds Key Value in registry.
    46. Dim Ret&
    47. Ret& = InStr(1, Extension, ".")
    48. If Ret& <> 0 Then
    49. MsgBox "Extension has . in it. Remove and try again."
    50. Exit Sub
    51. End If
    52.  
    53. sKeyName = ApplicationName
    54. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
    55. "\DefaultIcon")
    56.  
    57. sKeyName = ApplicationName
    58. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
    59. "\shell\open\command")
    60.  
    61. sKeyName = ApplicationName
    62. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
    63. "\shell\open")
    64.  
    65. sKeyName = ApplicationName
    66. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
    67. "\shell")
    68.  
    69. sKeyName = ApplicationName
    70. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName)
    71.  
    72. sKeyName = "." & Extension
    73. Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName)
    74.  
    75. SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
    76. End Sub
    the problem is, what should I do with it??
    how can I register a file type with my program??

    in my program, i have an OPEN button, that uses a CommonDialog to open the file. If i can register file type, how can tell my program what to do with it??

    thanks in advance
    Last edited by Daskalos; Dec 8th, 2003 at 09:54 AM.
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  2. #2

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    ps.: bealtiful smiles in the vbcode... how do I put them in Visual Basic
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  3. #3
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Ohh, smilies in visual basic! I'll get to work on that idea asap!

  4. #4
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    the problem is, what should I do with it??
    I think you just ran into a bigger problem..


    BTW... Your missing some defined constance in your code.

  5. #5

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    i found an item called Disabe Smilies in This Post now the vbcode is right again ;-)
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  6. #6

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    ok ok.. i also agree that i had to search a little harder
    here is the solution for registering (special thanks to Aaron Young):

    Add the following to a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RegCreateKey Lib "advapi32.dll" _
    4. Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey _
    5. As String, phkResult As Long) As Long
    6. Private Declare Function RegSetValueEx Lib "advapi32.dll" _
    7. Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName _
    8. As String, ByVal Reserved As Long, ByVal dwType As Long, _
    9. lpData As Any, ByVal cbData As Long) As Long
    10. Private Declare Function RegCloseKey Lib "advapi32.dll" _
    11. (ByVal hKey As Long) As Long
    12. Private Declare Function SHChangeNotify Lib "shell32.dll" _
    13. (ByVal wEventID As Long, ByVal uFlags As Long, ByVal dwItem1 _
    14. As String, ByVal dwItems As String) As Long
    15.  
    16. Private Const HKEY_CLASSES_ROOT = &H80000000
    17. Private Const SHCNE_ASSOCCHANGED = &H8000000
    18. Private Const SHCNF_IDLIST = &H0
    19.  
    20. Public Sub AssociateMyApp(ByVal sAppName As String, ByVal _
    21. sEXE As String, ByVal sExt As String, Optional ByVal sCommand _
    22. As String, Optional ByVal sIcon As String)
    23.     Dim sCommandString As String
    24.     Dim lRegKey As Long
    25.    
    26.     'Open/Create the Extension under the "HKEY_CLASSES_ROOT" Hive of the Registry
    27.     Call RegCreateKey(HKEY_CLASSES_ROOT, "." & sExt, lRegKey)
    28.     'Set the "Default" value of the Key to the Application Name
    29.     Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sAppName, Len(sAppName))
    30.     'Close the Registry Key
    31.     Call RegCloseKey(lRegKey)
    32.    
    33.     sCommand = "\Shell\" & IIf(Len(sCommand), sCommand, "Open") & "\Command"
    34.     'Create the Application Key in the "HKEY_CLASSES_ROOT" Hive of the Registry
    35.     Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & sCommand, lRegKey)
    36.     'Set the Command to the EXE
    37.     Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sEXE, Len(sEXE))
    38.     'Close the Registry Key
    39.     Call RegCloseKey(lRegKey)
    40.     'If an Icon is required...
    41.     If Len(sIcon) Then
    42.        'Create a "DefaultIcon" entry under the Association Key
    43.         Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & "\DefaultIcon", lRegKey)
    44.         Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sIcon, Len(sIcon))
    45.         Call RegCloseKey(lRegKey)
    46.     End If
    47.     'Notify the Shell that an Association has Changed, (Updates Icons).
    48.     SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, vbNullString, vbNullString
    49. End Sub
    Example Usage:
    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3.   AssociateMyApp "MyFileType", "C:\MyApp.exe %1", "xyz", , "C:\MyApp.exe,-1"
    4. End Sub


    But I still don't know how my program is going to recive it.. how is he going to open the file??

    when i click the OPEN button in my form, this is what it does:
    VB Code:
    1. Private Sub Command5_Click()
    2. 'Text1.Text = App.Path & "\" & iData.Day & "-" & iData.Month & "-" & iData.Year & ".cmf"
    3. Dim hFile As Integer
    4. Dim strData As String
    5. On Error GoTo problem
    6.     Dim strTemp As String
    7.     CommonDialog1.CancelError = True
    8.     CommonDialog1.ShowOpen
    9.     hFile = FreeFile
    10.  
    11.     Open CommonDialog1.FileName For Input As #hFile
    12.         Line Input #hFile, strData
    13.             iNome.Text = strData
    14.         Line Input #hFile, strData
    15.             iEMasc.Text = strData
    16.         Line Input #hFile, strData
    17.             iCMasc.Text = strData
    18.         Line Input #hFile, strData
    19.             iEFem.Text = strData
    20.         Line Input #hFile, strData
    21.             iCFem.Text = strData
    22.         Line Input #hFile, strData
    23.             iHora.Text = strData
    24.         Line Input #hFile, strData
    25.             iData.Value = strData
    26.         Line Input #hFile, strData
    27.             Label6.Caption = strData
    28.         Line Input #hFile, strData
    29.             img(0).Caption = strData
    30.         Line Input #hFile, strData
    31.             img(1).Caption = strData
    32.         Line Input #hFile, strData
    33.             img(2).Caption = strData
    34.         Line Input #hFile, strData
    35.             img(3).Caption = strData
    36.     Close #hFile
    37. problem:
    38. Exit Sub
    39. End Sub
    Last edited by Daskalos; Dec 8th, 2003 at 10:04 AM.
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  7. #7
    Addicted Member
    Join Date
    Nov 2003
    Location
    India
    Posts
    227
    if you want to open the file using it's associated executable use ShellExecute API.

    Here is the Example

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private Sub Form_Load()
    ShellExecute Me.hwnd, "Open", "C:\y.txt", vbNull, "C:\", 3
    End Sub


    Jai.
    See you,
    -Jai
    [Friends Never Say Good Bye]

  8. #8

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Jaiboy, thanks, but it's not what i need...

    i want to know how my program is going to recieve the file... is there a string filename?? how does it works?
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  9. #9
    Addicted Member akki's Avatar
    Join Date
    Jun 2003
    Location
    Jungle
    Posts
    220
    i want to know how my program is going to recieve the file... is there a string filename?? how does it works?
    The file name will be passed as a command line argument and you can get that as ...

    VB Code:
    1. Public Sub Main()
    2.     Dim FileName As String
    3.     FileName = Command()
    4.     If FileName <> "" Then
    5.         ' do your open operation
    6.     End If
    7. End Sub
    akki

  10. #10
    Addicted Member
    Join Date
    Nov 2003
    Location
    India
    Posts
    227
    Oh, I got it.

    In your formLoad event write this & check.

    Private Sub Form_Load()
    Msgbox Command$
    End Sub

    The 'Command$' is a variable that contains the command line argument. When you associate some extension with your program, while double clicking it thru Windows Explorer Windows will send the file name as a command line argument. So you will get the filename from 'Command$' app global variable.

    Jai.
    See you,
    -Jai
    [Friends Never Say Good Bye]

  11. #11

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364
    Jaiboy that's it ;-)
    thanks a lot
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  12. #12
    New Member
    Join Date
    Dec 2004
    Posts
    13

    Re: Registering file type and Open the file **Resolved**

    hey, i used your msgbox test and i see that it IS recieving the file path, and
    when i tell it do the typical input dialog, it doesn't input it? i tried all the
    examples here in this article but still can't get what i need. i can't get my text
    editor to recognize the command it is given when it is loaded, i keep getting err 52, bad file name or number, i want to be able to right click a document,
    such as a log file, and select "open with...." and select my program, and then
    have my program open with the file contents displayed. i need to know,
    WHAT do i put in my form load or initialize event, and WERE do i put it, form
    LOAD or initialize? but i mostly need the "WHAT" of this. NOTEPAD.EXE is
    associated with .TXT files, WORDPAD.EXE for .DOC and .RTF files, then you
    have miscellanious files, .log, .ini, .dat, .css, etc, which have no standard
    asoociation, well save for a few of them,.... my POINT though, is that if you
    right click a .RTF document, or ANY of the above, and select "open with...",
    and select NOTEPAD.exe, even though NOTEPAD.exe is NOT the associated
    file type, it will STILL open, and display the contents of the document, hell i
    look at EVERYTHING with notepad, even wmv, mp3, mdb, just to see what it
    would look like in it's text equivelant. so why would i need to do the %1
    argument association, when i don't have to do that to open a non associated
    file type in notepad? notepad has SOMETHING in it's form load event that tells
    it "hey!, wake up! someone wants to use you to open --->THIS<----, so
    open it up for them, and show them what you can" i need THAT piece of code
    to tell my program the same thing.

    thanks very much to anyone who can help
    *************************************
    www.grimmuis.com grim@grimmusic.com

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