• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

Why does RegCreateKeyEx fails on Windows 2k?

Hi All,
I am working in vc++ 6 mfc. In my program the RegCreateKeyEx() is fialing in Windows 2k. Below is my code. Can someone tell me what I am doing wrong?

HKEY hKey;
LPTSTR  lpClass=_T("*.crp");
LPDWORD  lpdwDisposition;
unsigned char szFilePath[MAX_PATH];
char szModulePath[MAX_PATH];

GetModuleFileName(NULL, szModulePath, sizeof(szModulePath));
CString str1="  \"%1\"";
                  
lstrcpy((char *)szFilePath,LPCTSTR(szModulePath));
strcat((char*)szFilePath ,str1);  
//create a registry key        
LONG lnRes=RegCreateKeyEx(HKEY_CLASSES_ROOT,      // handle of an open key
                         ".crp\\shell\\open\\command",
                                             0,      // reserved
                         lpClass,      // address of class string
                         REG_OPTION_NON_VOLATILE,      // special options flag
                         KEY_WRITE,// desired security access
                         0,      // address of key security structure
                         &hKey,      // address of buffer for opened handle  
                         lpdwDisposition);      // address of disposition value buffer

Thanks,
Arnold.

0
arnold100
Asked:
arnold100
1 Solution
 
AxterCommented:
Hi arnold100,
How do you know it fails.
Please give specifics of the failure.

David Maisonave :-)
Cheers!
0
 
arnold100Author Commented:
When running on computer with windows 2k installed the key doesn't get set in the registry. Works fine on windows 98 se. The key get set.
0
 
arnold100Author Commented:
Also if(ERROR_SUCCESS ! = inRes)
       {
            AfxMessageBox("RegCreateKeyEx failed.");
       }
Messagebox pops up.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
waysideCommented:
Do this:

if(ERROR_SUCCESS ! = inRes)
       {
            CString msg;
            msg.Format(""RegCreateKeyEx failed. error code is %ld", inRes);
            AfxMessageBox(msg);
       }

This will give you the error code for why it fails.

I'm guessing 5: Access Denied.
0
 
arnold100Author Commented:
I get error code 87.
0
 
waysideCommented:
Error code 87: The parameter is incorrect.

(If you look in the tools menu of Visual Studio, you will see a tool called Error Lookup. Run it and enter the error code, and it tells you what the error message is.)

I would try using KEY_ALL_ACCESS instead of KEY_WRITE, and for the lpdwDisposition field you need to pass in the address of a DWORD, not a DWORD pointer:

DWORD  dwDisposition;
RegCreateKeyEx(...
                         &dwDisposition,
                         ...

Also, I'm not sure what the lpClass variable does, but it can be NULL, try NULL and see what happens.

0
 
arnold100Author Commented:
Hi wayside,
I did some reading on the Microsoft site and found that both the lpClass and lpdwDisposition values can be null. I will try the value above.
0
 
arnold100Author Commented:
lpClass is for setting remote key. Like if you were programming windows CE. My program works for me but I have a friend who runs Win 2k and he tests for me. He seems to be out at the moment. I will get back to as soon as I hear from him.  
0
 
arnold100Author Commented:
Still got problems on 2k system. It creates the keys ok but doesn't set the default value. Here is the code for RegSetValueEx.

lnRes=RegSetValueEx(hKey,
                                "",
                                0,
                                REG_SZ *sizeof(szModulePath));,
                               
0
 
nonubikCommented:
RegSetValueEx has 6 parameters. You posted only 4. Try

lRes = RegSetValueEx(hKey, "", 0, REG_SZ, (LPBYTE)szModulePath, strlen(szModulePath)+1);
0
 
arnold100Author Commented:
Sorry nonubik,
I had 6. I just made type Os. I work on a different computer then the one thats on the net, so I couldn't copy and paste. It works on windows 98 up now. Your code removed the "%1". That was the problem. %1 was not needed. Thanks to all who tried to help. I really appreciate it.

Thank you nonubik very much for the help also,
Arnold.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now