Set default registry keys?

Hi All,
I am working in vc++ 6. I need help with setting the default value of a registry key. I can create registry keys and set them ok with RegCreateKeyEx() and RegSetValueEx(). My problem is I can't for the life of me figure out how to set the default key value to "C:\MYPROGRAM~1\MYPROGRAM"%1"". When I try to  create it as the default key it creates another key and leaves off the "%1" part. What I am tring to do is the same thing that happens when you highlight a file in Windows Explorer, Ctrl/Shift and select Open with. If you check Always open with, this sets up a registry key for that program and file extenstion. Does anyone know how to do this?

Thanks,
Arnold.
arnold100Asked:
Who is Participating?
 
NievergeltConnect With a Mentor Senior SW DevCommented:
Do add the "%1" to the szFilePath, e.g.

strcpy(szFilePath, szModulePath);
strcat(szFilePath, " \"%1\"");

0
 
NievergeltSenior SW DevCommented:
Is the string you are using "C:\MYPROGRAM~1\MYPROGRAM"%1"" ?

Then it should be: "C:\\MYPROGRAM~1\\MYPROGRAM \"%1\""
0
 
arnold100Author Commented:
Sorry, I was using "C:\\MYPROGRAM~1\\PROGRAM.EXE"%1"". It wouldn't work. I will give yours a try.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
arnold100Author Commented:
It still creates another default key and strips off the "%1"
0
 
NievergeltSenior SW DevCommented:
Please post the code snippet in question.
0
 
arnold100Author Commented:
Here it is:
sPath=Recurse(_T("C:"));//Gets the path to the program
      sPath+=_T("\\Myprogram.exe\"%1\"");

RegCreateKeyEx(HKEY_CLASSES_ROOT,      
                         "crp_auto_file\\shell\\open\\command",                         NULL,      // reserved
                         lpClass,      
                         REG_OPTION_NON_VOLATILE,
                         KEY_CREATE_SUB_KEY,
                         NULL,
                         &hKey,  
                         lpdwDisposition);
                                                   
                       //open the registry key
 LONG lnRes=RegOpenKeyEx            (HKEY_CLASSES_ROOT,
"crp_auto_file\\shell\\open\\command",0L,KEY_WRITE,  &hKey);                  
           // now add program path to the key
            char szModulePath[MAX_PATH];
            GetModuleFileName(NULL, szModulePath, sizeof(szModulePath));  
lstrcpy( (char *) szFilePath ,LPCTSTR(szModulePath));

            if( ERROR_SUCCESS == lnRes )
            {
lnRes = RegSetValueEx(hKey,
                        LPCTSTR("(Default)"),  
                        0,      
                        REG_SZ,    
                        szFilePath,  
             REG_SZ * sizeof(szModulePath));
0
 
NievergeltSenior SW DevCommented:
Assuming szFilePath contains the correct value, use this:

lnRes = RegSetValueEx(hKey,
                       "",  
                       0,      
                       REG_SZ,    
                       szFilePath,  
                       sizeof(szFilePath));
0
 
arnold100Author Commented:
Nievergelt,
Were gaining. The path is right and is setting the default key value now but it still strips off "%1\".
0
 
arnold100Author Commented:
I get a compiler error can't convert parameter 1 from unsigned char to char*.  Parameter 5 of RegSetValueEx has to be unsigned char.
0
 
arnold100Author Commented:
Nievergelt,
I Got it. Here is the fix:
CString str="\"%1\"";
strcat((char *)szFilePath,str);

Works perfectly now. You pointed me in the right direction.

Thanks for the help,
Arnold.
0
 
NievergeltSenior SW DevCommented:
Sorry, I missed that. Glad I could help you.

Two things to watch out for:
- also quote the program with the path (or at least if that string contains a blank). Otherwise it will not work correctly as soon as the path contains a blank.
- put a blank before the "%1" (to separate it from the ending quote of program path)

Share and Enjoy  Christoph
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.