Help with File Association

Hello I'm making a cd player for windows and I have the option to set it as the default cd player. It works ok when I Click on the CDRom Drive it opens up my cd player. I'm using vista and I want to associate my cd player with the cda extension so when I explorer my cdrom drive I want to see my icon with the cda and beable to click on the cda file and it open with my player. Can someone help me with this. here is my code I am trying to use:

BOOL CALLBACK DlgDefault::OnCommand(int NotifyCode, int ControlID, HWND hWndControl)
{
switch(ControlID) {
case RB_CHAMELEON:
EnableControl(EB_OTHER, FALSE);
break;

case RB_OTHER:
EnableControl(EB_OTHER, TRUE);
break;

case BT_SETDEFAULT:
{
int ret;
if(IsButtonChecked(RB_CHAMELEON) == 1) {
ret = MessageBox(Handle, "Set Creative CD As The Default CD Player ?",
"Set Creative CD As The Default CD Player ?",MB_YESNO | MB_ICONQUESTION);
}
else
ret = MessageBox(Handle, "Set This As The Default CD Player ?",
"Set This As The Default CD Player ?",MB_YESNO | MB_ICONQUESTION);

if(ret == IDYES ) {
//ShellExecute(Handle, NULL, "Assoc.exe", NULL, "",SW_SHOWNORMAL);
WRegKey *reg = new WRegKey(HKEY_CLASSES_ROOT, "cdafile\\shell\\play\\command");

WRegKey *reg1 = new WRegKey(HKEY_CLASSES_ROOT, ".cda");
reg1->SetStringValue("Audio CD","cdafile");
delete reg1;

// cda tipy description
WRegKey *regTrackTypeDesc = new WRegKey(HKEY_CLASSES_ROOT, "cdafile");
regTrackTypeDesc->SetStringValue(".cda","Audio CD");
delete regTrackTypeDesc;

WRegKey *reg2 = new WRegKey(HKEY_CLASSES_ROOT, "cdafile\\shell\\play\\command");


char path[MAX_PATH];
char text[MAX_PATH];
if(IsButtonChecked(RB_CHAMELEON) == 1) {
// icon
WRegKey *regIcon = new WRegKey(HKEY_CLASSES_ROOT, "cdafile\\DefaultIcon");
sprintf(path,"%s\\%s,1",myApp->FileDir, myApp->FileName);
regIcon->SetStringValue("",path);
delete regIcon;
sprintf(path,"%s\\%s /play %1",myApp->FileDir, myApp->FileName);
SHChangeNotify( SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL );
}
else {
if(IsButtonChecked(RB_OTHER) == 1) {

if( GetControlText(EB_OTHER, text, MAX_PATH) && ( lstrlen(text) > 0) ) {
WRegKey *regIcon = new WRegKey(HKEY_CLASSES_ROOT, "cdafile\\DefaultIcon");
sprintf(path,"%s,1",text);
regIcon->SetStringValue("",path);
delete regIcon;
sprintf(path,"%s %1", text);

}
else {
MessageBox(Handle, "Set data for default player !","Error",MB_ICONSTOP|MB_APPLMODAL);

}

}
}


reg->SetStringValue("",path);
reg2->SetStringValue("",path);

delete reg2;
delete reg;

// set default action
reg = new WRegKey(HKEY_CLASSES_ROOT, "Audio CD\\shell");
reg->SetStringValue("Creative CD.exe","play");
delete reg;

// set default action
WRegKey *reg4 = new WRegKey(HKEY_CLASSES_ROOT, "cdafile\\shell");
reg4->SetStringValue("Creative CD.exe","play");
delete reg4;
SHChangeNotify( SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL );





MessageBox(Handle, "Michael Hardy Said, It Is Now Set As Your Default CD Player","Now Set...",MB_OK | MB_ICONINFORMATION);




}

}
break;


default:
return FALSE;

}

return TRUE;


}

Thank you in advance
1. Use code tags. http://www.cplusplus.com/articles/z13hAqkS/
2. It doesn't matter how much code you see out there that uses HKEY_CLASSES_ROOT. It is incorrect. This is a hive deprecated since Windows 95! It is kept for compatiblity with OLD software. NEW software MUST write to the NEW locations: If you want per-user registration, use HKEY_CURRENT_USER\SOFTWARE\Classes instead of HKEY_CLASSES_ROOT; if you want per-machine registration then use HKEY_LOCAL_MACHINE\SOFTWARE\Classes instead of HKEY_CLASSES_ROOT. Note that per-machine registration requires elevation.
3. Objects 'reg' and 'reg2' point to the same registry key. Seems double work.
4. What is the problem, exactly? You say it works, so where do you need help????
Hi can you please give me an example using my code above?
It doesn't let me play .cda files.

