News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

File association

Started by ecube, January 21, 2007, 04:49:00 AM

Previous topic - Next topic

ecube

Heres some code I wrote to associate a file extension with a program


.486
.model flat, stdcall
option casemap:none


include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\advapi32.inc
include \masm32\include\shell32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\shell32.lib
Associate_File proto :DWORD,:DWORD,:DWORD

     CTEXT MACRO text:VARARG
            LOCAL TxtName
              .data
               TxtName BYTE text,0
              .code
            EXITM <ADDR TxtName>
     ENDM


.data?
szBuff             db 256 dup(?)
RegKey             dd ?
CreatedKey         dd ?
CreatedKey2        dd ?
tempvalue          dd ?


.code
start:
;example
invoke Associate_File,CTEXT(".asm"),CTEXT("asm_auto_file"), CTEXT("C:\Program Files\RadAsm\RadASM.exe")
invoke Associate_File,CTEXT(".inc"),CTEXT("inc_auto_file"), CTEXT("C:\Program Files\RadAsm\RadASM.exe")

invoke ExitProcess,0

;this associates the specified file extensions to the created classname that points to the Exeprogram for opening
Associate_File proc Extension:DWORD,ClassName:DWORD,ExeProgram:DWORD

;create a value for this key that contains the classname
invoke RegCreateKeyEx,HKEY_CLASSES_ROOT,Extension,0,0,0,0,0,offset CreatedKey,offset tempvalue
invoke RegCloseKey,CreatedKey

;reopen, since direct creation handle wasn't working
invoke RegOpenKey,HKEY_CLASSES_ROOT,Extension,offset CreatedKey
invoke lstrlen,ClassName
mov ecx,eax
invoke RegSetValueEx, CreatedKey, NULL, 0, REG_SZ, ClassName,ecx

; create a new key for the Class name
invoke wsprintf,addr szBuff,CTEXT("%s\Shell\Open\Command"),ClassName
invoke RegCreateKeyEx,HKEY_CLASSES_ROOT,addr szBuff,0,0,0,0,0,offset CreatedKey2,offset tempvalue
invoke RegCloseKey,CreatedKey2

;reopen, since direct creation handle wasn't working
invoke RegOpenKey,HKEY_CLASSES_ROOT,addr szBuff,offset CreatedKey2

;set its value to the command line
invoke wsprintf,addr szBuff,CTEXT('"%s"'),ExeProgram
invoke lstrcat,addr szBuff,CTEXT(' "%1"')
invoke RegSetValueEx, CreatedKey2, NULL, 0, REG_SZ,addr szBuff,sizeof szBuff
invoke RegCloseKey,CreatedKey
invoke RegCloseKey,CreatedKey2

;notify Windows that file associations have changed
invoke SHChangeNotify,SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
ret
Associate_File endp

end start

Shantanu Gadgil

Hi,
Nice function!  :U :U

just a few thoughts though:
How about having the regkey handle variables local to the function? (so I can pick up the function as-is anywhere else  :bg :bg )
Some error checking???

Also how about NOT having the update notification as part of the main function. (If there were many associations, each would fire an update) So, could there be a set of "association related functions", each doing those little things/

P.S. I am intrigued why the direct creation is not working?  :( :(

Regards,
Shantanu
To ret is human, to jmp divine!

ecube

Thanks, and feel free to change it as you see fit, in the future i'll release more clean code, thanks for the suggestions. As far as RegCreateKeyEx goes I honestly can't tell you, but anytime I try to use the returned handle from it, it doesn't work correctly.


Shantanu Gadgil

Hi,
I just noticed that you are creating a key with the SAM as '0' (zero). I usually create it with flag KEY_WRITE:
invoke RegCreateKeyEx,hKeyRoot,pszSubKey,0,offset szREG_SZ,0,KEY_WRITE,0,addr hKey,0

Could that be the problem?

You could check out the "Registry Helper" APIs which I have submitted in the GeneSys project. They have always worked OK for me.
http://www.masm32.com/board/index.php?topic=5735.0

Also, from MSDN:
KEY_ALL_ACCESS (0xF003F) Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.
KEY_CREATE_LINK (0x0020) Reserved for system use.
KEY_CREATE_SUB_KEY (0x0004) Required to create a subkey of a registry key.
KEY_ENUMERATE_SUB_KEYS (0x0008) Required to enumerate the subkeys of a registry key.
KEY_EXECUTE (0x20019) Equivalent to KEY_READ.
KEY_NOTIFY (0x0010) Required to request change notifications for a registry key or for subkeys of a registry key.
KEY_QUERY_VALUE (0x0001) Required to query the values of a registry key.
KEY_READ (0x20019) Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.
KEY_SET_VALUE (0x0002) Required to create, delete, or set a registry value.
KEY_WOW64_32KEY (0x0200) Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. For more information, see Accessing an Alternate Registry View.
This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.

KEY_WOW64_64KEY (0x0100) Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. For more information, see Accessing an Alternate Registry View.
This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.

KEY_WRITE (0x20006) Combines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE, and KEY_CREATE_SUB_KEY access rights.


Cheers,
Shantanu
To ret is human, to jmp divine!