-
Dec 8th, 2003, 02:55 AM
#1
Thread Starter
Hyperactive Member
Registering file type and Open the file **Resolved**
Hello all
looking for "Registering an extension in windows", I came across this :
VB Code:
Public Sub AssociateFileExtension(Extension As String, _
PathToExecute As String, ApplicationName As String)
Dim sKeyName As String 'Holds Key Name in registry.
Dim sKeyValue As String 'Holds Key Value in registry.
Dim Ret&
Dim lphKey&
Ret& = InStr(1, Extension, ".")
If Ret& <> 0 Then
MsgBox "Extension has . in it. Remove and try again."
Exit Sub
End If
sKeyName = ApplicationName
sKeyValue = ApplicationName
Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
Ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
sKeyName = "." & Extension
sKeyValue = ApplicationName
Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
lphKey&)
Ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
sKeyName = ApplicationName
sKeyValue = PathToExecute & " %1"
Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
lphKey&)
Ret& = RegSetValue&(lphKey&, "shell\open\command", _
REG_SZ, sKeyValue, MAX_PATH)
sKeyName = ApplicationName
sKeyValue = App.Path & "\" & App.EXEName & ".exe,0"
Ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, _
lphKey&)
Ret& = RegSetValue&(lphKey&, "DefaultIcon", REG_SZ, _
sKeyValue, MAX_PATH)
SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
End Sub
'This will remove the file associations created above:
Public Sub UnAssociateFileExtension(Extension As String, _
ApplicationName As String)
Dim sKeyName As String 'Finds Key Name in registry.
Dim sKeyValue As String 'Finds Key Value in registry.
Dim Ret&
Ret& = InStr(1, Extension, ".")
If Ret& <> 0 Then
MsgBox "Extension has . in it. Remove and try again."
Exit Sub
End If
sKeyName = ApplicationName
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
"\DefaultIcon")
sKeyName = ApplicationName
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
"\shell\open\command")
sKeyName = ApplicationName
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
"\shell\open")
sKeyName = ApplicationName
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName & _
"\shell")
sKeyName = ApplicationName
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName)
sKeyName = "." & Extension
Ret& = RegDeleteKey(HKEY_CLASSES_ROOT, sKeyName)
SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
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
-
Dec 8th, 2003, 03:03 AM
#2
Thread Starter
Hyperactive Member
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
-
Dec 8th, 2003, 03:09 AM
#3
Fanatic Member
-
Dec 8th, 2003, 03:14 AM
#4
Fanatic Member
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.
-
Dec 8th, 2003, 03:19 AM
#5
Thread Starter
Hyperactive Member
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
-
Dec 8th, 2003, 09:21 AM
#6
Thread Starter
Hyperactive Member
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:
Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" _
Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey _
As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal Reserved As Long, ByVal dwType As Long, _
lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Declare Function SHChangeNotify Lib "shell32.dll" _
(ByVal wEventID As Long, ByVal uFlags As Long, ByVal dwItem1 _
As String, ByVal dwItems As String) As Long
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const SHCNE_ASSOCCHANGED = &H8000000
Private Const SHCNF_IDLIST = &H0
Public Sub AssociateMyApp(ByVal sAppName As String, ByVal _
sEXE As String, ByVal sExt As String, Optional ByVal sCommand _
As String, Optional ByVal sIcon As String)
Dim sCommandString As String
Dim lRegKey As Long
'Open/Create the Extension under the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, "." & sExt, lRegKey)
'Set the "Default" value of the Key to the Application Name
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sAppName, Len(sAppName))
'Close the Registry Key
Call RegCloseKey(lRegKey)
sCommand = "\Shell\" & IIf(Len(sCommand), sCommand, "Open") & "\Command"
'Create the Application Key in the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & sCommand, lRegKey)
'Set the Command to the EXE
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sEXE, Len(sEXE))
'Close the Registry Key
Call RegCloseKey(lRegKey)
'If an Icon is required...
If Len(sIcon) Then
'Create a "DefaultIcon" entry under the Association Key
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & "\DefaultIcon", lRegKey)
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sIcon, Len(sIcon))
Call RegCloseKey(lRegKey)
End If
'Notify the Shell that an Association has Changed, (Updates Icons).
SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, vbNullString, vbNullString
End Sub
Example Usage:
VB Code:
Option Explicit
Private Sub Command1_Click()
AssociateMyApp "MyFileType", "C:\MyApp.exe %1", "xyz", , "C:\MyApp.exe,-1"
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:
Private Sub Command5_Click()
'Text1.Text = App.Path & "\" & iData.Day & "-" & iData.Month & "-" & iData.Year & ".cmf"
Dim hFile As Integer
Dim strData As String
On Error GoTo problem
Dim strTemp As String
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
hFile = FreeFile
Open CommonDialog1.FileName For Input As #hFile
Line Input #hFile, strData
iNome.Text = strData
Line Input #hFile, strData
iEMasc.Text = strData
Line Input #hFile, strData
iCMasc.Text = strData
Line Input #hFile, strData
iEFem.Text = strData
Line Input #hFile, strData
iCFem.Text = strData
Line Input #hFile, strData
iHora.Text = strData
Line Input #hFile, strData
iData.Value = strData
Line Input #hFile, strData
Label6.Caption = strData
Line Input #hFile, strData
img(0).Caption = strData
Line Input #hFile, strData
img(1).Caption = strData
Line Input #hFile, strData
img(2).Caption = strData
Line Input #hFile, strData
img(3).Caption = strData
Close #hFile
problem:
Exit Sub
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
-
Dec 8th, 2003, 09:28 AM
#7
Addicted Member
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]
-
Dec 8th, 2003, 09:32 AM
#8
Thread Starter
Hyperactive Member
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
-
Dec 8th, 2003, 09:43 AM
#9
Addicted Member
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:
Public Sub Main()
Dim FileName As String
FileName = Command()
If FileName <> "" Then
' do your open operation
End If
End Sub
-
Dec 8th, 2003, 09:47 AM
#10
Addicted Member
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]
-
Dec 8th, 2003, 09:53 AM
#11
Thread Starter
Hyperactive Member
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
-
Jan 2nd, 2005, 10:25 PM
#12
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|