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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new Registry entry and read it later

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I have searched previous threads and been reading in MSDN...but I could use a little help.

I have a program that needs to create a new location in the registry and save a value in the new registry location. Then, when the program is run again, it checks to see if the registry location had been created in a prior run and reads the value...and then increments it.

I've noticed a few threads on this but it didn't seem to help me. I'm looking for a code example.

FYI...I did look up CRegKey with ATL in MSDN...but I didn't find it to be all that useful.

thanks for the help.

/jbs
 
Hi,

Doing what you want isn't that difficult. First you need to open the Registry using CRegKey. Using the returned * call the Create Method to create the new key.

You then use QueryValue to get what you need if it fails the assume! first time access and then call SetValue.

In your case it's a simple manner of ++ the value and rewriting it to the registry.

What I've done it to write a wrapper Class around my Registry Access. I then create Functions that get/set a particular key. So in your case for example I would call an UpdateRunCount() function that would do everything in a "single" line of code.

HTH.
William
Software Engineer
ICQ No. 56047340
 
Good Morning William,

Thanks for the info. I did immediatly go to my trusty MSDN and did a search on CRegKey...with the hope of finding same example code that would guide me toward the light. However the MSDN entry for CRegKey is completly void of indepth discussion and example code. MSDN is usually great if not outstanding but their are still topics that are a bit low on detail/instructions for the novice.

Could you provide a reference to some sample code that will create a new hive entry for a program and allow me to store a value, retireve a value, and increment it?

thanks,

/jbs
 
Below I've hacked (and I mean hacked) out some of the code from my registry wrapper class and tweaked it to suit you. It should get you started.

Please let me know how you get on.

Header

class CRegistry
{
public:
CRegistry(void);
virtual ~CRegistry() ;
bool GetRunCount(void) ;
bool SetRunCount(void) ;
private:
char szBuff[255] ;
unsigned long dwRuns ;
HKEY m_hKeyParent ;
bool m_bOpen ;
const char * m_szKeyNode ;
bool CloseRegistry(void);
bool OpenRegistry(void);
CRegKey* m_pRegKey ;
const bool IsOpen(void) const { return m_bOpen ; } ;
};


Source

CRegistry::CRegistry() : dwRuns(0)
{
m_pRegKey = 0 ;
m_bOpen = false ;
m_hKeyParent = HKEY_CURRENT_USER ;
m_szKeyNode = "SOFTWARE\\JBS\\MyAppName" ;
}

CRegistry::~CRegistry()
{
if (m_pRegKey)
{
if (m_pRegKey->m_hKey)
{
if (CloseRegistry())
{ m_bOpen = false ; }
}
delete m_pRegKey ;
}
}

bool CRegistry::OpenRegistry()
{
if (m_pRegKey)
{ if (m_pRegKey->m_hKey) return true ; }
else
{
m_pRegKey = new CRegKey() ;
if (m_pRegKey)
{ m_bOpen = (m_pRegKey->Create(m_hKeyParent, m_szKeyNode) == ERROR_SUCCESS) ? true : false ; }
}
return m_bOpen ;
}

bool CRegistry::CloseRegistry()
{
if (m_pRegKey)
{ return (m_pRegKey->Close() == ERROR_SUCCESS) ? true : false ; }
return true ;
}

bool CRegistry::GetRunCount()
{
if (OpenRegistry())
{ return m_pRegKey->QueryValue(dwRuns, _T("Run Times")) == ERROR_SUCCESS ? dwRuns == 0 ? false : true : false ; }
return false ;
}

bool CRegistry::SetRunCount()
{
if (OpenRegistry())
{ return m_pRegKey->SetValue(++dwRuns, _T("Run Times")) == ERROR_SUCCESS ? true : false ; }
return false ;
}


William
Software Engineer
ICQ No. 56047340
 
...just finished the morning coffee....will work it now.

Thank you very much!....will let you know.

/jbs

p.s. This may be a good one for the FAQs..but let me try it out...
 
William,

FYI...still playing with it. Could get it to compile....

I plan on working on this one on Wed.

/jerry
 
Hi Jerry,

If you have any problems with it then let me know and I'll point you in the right direction. Since posting the code I have since tweaked (again) the section of code I hacked out.

I've tested it and it works as expected, so if you want it (or anyone else) then please let me know. As it stands it saves and gets both text and numbers in the registry.

:)



William
Software Engineer
ICQ No. 56047340
 
William,

Could you post it or email? x6153@att.net

Thanks. This is a topic that I really need to come up to speed on.

FYI, the error that I was getting was the line:

CRegKey* m_pRegKey ;

Error was:

"error C2146 syntax error: missing ';' before identifier m_pRegKey". Usually this error indicates an issue before the line...but I didn't see anything wrong.

I did put the header stuff in a header and the rest in a .cpp.

thanks so much for the help. Interesting topic.

/jerry
 
Jerry, you have new mail!
William
Software Engineer
ICQ No. 56047340
 
Just a side note. If the registry is not really required you can just use an ini file. See WriteProfileInt and GetProfileInt. There are others. As these names suggest, they write an int. Their usage is easier but here is a quick blurb of how I have done some registry stuff :)

Code:
HKEY m_hKey = HKEY_LOCAL_MACHINE;
HKEY hKey;
LONG err = RegOpenKeyEx(m_hKey, _T("SYSTEM\\CurrentControlSet\\Services\\Aspi32\\Parameters"), 0, 
	(KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_SUB_KEY | KEY_SET_VALUE), &hKey);

if (err == ERROR_SUCCESS)
{
	const unsigned char data[] = "";
	RegSetValueEx(hKey,"ExcludeMiniports",0,REG_SZ,data,0);
	::RegCloseKey(hKey);
}

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top