Solved

RegSetValueEx in C/C++ always fails

Posted on 2004-03-30
10
505 Views
Last Modified: 2013-12-03
I do not see why RegSetValueEx is always failing.  I have the following code


char *buffer = "CANDYS //GRUP";

if (RegSetValueEx(hKey, "RegName", 0, REG_SZ, (LPBYTE)buffer, strlen(buffer) + 1 ) != ERROR_SUCCESS){
MessageBox(hMaster, "Failed to set key value", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}

The hKey is valid, I tested it already.  It always displays the error, and I have tryed everything, why would it fail?

Can someone help me get this to work?
0
Comment
Question by:smitergrite
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10719458
Are you opening the key with KEY_WRITE or KEY_ALL_ACCESS?
0
 

Author Comment

by:smitergrite
ID: 10720346
Yes I am opening with KEY_ALL_ACCESS, I tryed replaceing it with 0 but still no success.

RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Softwate\\PopCap\\Bejeweled", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (hKey != ERROR_SUCCESS){
MessageBox(hMaster, "Failed to open registry key", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10721470
RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Softwate\\PopCap\\Bejeweled", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (hKey != ERROR_SUCCESS){
MessageBox(hMaster, "Failed to open registry key", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}

Should be:

if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Softwate\\PopCap\\Bejeweled", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS )
{
MessageBox(hMaster, "Failed to open registry key", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}

hKey should not be equal to NULL if RegCreateKeyEx succeeds.

Possibly RegCreateKeyEx failed, test this again. If any of function fails, please write what is it's return code.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:smitergrite
ID: 10721706
I changed the code as you said, but RegSetVaslueEx is still failing for some reason, here is the up2date code:


if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Softwate\\PopCap\\Bejeweled", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS )
{
MessageBox(hMaster, "Failed to open registry key", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}
if (RegSetValueEx(hKey, "RegName", 0, REG_SZ, (LPBYTE)buffer, strlen(buffer) + 1 ) != ERROR_SUCCESS)
{
MessageBox(hMaster, "Failed to set key value", "Fatal Error", MB_OK|MB_ICONERROR);
return FALSE;
}

Each time Failed to set key value is displayed, the open key always works fine.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10721751
What is return value of RegSetValueEx? What is hKey value?
0
 

Author Comment

by:smitergrite
ID: 10721856
The return value of RegSetValueEx is not ERROR_SUCCESS, so it is failing, I dont know how to messagebox that or display it so I can see.  I would give that value to you if i knew how to get it.

Same with hKey, through an if statement I can tell it is not null, so RegCreateKeyEx is successfull.  But its value I can't say because I dont know how to get it.

I only know RegCreateKeyEx works and RegSetValueEx fails
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10721937
LONG result = RegSetValueEx(hKey, "RegName", 0, REG_SZ, (LPBYTE)buffer, strlen(buffer) + 1 );
if ( result != ERROR_SUCCESS )
...

Stop in debugger and see result value.
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 10724445
I doubt that there is a key named "Softwate".
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10724787
Good point, but he creates it.
0
 

Author Comment

by:smitergrite
ID: 10729399
Well, neat of you to spot my spelling mistake, I totaly overlooked it.

Here are your points as that was the problem, solved now.

Incidently I looked in regedit but can't find any softwate. Oh well at least it works now.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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