///////////////////////////////////////////////////////////////////////////// // Creation 18/12/2003 // // // REGISTRY.C // ---------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Show how to read/write data in registry // This example shows how to use registry function in a same manner as // using GetPrivateProfileString() and WritePrivateProfileString() // ///////////////////////////////////////////////////////////////////////////// #include #include #include // Read/Save data in registry #define CONFIG_IN_FILE 1 #if ! CONFIG_IN_FILE #define KEY_BASE_CONFIG HKEY_LOCAL_MACHINE #define KEY_CONFIG TEXT("SOFTWARE\\MySoftware\\MyProduct\\MyConfig") #else #define CONFIG_FILE "myconfig.ini" // Stored by default in windir // specify a complete path #endif ///////////////////////////////////////////////////////////////////////////// // // WRITECONFIGSTRING ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --WriteConfigString-- // // Write data in registry or in a configuration file // (CONFIG_IN_FILE=0/1) // // ARGUMENTS // Argument1: LPCTSTR pszSection // Argument2: LPCTSTR pszName // Argument3: LPCTSTR pszBuffer // RETOUR/RESULTAT // int // REMARQUE // Rev 18/12/2003 ////////////////////////////////////////////////////////////////////////////// int WriteConfigString( LPCTSTR pszSection, LPCTSTR pszName, LPCTSTR pszBuffer ) { #if CONFIG_IN_FILE WritePrivateProfileString( pszSection, pszName, pszBuffer, CONFIG_FILE ); #else HKEY hKey; long dwReturn; TCHAR szKey[250]; // Build key name _stprintf( szKey, TEXT("%s\\%s"), KEY_CONFIG, pszSection ); // Open key if ( RegOpenKeyEx( KEY_BASE_CONFIG, szKey, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) { DWORD dwDisposition; if( RegCreateKeyEx( KEY_BASE_CONFIG, szKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition ) != ERROR_SUCCESS) { return -1; } } dwReturn = 0; if( RegSetValueEx( hKey, pszName, 0, REG_SZ, (CONST BYTE *)pszBuffer, (_tcslen(pszBuffer) + 1) * sizeof(TCHAR) ) != ERROR_SUCCESS) { dwReturn = -1; } RegCloseKey (hKey); #endif return 0; } ///////////////////////////////////////////////////////////////////////////// // // GETCONFIGSTRING ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --GetConfigString-- // // Read data from registry or from configuration file // (CONFIG_IN_FILE=0/1) // // ARGUMENTS // Argument1: LPCTSTR pszSection // Argument2: LPCTSTR pszName // Argument3: LPCTSTR pszInitBuffer // Argument4: LPTSTR pszBuffer // Argument5: int cbBuffer // RETOUR/RESULTAT // int // REMARQUE // Rev 18/12/2003 ////////////////////////////////////////////////////////////////////////////// int GetConfigString( LPCTSTR pszSection, LPCTSTR pszName, LPCTSTR pszInitBuffer, LPTSTR pszBuffer, int cbBuffer ) { #if CONFIG_IN_FILE GetPrivateProfileString( pszSection, pszName, pszInitBuffer, pszBuffer, cbBuffer, CONFIG_FILE ); #else HKEY hKey; DWORD lpType; long dwReturn; char szKey[250]; // Build key name _stprintf( szKey, TEXT("%s\\%s"), KEY_CONFIG, pszSection ); // Open key. if ( RegOpenKeyEx( KEY_BASE_CONFIG, szKey, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { dwReturn = RegQueryValueEx(hKey, // handle to key to query pszName, // address of name of value to query NULL, // reserved &lpType, // address of buffer for value type (LPBYTE)pszBuffer, // address of data buffer (LPDWORD)&cbBuffer // address of data buffer size ); if ( dwReturn == ERROR_SUCCESS ) { RegCloseKey (hKey); return 0; } RegCloseKey (hKey); } if( pszInitBuffer != pszBuffer ) { _tcscpy( pszBuffer, pszInitBuffer ); } #endif return 0; } ///////////////////////////////////////////////////////////////////////////// // // MAIN ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --main-- // ARGUMENTS // RETOUR/RESULTAT // int // REMARQUE // Rev 18/12/2003 ////////////////////////////////////////////////////////////////////////////// int main() { char writedata[] = "coucou"; char readdata[256]; // Write some data printf( "Write data\n" ); WriteConfigString( "TheSection", "Name", writedata ); // Read the data GetConfigString( "TheSection", "Name", "nothing", readdata, sizeof(readdata)-1 ); printf( "data readen is '%s'\n", readdata ); return 0; }