Thanks
I am not sure what you mean by "an example" because I'm not sure what is wrong. You said in your original post that it works. So I guess that is not true from what you are saying now. So I'll start from the fact that the above registration is not working for you. But let me ask you this: Is the registration not working, or is the code above not writing the correct registration?

Show the desired registration keys so I can try to figure out if the code above is producing it or not.

Also note that I don't know the class WRegKey, so I don't think I can write code using it.
Hi, Thank you for your reply. It works partially. will you be willing to download the source code and help me?
Currently busy learning Raw Input from mouse and trying to use it in .Net C#, so I don't think I have the time to check out your project. However, if you post the registry keys that you want to write and show exactly what they need to look like including key values, I can probably tell you if the code above produces it or not.

Also note that I am no expert in file registration. Are you certain that your desired keys would work OK 100%? Have you set the keys manually using the registry editor to verify this?

It would also be good if you verified the keys by comparison. Install some other CD player and see the registration this other CD player injects in the system, and then perform a similar registration yourself.
Hi When I register my cd player using cdrun which is another cdplayer for windows which has a register another player as default option it works. I'll pay you $20.00 if you can help me.

I'm new to forums and don't know how to attach files but I can send you a download link.

Thanks
Hi this is my reg code:

#define STRICT
#include <windows.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "wregistry.h"

WRegKey::WRegKey(HKEY root, LPCTSTR lpszSubKey)
{
dwDisposition = 0L;
::RegCreateKeyEx(root,lpszSubKey,0,"",REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hKey, &dwDisposition);


}

WRegKey::~WRegKey()
{
::RegCloseKey(hKey);
}

BOOL WRegKey::SetStringValue(LPCTSTR lpValueName, LPCTSTR lpData)
{

if( RegSetValueEx(hKey, lpValueName, 0, REG_SZ,
(CONST BYTE * ) lpData,lstrlen(lpData)) == ERROR_SUCCESS)
return TRUE;

return FALSE;
}

BOOL WRegKey::GetStringValue(LPCTSTR lpValueName, LPTSTR lpData, LPDWORD size)
{
char szClass[512];
DWORD cchClass = 512;
DWORD cSubKeys;
DWORD cchMaxSubkey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValueName;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime ;
DWORD dwType;
DWORD cbData;
char szValue[512];
DWORD cchValue;


if(RegQueryInfoKey (hKey, szClass, &cchClass, 0, &cSubKeys, &cchMaxSubkey,
&cchMaxClass, &cValues, &cchMaxValueName, &cbMaxValueData,
&cbSecurityDescriptor, &ftLastWriteTime) == ERROR_SUCCESS)
{
for ( int i = cValues -1; i >= 0; i--) {
cbData = *size;
cchValue = 512;
if(RegEnumValue(hKey, i, (LPTSTR) &szValue, &cchValue, NULL, &dwType,
( LPBYTE) lpData, &cbData) == ERROR_SUCCESS) {
if(lstrcmp(szValue,lpValueName) == 0) {
*size = cbData;
return TRUE;

}
}
else
return FALSE;

}
}
return FALSE;
}

Sorry I should say I'm using wxDevC++ for this project
Last edited on
this is my reg key when setting it as default with a different program. How can I use this in my c++ code?

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD]
@="CD Audio Track"
".cda"="CD Audio Track"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\DefaultIcon]
@="C:\\Creative CD\\Creative CD.exe,1"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\shell]
@="play"
"C:\\Creative CD\\Creative CD.exe"="play"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\shell\open]
@="&Open"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\shell\open\command]
@="\"C:\\Creative CD\\Creative CD.exe\" \"%1\""
"command"=hex(7):46,00,6e,00,34,00,70,00,61,00,31,00,5a,00,63,00,65,00,38,00,\
6c,00,51,00,21,00,52,00,56,00,6f,00,60,00,39,00,4b,00,2b,00,63,00,64,00,61,\
00,3e,00,5d,00,34,00,73,00,6d,00,52,00,48,00,78,00,2b,00,7e,00,3d,00,46,00,\
5f,00,77,00,2d,00,52,00,27,00,4d,00,2b,00,3f,00,6c,00,20,00,22,00,25,00,31,\
00,22,00,00,00,00,00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\shell\play]
@="&Play"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AudioCD\shell\play\command]
@="\"C:\\Creative CD\\Creative CD.exe\" /play %1"

Hi Thank you for trying to help. I figured it out.

Instead of Compiling with WxDevCpp I used Code::Blocks and the free
borland 5.5.1 Commandline tools. Now it works great.

Thanks for everyones help.
Mark it as Solved
Topic archived. No new replies allowed.