Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

registry function doesn't work :(

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The registry key does exist but for some reason it always fails to get it :(
fw::reg::HKey Key = fw::reg::OpenKey(reg::LOCAL_MACHINE, "SOFTWARE\\TestKey");
char* Value = (char*)reg::GetValue(Key, "TestStr");


HKey OpenKey(const HKey &Key, const std::string &SubKey)
{
	HKEY Out;
	if(RegOpenKeyEx((HKEY)Key, SubKey.c_str(), NULL, KEY_ALL_ACCESS, &Out)
		==ERROR_SUCCESS)
	{
		return (HKey)Out;
	}
	else
	{
		RegCloseKey   ((HKEY)Out);
		return NULL;
	}
}
...
void* GetValue(const HKey &Key, const std::string &Name)
{
	unsigned Size = 0;
	if(RegQueryValueEx((HKEY)Key, Name.c_str(), NULL, NULL, NULL, (unsigned long*)&Size)
		!= ERROR_SUCCESS)
	{
		//always fails here
		return NULL;
	}
	unsigned char* Data = new unsigned char[Size];
	if(RegQueryValueEx((HKEY)Key, Name.c_str(), NULL, NULL, Data, (unsigned long*)&Size)
		!= ERROR_SUCCESS)
	{
		return (void*)Data;
	}
	else
	{
		return NULL;
	}
}


GetLastError() returns 183 which is ERROR_ALREADY_EXISTS but I'm not trying to create anything just read it..... [Edited by - Sync Views on June 7, 2008 3:58:51 PM]

Share this post


Link to post
Share on other sites
Advertisement
The RegOpenKeyEx() allows you permission to operate on a key, trying to get full permissions to anything in Windows can be rough so, as a general rule of thumb only operate with the LOWEST Permissions possible.
RegOpenKeyEx((HKEY)Key, SubKey.c_str(), NULL, KEY_QUERY_VALUE, &Out)



Also, if the "key" you want exists as such:
SYSTEM\Key\

Then there's no reason why it's not working, however in the situation "key" isn't a subtree itself and just a value under system as:
SYSTEMName: MyVal; Type: Value:

Then you would want to do

RegQueryValueEx((HKEY)hKey, TEXT("TestKey"), 0, ?, Buffer, 0,)

Share this post


Link to post
Share on other sites
I don't know why you can't open keys but this is very bad:

else
{
RegCloseKey ((HKEY)Out);
return NULL;
}

If you failed to open the key don't go trying to close what isn't open. At this point you are probably calling RegCloseKey on unitialized stack garbage. If the random value just happened to match the handle for something important then you've potentially hosed your process.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!