Why does RegCreateKeyEx fails on Windows 2k?

Posted on 2005-04-27
Last Modified: 2013-11-20
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


Question by:arnold100
    LVL 30

    Expert Comment

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

    David Maisonave :-)

    Author Comment

    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.

    Author Comment

    Also if(ERROR_SUCCESS ! = inRes)
                AfxMessageBox("RegCreateKeyEx failed.");
    Messagebox pops up.
    LVL 14

    Expert Comment

    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.

    Author Comment

    I get error code 87.
    LVL 14

    Expert Comment

    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.


    Author Comment

    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.

    Author Comment

    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.  

    Author Comment

    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));,
    LVL 16

    Accepted Solution

    RegSetValueEx has 6 parameters. You posted only 4. Try

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

    Author Comment

    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,

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    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.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now