Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] File Association And The Registry

delphi

  • This topic is locked This topic is locked
7 replies to this topic

#1 oggtheclever

oggtheclever

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 08 April 2012 - 02:57 PM

I come to you in desperation as I've been struggling with this problem all weekend. So obsessed with it have I become that I even dreamt about it last night.

My problem is this. For the past couple of months I have been creating my own media player in Delphi (where's your delphi section :worry: ) and I must admit that it's really coming along very nicely.

This weekend I finally got around to doing some of the options in 'preferences' and one of these was the file associations. The idea, as you expect, is that the user can select various extensions, such as .avi, .mpg, .mp3 etc.. and assign them to my programme so that the next time they double-click on one of these files it's my media player that opens and plays them and not, say, windows media player.

So far so good...

Now it seems no matter what I do, how I change the registry using examples found on various sites, whether I run the programme as an admin or not it doesn't make the slightest difference. I simply cannot change any file association to my media player.

Further imagine my utter frustration when I download media players such as Zoom Player or the DIVX Player and go into their options dialog and find that within a couple of clicks I can assign extensions to them with no problem whatsoever. What is it that they are doing that my media player isn't?

So, to cut a long story short I suppose the real question is this. What registry entries do I need to change (without having to run my app in admin mode as I didn't with Zoom Player etc.) to associate certain filetypes such as .avi with my media player that actually works under Windows 7?

Can I confine myself entirely to HKEY_CURRENT_USER for example?

Many thanks in advance :D
  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 08 April 2012 - 07:00 PM

I would imagine that in order to make the change for all the users on the machine, you'll have to run the program as an administrator. There might be a way to change that for the current user without those, though, but I don't know.

Hold on, let me do some search online...


Take a look at this page (by the way, I haven't tested anything it says, so I don't know for sure whether it'll work):
http://social.msdn.m...f1-1a6bf292c901
  • 0
Regards,
RR

#3 oggtheclever

oggtheclever

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 08 April 2012 - 07:50 PM

Thanks, I'll take a good look at that and get back to you :D
  • 0

#4 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 08 April 2012 - 11:52 PM

Can I confine myself entirely to HKEY_CURRENT_USER for example?


No, since the association is done in very different root key. It's done under HKEY_CLASSES_ROOT. So the association always applied to all the users (whole machine). So yes, you have to administrative privilege to be able to change the file association.

Can you post your code for registering your application to the extensions? So we can see what steps you've missed or done improperly.

For the mean time, your code may have forgotten to tell "everyone" in the current shell session that file association has changed. You can do this by calling SHChangeNotify after done with file association process. Like this:

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);

  • 0

#5 oggtheclever

oggtheclever

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 09 April 2012 - 01:50 AM

Here is a pretty typical example of what I'm trying



with TRegistry.Create do
  try
	RootKey := HKEY_ClASSES_ROOT;

	if OpenKey('.avi', True) then
	  WriteString('', 'avifile');

	if OpenKey('avifile', True) then
	  WriteString('', 'Media File');

	if OpenKey('avifile\DefaultIcon', True) then
	  WriteString('', ParamStr(0));

	if OpenKey('avifile\shell', True) then
	  WriteString('', 'open');

	if OpenKey('avifile\shell\open\command', True) then
	  WriteString('', '"'+ParamStr(0)+'" "%1"');

  finally
	Free;
  end;

  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);

Additional.. I changed the above code to .jmp and jmpfile and also gave one of my avi files the extension .jmp and ran it as admin and it worked perfectly. However, I then re-set the .jmp file to open with Zoom Player, which it did and then ran my programme again but this time it could not change the association. It seems that if the association doesn't yet exist then it works fine but otherwise, forget it..

Having thoroughly investigated everything to do with my new .jmp file in the registry it turns out that I can reset it all back to my programme except the following

\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jmp\UserChoice\

which reads

ProgId Applications\zplayer.exe

Even using RedEdit itself I cannot change this key. If I could then all would be ok.
  • 0

#6 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 09 April 2012 - 04:34 AM

Please wrap the codes inside CODE tag. It will making it easier to read.

Your suspicion is actually correct. Starting from Vista, you can have several shell actions associated with a file extension. What you are doing there just add a new shell action related with .avi but not necessarily making your shell action the default.

Check this msdn page.

Also take a look info this registry key:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi\UserChoice

It is where the default shell action defined (if there is more than one). For your case, if you add a string value with name "Progid" and give "avifile" as the value, you will get your program to be the default for .avi files.
  • 0

#7 oggtheclever

oggtheclever

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 09 April 2012 - 05:02 AM

Success... Many thanks for the help :biggrin:

procedure TForm1.RegisterFileType(ExtName:String);


var


a: byte;



begin


a:=0;



with TRegistry.Create do


try


RootKey := HKEY_CLASSES_ROOT;



DeleteKey('\.avi\');



if OpenKey('\.avi\', True) then begin


WriteString('','avifile');


WriteString('PerceivedType', 'video');


WriteString('Content Type', 'video/avi');


end;



OpenKey('\.avi\OpenWithList\ehshell.exe\', True);



if OpenKey('\.avi\OpenWithProgIds\', True) then


WriteBinaryData('avifile', a, 1);



if OpenKey('\avifile\shell\open\command\', True) then


WriteString('', '"'+ParamStr(0)+'" "%1"');



RootKey := HKEY_CURRENT_USER;



DeleteKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.av​i\');



if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi\OpenW​ithProgids\', True) then


WriteBinaryData('avifile', a, 1);



finally


Free;


end;



SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);


end;


  • 0

#8 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 09 April 2012 - 05:22 AM

That's great! Congrats for finally conquered the beast. :)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download