[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Set default registry keys?

Posted on 2005-04-23
11
Medium Priority
?
197 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:arnold100
  • 6
  • 5
11 Comments
 
LVL 7

Expert Comment

by:Nievergelt
ID: 13851018
Is the string you are using "C:\MYPROGRAM~1\MYPROGRAM"%1"" ?

Then it should be: "C:\\MYPROGRAM~1\\MYPROGRAM \"%1\""
0
 

Author Comment

by:arnold100
ID: 13851102
Sorry, I was using "C:\\MYPROGRAM~1\\PROGRAM.EXE"%1"". It wouldn't work. I will give yours a try.
0
 

Author Comment

by:arnold100
ID: 13851157
It still creates another default key and strips off the "%1"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:Nievergelt
ID: 13851189
Please post the code snippet in question.
0
 

Author Comment

by:arnold100
ID: 13851312
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
 
LVL 7

Expert Comment

by:Nievergelt
ID: 13851370
Assuming szFilePath contains the correct value, use this:

lnRes = RegSetValueEx(hKey,
                       "",  
                       0,      
                       REG_SZ,    
                       szFilePath,  
                       sizeof(szFilePath));
0
 

Author Comment

by:arnold100
ID: 13851434
Nievergelt,
Were gaining. The path is right and is setting the default key value now but it still strips off "%1\".
0
 
LVL 7

Accepted Solution

by:
Nievergelt earned 200 total points
ID: 13851476
Do add the "%1" to the szFilePath, e.g.

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

0
 

Author Comment

by:arnold100
ID: 13851576
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
 

Author Comment

by:arnold100
ID: 13852122
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
 
LVL 7

Expert Comment

by:Nievergelt
ID: 13852805
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question