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

how to link the ListBox to the files in the Registry 1

Status
Not open for further replies.

Amulet

Programmer
Sep 29, 2002
19
MY
hi there...

i am new with programming and now i have to use MFC to create a program for my new job.I have a program that list out the products of the company in the listbox. My task is, everytime i doubleclick on an element in the list, another window listing out the properties of the element(such as the name of the element, its type, its manufacturer etc). The properties are kept in the windows Registry.

How can i access the Registry from the list in ListBox?
i was told that i must used RegCreateKey and RegEnumKey.
i am not so sure on how to use them. How do i create the key to access the registry ?
 
this isn't pretty but it should do what you need.
the first parameter is the key name the second is a string to recieve the info and the third is a flag to set to either get a string from the reg TRUE or set info to the reg FALSE.
void GetOrSetRegValue(const CString& strKey, CString& strValue,BOOL bGet)
{
char szValue[40];
HKEY hKey;
DWORD dwDisp;
unsigned char* pBuff = (unsigned char*)szValue;
char szClass[40];
szClass[0] = '\0';

CString strSubKey = "SOFTWARE\\";
strSubKey += "MYKEYS\\";
int nResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
strSubKey,
0,
szClass,
REG_OPTION_NON_VOLATILE,
READ_CONTROL |
KEY_QUERY_VALUE |
KEY_SET_VALUE |
KEY_CREATE_SUB_KEY |
KEY_ENUMERATE_SUB_KEYS,
NULL,
&hKey,
&dwDisp);


if (nResult == ERROR_SUCCESS)
{
// If we just created this key the set the value
if(dwDisp == REG_CREATED_NEW_KEY)
{
strcpy(szValue, strValue);
nResult = RegSetValueEx( hKey,
strKey,
0,
REG_SZ,
pBuff,
strlen(szValue)+1);
}
// Key already existed
else if((dwDisp == REG_OPENED_EXISTING_KEY)&&(bGet))
{
DWORD dwType;
DWORD dwSize = sizeof(szValue);
nResult = RegQueryValueEx( hKey,
strKey,
0,
&dwType,
pBuff,
&dwSize);
// If we failed to read the value then set the value to the default
if (nResult != ERROR_SUCCESS)
{
strcpy(szValue, strValue);
nResult = RegSetValueEx( hKey,
strKey,
0,
REG_SZ,
pBuff,
strlen(szValue)+1);
}
}
else if((dwDisp == REG_OPENED_EXISTING_KEY)&&(!bGet))
{
strcpy(szValue, strValue);
nResult = RegSetValueEx( hKey,
strKey,
0,
REG_SZ,
pBuff,
strlen(szValue)+1);
}
// If we failed to create and didn't open an existing one
// (and yet somehow the create call didn't fail)
// Then do some default action
// (This is a serious error)
else
{
ASSERT(FALSE);
strcpy(szValue, strValue);
}

nResult = RegCloseKey(hKey);
}
// If the create failed then do some default action
// (This is a serious error)
else
{
strcpy(szValue, strValue);
}
strValue = szValue;
}
 
hi, pls help

how do i make the index number call the create key/check for existing key function to retrieve the value from the register
 
sorry, this is the complete enquiry: pls reply


Dear Olaf,

I indexed each element listed. say when u click on an element in the list box, then you click on the button select, it will display a message box indicating the index of that selected element.

void CRegistryCardsDlg::OnButtonSelect()
{
// TODO: Add your control notification handler code here
CListBox *lbptr = (CListBox *)GetDlgItem(IDC_LIST_CARDS);
char str1[100], str2[100];
int iListIndex;

iListIndex = lbptr->GetCurSel();

if(iListIndex==LB_ERR)wsprintf(str1, "No Selection");
else {
lbptr->GetText(iListIndex, str1);
wsprintf(str2, "at index %d.", iListIndex);
strcat(str1, str2);
}
MessageBox(str1, "Selection Made");
}

how do i make the index number call the create key/check for existing key function to retrieve the value from the registry? is indexing the elements in the listbox a hard way to access the registry? can you suggest a better way?
the biggest problem now is i dont know how to call the function of GetOrSetRegValue. should i call it OnButtonSelect?


thank you
 
Look at the function LVN_ITEMCHANGED. It's triggered each time you alter the selection in a listbox.
 
It really depends on how the info is stored in the registry.
I'll take a guess and say each element has its own key and the data is stored as strings i.e.
NAME "some name"
TYPE "some type"
MANUFACTURER "some manufacturer"
the GetOrSetRegValue is slightly missleading as the strKey is the name of the data ie NAME and not the key. You can see the key is hard coded in as "SOFTWARE\\MYKEYS\\" you would need to change the function so that you can pass in both the key name and the data name.
Then the simplest way,probabilly not the greatest, is to call each elements key something like "Element1","Element2",etc... this way you take your index and use it to generate the key name ie...
CString elementKey;
elementKey.Format("Element%d",index);
then you make a call to GetOrSetRegValue for each piece of data by passing it the element key and data name.
hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top