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
                                             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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi arnold100,
How do you know it fails.
Please give specifics of the failure.

David Maisonave :-)
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.
arnold100Author Commented:
Also if(ERROR_SUCCESS ! = inRes)
            AfxMessageBox("RegCreateKeyEx failed.");
Messagebox pops up.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Do this:

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

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

I'm guessing 5: Access Denied.
arnold100Author Commented:
I get error code 87.
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;

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

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.
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.  
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.

                                REG_SZ *sizeof(szModulePath));,
RegSetValueEx has 6 parameters. You posted only 4. Try

lRes = RegSetValueEx(hKey, "", 0, REG_SZ, (LPBYTE)szModulePath, strlen(szModulePath)+1);

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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,
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.