Thursday, January 15, 2009

How to create REG_MULTI_SZ and REG_BINARY in C++ and C#

1. REG_MULTI_SZ

C#:

string[] ConfigurationString = new string[8];
ConfigurationString[0] = Location;
ConfigurationString[1] = LocalCalls;
ConfigurationString[2] = LongDistanceCalls;
ConfigurationString[3] = InternationalCalls;
ConfigurationString[4] = AreaCode;
ConfigurationString[5] = DisableCallWaitingSequence;
ConfigurationString[6] = CountryCode;
ConfigurationString[7] = ToneOrPulse;

RegistryKey rk = Registry.CurrentUser;
using (RegistryKey rk1 = rk.CreateSubKey("ControlPanel\
\Dial\\Locations"))
{
rk1.SetValue(KeyName, ConfigurationString);
rk1.Close();
}

C++: (not tested yet!)

HKEY hKey = NULL;
DWORD dataType = REG_MULTI_SZ;
LONG retVal = 0;
LONG ConfigurationStringSize = 512;
WCHAR *ConfigurationString = new WCHAR[ConfigurationStringSize];
memset(ConfigurationString,'\0',512);

// Build the configuration string.
wcscat(ConfigurationString, Location);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, LocalCalls);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, LongDistanceCalls);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, InternationalCalls);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, AreaCode);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, DisableCallWaitingSequence);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, CountryCode);
wcscat(ConfigurationString, TEXT("$"));

wcscat(ConfigurationString, ToneOrPulse);
wcscat(ConfigurationString, TEXT("$"));

// Format the string correctly.
int ConfigStringLen = wcslen(ConfigurationString);
for(int x = 0; x < ConfigStringLen; ++x)
{
if(ConfigurationString[x] == '$')
ConfigurationString[x] = '\0';
}

// Open the registry key.
retVal = RegOpenKeyEx(HKEY_CURRENT_USER , TEXT("ControlPanel\\Dial\\Locations"), 0, KEY_READ, &hKey);
if(retVal != ERROR_SUCCESS)
{
delete [] ConfigurationString;
return false;
}

// Set the value of the key.
retVal = RegSetValueEx(hKey, KeyName, NULL, dataType, (PBYTE)ConfigurationString, ConfigurationStringSize);
RegCloseKey(hKey);

delete [] ConfigurationString;

return retVal != ERROR_SUCCESS ? false : true;
-------------------------------------------------------------------------------
2. REG_BINARY

C#:
private bool GetBinaryBluetoothAddress(string sBluetoothAddress, ref byte[] bBTAddress)
{
try
{
//00:03:c9:56:0c:EE
string[] sTmp = sBluetoothAddress.Split(':');
bBTAddress[0] = Convert.ToByte(sTmp[5], 16);
bBTAddress[1] = Convert.ToByte(sTmp[4], 16);
bBTAddress[2] = Convert.ToByte(sTmp[3], 16);
bBTAddress[3] = Convert.ToByte(sTmp[2], 16);
bBTAddress[4] = Convert.ToByte(sTmp[1], 16);
bBTAddress[5] = Convert.ToByte(sTmp[0], 16);
bBTAddress[6] = 0;
bBTAddress[7] = 0;

return true;
}
catch
{
return false;
}
}

RegistryKey rk = Registry.LocalMachine;
using (RegistryKey rk1 = rk.CreateSubKey("\\Software\\dx"))
{
byte[] bA = new byte[8];
if (GetBinaryBluetoothAddress("00:03:c9:56:0c:EE", ref bA))
{
rk1.SetValue("dxaddress", bA);
MessageBox.Show("Good!");
}
else
MessageBox.Show("Failed!");

rk1.Close();
}
rk.Close();

C++:

unsigned char epwd[9];

//set epwd here

RegSetValueEx(hKey,"keyname",0,REG_BINARY,(LPBYTE)&epwd,8);

No comments: