请问如何修改注册表中REG_MULTI_SZ类型的IP地址
我想写一个能够自己修改IP的程序,查了好多资料,最后确定可以用ReadBinaryData函数和WriteBinaryData函数来读写IP。目前读IP已经成功,但是修改IP却一直不对,我试了好多次,不是写进去是乱码就是IP的ASC||码,而且写的IP类型也不是REG_MULTI_SZ。不知道为什么,请各位帮帮我,我的具体函数如下:
bool IP_Message(AnsiString &str,AnsiString operate)
{
int BuffLength;
char *Buff;
TRegistry *Registry;
Registry=new TRegistry();
try
{
Registry-> RootKey = HKEY_LOCAL_MACHINE;
if( Registry-> OpenKey( "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{B9051EFF-1F98-4FC6-AE6E-8C5A141E7F09} ",FALSE))
{
if(operate!= "read ")
{//写IP,IP类型为REG_MULTI_SZ
BuffLength= str.Length();
Buff=str.c_str();
Registry-> WriteBinaryData( "IPAddress ",&Buff,BuffLength);
Registry-> CloseKey();
}
else
{//读IP
BuffLength = Registry-> GetDataSize( "IPAddress ");
Buff = new char[BuffLength+1];
Registry-> ReadBinaryData( "IPAddress ",Buff,BuffLength);
str = (AnsiString)Buff;
Registry-> CloseKey();
}
return true;
}
else
{//没有找到则创建该地址
if(operate!= "read ")
{
Registry-> CreateKey( "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{B9051EFF-1F98-4FC6-AE6E-8C5A141E7F09} ");
Registry-> WriteString( "IPAddress ",str);
Registry-> CloseKey();
}
else
{
str= "无法获取IP信息 ";
Registry-> CloseKey();
}
return true;
}
}
__finally
{
Registry-> CloseKey();
delete Registry;
return false;
}
}
我估计应该主要是WriteBinaryData使用有问题,请大哥帮我分析下,谢谢了!
[解决办法]
Registry-> WriteBinaryData( "IPAddress ",Buff,BuffLength); 注意是Buff 不是 &Buff