/// Attempts to write values to the registry /** * @warning * This is only an example, NEVER expose the registry like this in your API! * @see * http://msdn.microsoft.com/en-us/library/b2hs0tae.aspx * @pre * None * @post * The registry now contains the subkey CurrentUser/Software/productName * @param productName * The name of the subkey to create * @param majorVersion * A value to add to the productName subkey * @param minorVersion * A value to add to the productName subkey * @return * True if the operation completed successfully, false if an exception was encountered * @throw * This method will not throw */ using namespace System; static Boolean writeRegistry(String^ productName, Int32 majorVersion, Int32 minorVersion) { using Microsoft::Win32::RegistryValueKind; using Microsoft::Win32::RegistryKey; using Microsoft::Win32::Registry; using System::IO::IOException; using System::Security::SecurityException; try { RegistryKey^ currentUser = Registry::CurrentUser; RegistryKey^ software = currentUser->OpenSubKey("Software", true); RegistryKey^ product = software->CreateSubKey(productName); product->SetValue("ProductName", productName, RegistryValueKind::String); product->SetValue("MajorVersion", majorVersion, RegistryValueKind::DWord); product->SetValue("MinorVersion", minorVersion, RegistryValueKind::DWord); product->Close(); software->Close(); } catch(ArgumentNullException^) { Console::WriteLine("Error - The product name cannot be empty."); return false; } catch(SecurityException^) { Console::WriteLine("Error - This user account is not permitted to create or open the registry key(s)."); return false; } catch(ObjectDisposedException^) { Console::WriteLine("Error - A registry key was unexpectedly closed."); return false; } catch(UnauthorizedAccessException^) { Console::WriteLine("Error - This user account is not permitted to write to the registry key(s)."); return false; } catch(IOException^) { Console::WriteLine("Error - An unexpected error has occurred, this may happen if the nesting level exceeds 510."); return false; } return true; }
Monday, April 18, 2011
C++/CLI: Writing to the Windows Registry
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment