Solved

RegSetValueEx in C/C++ always fails

Posted on 2004-03-30
10
489 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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