Discussion:
RegOpenKeyEx() returns ERROR_ACCESS_DENIED
(too old to reply)
Jo
2006-04-07 14:12:01 UTC
Permalink
And here's the code that does it:

HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0, 0,
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;

If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting the
key and recreating it with with:

HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);

as the same user which returns ERROR_SUCCESS.
Ajay Kalra
2006-04-07 15:49:17 UTC
Permalink
If you are able to create and delete it, then opening should work. Is
it by any chance open elsewhere for writing?

---
Ajay
Jo
2006-04-07 16:52:46 UTC
Permalink
Post by Ajay Kalra
If you are able to create and delete it, then opening should work. Is
I'm able to create it and delete it in RegEdit, but I'm not able to
delete it in my program, since I need to call RegOpenKeyEx() on it
first.
Post by Ajay Kalra
it by any chance open elsewhere for writing?
I doubt it. I closed RegEdit, and it isn't open anywhere else in my
app. I can't see any reason why any other process would open it. I even
tried restarting in case something somewhere had a lock on it.
AliR
2006-04-07 18:17:16 UTC
Permalink
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do that.
I would try something in HKEY_CURRENT_USER.

AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0, 0,
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting the
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
Jo
2006-04-07 18:42:04 UTC
Permalink
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do that.
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears to
have the exact same effect: ERROR_ACCESS_DENIED

This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things with
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?

Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0, 0,
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting the
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
AliR
2006-04-07 19:12:07 UTC
Permalink
Did you try KEY_ALL_ACCESS????

Both of these worked ok for me
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft", 0,
KEY_ALL_ACCESS,&hKey);

LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Intel", 0,
KEY_ALL_ACCESS,&hKey);

AliR.
Post by Jo
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do that.
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears to
have the exact same effect: ERROR_ACCESS_DENIED
This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things with
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?
Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0, 0,
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting the
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
Jo
2006-04-07 19:52:49 UTC
Permalink
Post by AliR
Did you try KEY_ALL_ACCESS????
I did now, against the recommendation of the MSDN which describes
that parameter as: "Not supported; set to 0. " and it worked. What's
that all about?
Post by AliR
Both of these worked ok for me
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft", 0,
KEY_ALL_ACCESS,&hKey);
LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Intel", 0,
KEY_ALL_ACCESS,&hKey);
AliR.
Post by Jo
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do
that.
Post by Jo
Post by AliR
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears to
have the exact same effect: ERROR_ACCESS_DENIED
This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things with
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?
Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
0,
Post by Jo
Post by AliR
Post by Jo
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting
the
Post by Jo
Post by AliR
Post by Jo
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
AliR
2006-04-07 19:53:32 UTC
Permalink
I think you are getting the ulOptions and samDesired parameters mixed up!

LONG RegOpenKeyEx(
HKEY hKey,
LPCTSTR lpSubKey,
DWORD ulOptions,
REGSAM samDesired,
PHKEY phkResult
);

...
ulOptions
Reserved; must be zero.
samDesired
[in] Access mask that specifies the desired access rights to the key. The
function fails if the security descriptor of the key does not permit the
requested access for the calling process. For more information, see Registry
Key Security and Access Rights.


AliR.
Post by Jo
Post by AliR
Did you try KEY_ALL_ACCESS????
I did now, against the recommendation of the MSDN which describes
that parameter as: "Not supported; set to 0. " and it worked. What's
that all about?
Post by AliR
Both of these worked ok for me
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft", 0,
KEY_ALL_ACCESS,&hKey);
LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Intel", 0,
KEY_ALL_ACCESS,&hKey);
AliR.
Post by Jo
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do
that.
Post by Jo
Post by AliR
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears to
have the exact same effect: ERROR_ACCESS_DENIED
This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things with
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?
Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
0,
Post by Jo
Post by AliR
Post by Jo
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting
the
Post by Jo
Post by AliR
Post by Jo
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
"Software\\mycompany", 0,
Post by Jo
Post by AliR
Post by Jo
Post by AliR
Post by Jo
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
Jo
2006-04-07 20:04:09 UTC
Permalink
Post by AliR
I think you are getting the ulOptions and samDesired parameters mixed up!
No, I copied and pasted that text right out of the MSDN which
directs that both ulOptions and samDesired should be 0. When I say
"MSDN" I mean the Oct 2001 publication I have *and* the one Microsoft
hosts on the web. I guess I should extend the "you can't believe
everything you read" philosophy to include Microsoft publications.

