Solved

RegSetValueEx in C/C++ always fails

Posted on 2004-03-30
10
495 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

19 Experts available now in Live!

Get 1:1 Help Now