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
Solved

RegSetValueEx in C/C++ always fails

Posted on 2004-03-30
10
509 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

809 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