?
Solved

RegSetValueEx in C/C++ always fails

Posted on 2004-03-30
10
Medium Priority
?
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

719 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