Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help understanding some code

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
Hi,

Could someone explain the VBA code below (Its using a windows API)

If RegOpenKeyA(MyKey, Path, hKey) <> 0 Then
y = regCreateKeyA(MyKey, Path, hKey)
End If

Thanks,

Chris
 
RegOpenKeyA attempts to open a Registry key. If it is successful, it will return a value of 0. If it doesn't find the Registry key passed in, the return value will not be 0. The If statement checks for the return value...if it is not equal to 0, it didn't find the Registry key, so it uses the RegCreateKeyA to create the Registry key.

Info on RegOpenKeyA:

Info on RegCreateKeyA:
 
How comes when I delete the reg key in question. The function still returns 0?

I can't seem to get the if statement to return false at the moment.

Thanks,

Chris
 
Thanks. Regarding the code I posted (I just wanted to understand what the author was thinking) Do you have any ideas? I mean in practice if the key is not there it still returns 0...

It may be academic, but do you have any ideas?

Thanks a lot,

Chris
 
The If statement you posted is checking for an error code, or a non-zero return value. If it returns a zero, then no error occured, in theory.
 
Hmm so in practice not finding a key in the registry is not an error?
 
Chrissirhc said:
Has anyone had this experience with this reg key?
What reg key?

From your other thread I know this is related to code from a book that you can't post here and as I don't have the book I can't really help much.

I am not aware of any issues with this API; if it returns success it has had success. Are you absolutely sure you are trying to open the key you think you are and that it doesn't exist? If you use the handle from the Open to read a value (which you say doesn't exist), what do you get?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top