Thanks.
Post by AliR
LONG RegOpenKeyEx(
HKEY hKey,
LPCTSTR lpSubKey,
DWORD ulOptions,
REGSAM samDesired,
PHKEY phkResult
);
...
ulOptions
Reserved; must be zero.
samDesired
[in] Access mask that specifies the desired access rights to the key. The
function fails if the security descriptor of the key does not permit the
requested access for the calling process. For more information, see Registry
Key Security and Access Rights.
AliR.
Post by Jo
Post by AliR
Did you try KEY_ALL_ACCESS????
I did now, against the recommendation of the MSDN which describes
that parameter as: "Not supported; set to 0. " and it worked. What's
that all about?
Post by AliR
Both of these worked ok for me
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft", 0,
KEY_ALL_ACCESS,&hKey);
LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Intel", 0,
KEY_ALL_ACCESS,&hKey);
AliR.
Post by Jo
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany",
0,
Post by Jo
Post by AliR
Post by Jo
Post by AliR
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do
that.
Post by Jo
Post by AliR
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears
to
Post by Jo
Post by AliR
Post by Jo
have the exact same effect: ERROR_ACCESS_DENIED
This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things
with
Post by Jo
Post by AliR
Post by Jo
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?
Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany",
0,
Post by Jo
Post by AliR
0,
Post by Jo
Post by AliR
Post by Jo
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED.
I've
Post by Jo
Post by AliR
Post by Jo
Post by AliR
Post by Jo
been all through the access permissions and I've given full access
to
Post by Jo
Post by AliR
Post by Jo
Post by AliR
Post by Jo
everything. I've tried creating the key with regedit, then
deleting
Post by Jo
Post by AliR
the
Post by Jo
Post by AliR
Post by Jo
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
"Software\\mycompany", 0,
Post by Jo
Post by AliR
Post by Jo
Post by AliR
Post by Jo
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
&dwDummy);
Post by Jo
Post by AliR
Post by Jo
Post by AliR
Post by Jo
as the same user which returns ERROR_SUCCESS.
DavidCrow
2006-04-07 20:38:51 UTC
Permalink
I too use the Oct 2001 version and it indeed says that ulOptions should
be 0, not samDesired.
Post by Jo
No, I copied and pasted that text right out of the MSDN which
directs that both ulOptions and samDesired should be 0. When I say
"MSDN" I mean the Oct 2001 publication I have *and* the one Microsoft
hosts on the web. I guess I should extend the "you can't believe
everything you read" philosophy to include Microsoft publications.
Jo
2006-04-07 21:06:16 UTC
Permalink
Post by DavidCrow
I too use the Oct 2001 version and it indeed says that ulOptions should
be 0, not samDesired.
Does it really..?

Well, here's why: I was looking at the Windows CE page on
RegOpenKeyEx(). The PSDK has the right one. That's the first time I've
found an inconsistency between them. I'll have to keep my eye open for
that next time.
Post by DavidCrow
Post by Jo
No, I copied and pasted that text right out of the MSDN which
directs that both ulOptions and samDesired should be 0. When I say
"MSDN" I mean the Oct 2001 publication I have *and* the one Microsoft
hosts on the web. I guess I should extend the "you can't believe
everything you read" philosophy to include Microsoft publications.
DavidCrow
2006-04-10 17:41:03 UTC
Permalink
Had you mentioned that you were using CE initially, we might've found
the solution a bit sooner.
Post by Jo
Well, here's why: I was looking at the Windows CE page on
RegOpenKeyEx().
Ajay Kalra
2006-04-10 17:48:15 UTC
Permalink
I think OP isnt using CE but was looking at help page for CE. Using CE
help page is very common in MSDN andn IMO, there should be an option to
turn it off.

---
Ajay
AliR
2006-04-10 17:55:13 UTC
Permalink
The solution had nothing to do with CE.

AliR.
Post by DavidCrow
Had you mentioned that you were using CE initially, we might've found
the solution a bit sooner.
Post by Jo
Well, here's why: I was looking at the Windows CE page on
RegOpenKeyEx().
AliR
2006-04-07 19:14:13 UTC
Permalink
As far as the access to HKEY_LOCAL_MACHINE goes, generally only the admin
account will have access to that.

AliR.
Post by Jo
Post by AliR
Does it work if you set the access right to KEY_ALL_ACCESS?
Post by Jo
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
KEY_ALL_ACCESS,
Post by Jo
&hKey);
also try to open something else in the registry to see if you can do that.
I would try something in HKEY_CURRENT_USER.
Ok, I tried opening HKEY_CURRENT_USER\Software\Intel. It appears to
have the exact same effect: ERROR_ACCESS_DENIED
This brings another question to mind: I'm debugging my program in
an administrator account, but I need to be able to do these things with
an ordinary user. Will and ordinary user be unable to write to
HKEY_LOCAL_MACHINE in XP (and upward)?
Perhaps I should point out that the workaround for the OP is to
use RegCreateKeyEx() (which is the same one I used to create the key)
instead of RegOpenKeyEx(). I don't have any problems whether the key
already exists or not. However, I want to use RegOpenKeyEx(), but the
difference in behavior doesn't make sense to me.
Post by AliR
AliR.
Post by Jo
HKEY hKey;
LONG dw = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0, 0,
&hKey);
if (dw != ERROR_SUCCESS)
return FALSE;
If I step thought it I find dw == 5 which is ERROR_ACCESS_DENIED. I've
been all through the access permissions and I've given full access to
everything. I've tried creating the key with regedit, then deleting the
HKEY hKey;
DWORD dwDummy;
LONG dw = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\mycompany", 0,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDummy);
as the same user which returns ERROR_SUCCESS.
Loading...