Автор темы
Codeby Academy
Практика и мастерство
От основ до продвинутого — программы для практиков Codeby.
К курсу →
- Статус
- Закрыто для дальнейших ответов.
Автор темы
Подскажите пожалуйста как сделать чтобы прога записывала себя в реестр??
RTFM
Код:
// QueryKey - enumerates the subkeys of a given key and the associated
// values and then copies the information about the keys and values
// into a pair of edit controls and list boxes.
// hDlg - dialog box that contains the edit controls and list boxes
// hKey - key whose subkeys and values are to be enumerated
VOID QueryKey(HWND hDlg, HANDLE hKey)
{
CHAR achKey[MAX_PATH];
CHAR achClass[MAX_PATH] = ""; // buffer for class name
DWORD cchClassName = MAX_PATH; // length of class string
DWORD cSubKeys; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, j;
DWORD retCode, retValue;
CHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
CHAR achBuff[80];
// Get the class name and the value count.
RegQueryInfoKey(hKey, // key handle
achClass, // buffer for class name
&cchClassName, // length of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
SetDlgItemText(hDlg, IDE_CLASS, achClass);
SetDlgItemInt(hDlg, IDE_CVALUES, cValues, FALSE);
SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG) "..");
// Enumerate the child keys, looping until RegEnumKey fails. Then
// get the name of each child key and copy it into the list box.
SetCursor(LoadCursor(NULL, IDC_WAIT));
for (i = 0, retCode = ERROR_SUCCESS;
retCode == ERROR_SUCCESS; i++)
{
retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
if (retCode == (DWORD)ERROR_SUCCESS)
{
SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG) achKey);
}
}
SetCursor(LoadCursor (NULL, IDC_ARROW));
// Enumerate the key values.
SetCursor(LoadCursor(NULL, IDC_WAIT));
if (cValues)
{
for (j = 0, retValue = ERROR_SUCCESS;
j < cValues; j++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retValue = RegEnumValue(hKey, j, achValue,
&cchValue,
NULL,
NULL, // &dwType,
NULL, // &bData,
NULL); // &bcData
if (retValue != (DWORD) ERROR_SUCCESS &&
retValue != ERROR_INSUFFICIENT_BUFFER)
{
wsprintf (achBuff,
"Line:%d 0 based index = %d, retValue = %d, "
"ValueLen = %d",
__LINE__, j, retValue, cchValue);
MessageBox (hDlg, achBuff, "Debug", MB_OK);
}
achBuff[0] = '\0';
// Add each value to a list box.
if (!lstrlen(achValue))
lstrcpy(achValue, "<NO NAME>");
wsprintf(achBuff, "%d) %s ", j, achValue);
SendMessage(GetDlgItem(hDlg,IDL_LISTBOX2),
LB_ADDSTRING, 0, (LONG) achBuff);
}
SetCursor(LoadCursor(NULL, IDC_ARROW));
}
Из мсдн
есть способ лучше и проще (но только если вы используете MFC)...
во-первых в функции
SetRegistryKey(_T("MyName");
ASSERT( m_pszRegistryKey != NULL );
if( m_pszProfileName != NULL )
free( (void*)m_pszProfileName );
m_pszProfileName = _tcsdup( _T("MyProgrammName") );
ASSERT( m_pszProfileName != NULL );
во-первых в функции
SetRegistryKey(_T("MyName");
ASSERT( m_pszRegistryKey != NULL );
if( m_pszProfileName != NULL )
free( (void*)m_pszProfileName );
m_pszProfileName = _tcsdup( _T("MyProgrammName") );
ASSERT( m_pszProfileName != NULL );
сорри...недописал...
Если вы работаете с MFC, то есть способ проще…
Во-первых в функции InitInstance(), класса производного от CWinApp необходимо добавить следующее:
SetRegistryKey(_T("MyCompanyName");
ASSERT( m_pszRegistryKey != NULL );
if( m_pszProfileName != NULL )
free( (void*)m_pszProfileName );
m_pszProfileName = _tcsdup( _T("MyProgrammName") );
ASSERT( m_pszProfileName != NULL );
Вся информация записывается в:
HKEY_LOCAL_MACHINE\SOFTWARE\MyCompanyName\MyProgrammName.
Пример для вызова будет такой:
AfxGetApp()->WriteProfileInt(“SerialPortSettings”,”PortNumber”,1);
Или для чтения:
INT mPortNumber = AfxGetApp()->GetProfileInt(“SerialPortSettings”,”PortNumber”,mDefaultValue);
Если такого ключа в реестре нет то, функция вернет mDefaultValue.
А затем, где вам это необходимо воспользоваться следующими функциями:
BOOL WriteProfileBinary(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPBYTE pData,
UINT nBytes
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
pData
Points to the data to be written.
nBytes
Contains the number of bytes to be written.
Return Value
Nonzero if successful; otherwise 0.
BOOL WriteProfileInt(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
int nValue
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
nValue
Contains the value to be written.
Return Value
Nonzero if successful; otherwise 0.
BOOL WriteProfileString(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPCTSTR lpszValue
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
lpszValue
Points to the string to be written. If this parameter is NULL, the entry specified by the lpszEntry parameter is deleted.
Return Value
Nonzero if successful; otherwise 0.
Соответственно для чтения существуют следующие функции:
CString GetProfileString(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPCTSTR lpszDefault = NULL
);
UINT GetProfileInt(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
int nDefault
);
BOOL GetProfileBinary(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPBYTE* ppData,
UINT* pBytes
);
Если вы работаете с MFC, то есть способ проще…
Во-первых в функции InitInstance(), класса производного от CWinApp необходимо добавить следующее:
SetRegistryKey(_T("MyCompanyName");
ASSERT( m_pszRegistryKey != NULL );
if( m_pszProfileName != NULL )
free( (void*)m_pszProfileName );
m_pszProfileName = _tcsdup( _T("MyProgrammName") );
ASSERT( m_pszProfileName != NULL );
Вся информация записывается в:
HKEY_LOCAL_MACHINE\SOFTWARE\MyCompanyName\MyProgrammName.
Пример для вызова будет такой:
AfxGetApp()->WriteProfileInt(“SerialPortSettings”,”PortNumber”,1);
Или для чтения:
INT mPortNumber = AfxGetApp()->GetProfileInt(“SerialPortSettings”,”PortNumber”,mDefaultValue);
Если такого ключа в реестре нет то, функция вернет mDefaultValue.
А затем, где вам это необходимо воспользоваться следующими функциями:
BOOL WriteProfileBinary(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPBYTE pData,
UINT nBytes
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
pData
Points to the data to be written.
nBytes
Contains the number of bytes to be written.
Return Value
Nonzero if successful; otherwise 0.
BOOL WriteProfileInt(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
int nValue
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
nValue
Contains the value to be written.
Return Value
Nonzero if successful; otherwise 0.
BOOL WriteProfileString(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPCTSTR lpszValue
);
Parameters
lpszSection
Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
lpszEntry
Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
lpszValue
Points to the string to be written. If this parameter is NULL, the entry specified by the lpszEntry parameter is deleted.
Return Value
Nonzero if successful; otherwise 0.
Соответственно для чтения существуют следующие функции:
CString GetProfileString(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPCTSTR lpszDefault = NULL
);
UINT GetProfileInt(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
int nDefault
);
BOOL GetProfileBinary(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPBYTE* ppData,
UINT* pBytes
);
- Статус
- Закрыто для дальнейших ответов.
Ещё по теме
4
Поделиться: