关于读取注册表键值的一个问题
我想读取HKEY_CURRENT_USER,Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\下ProxyServer的键值
数据是REG_SZ类型,内容是59.57.15.71:80,但是我读取的结果是个53,我是菜鸟不懂,求各位指点迷津,谢谢了。
HKEY hkey=HKEY_CURRENT_USER;
LPCSTR data_Set="Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\";
LONG res;
//DWORD datatype=REG_DWORD;
DWORD datatype2=REG_SZ;
DWORD dwValue = 1;
unsigned char vari[90];
DWORD ncbvari=20; //数值名称缓冲区大小
::RegOpenKeyEx(hkey,
data_Set, 0,
KEY_WRITE|KEY_READ, &hkey);
res = ::RegQueryValueEx(hkey,"ProxyServer",0, &datatype2,vari,&ncbvari);
if(res==ERROR_SUCCESS)
MessageBox(NULL,"find!","提示",MB_OK);
else
MessageBox(NULL,"no ","提示",MB_OK);
String str;
str=*vari;
Edit3->Text=*vari;
RegCloseKey(hkey);
FileList1Click(Sender);
}
}
//---------------------------------------
void __fastcall TForm1::FileList1Click(TObject *Sender)
{
String TempStr;
TRegistry *reg=new TRegistry;
try{
reg->RootKey=HKEY_CURRENT_USER;
if(reg->OpenKey("\\Software\\Science\\File List",false)){
TempStr=reg->ReadString("1");
if(TempStr!=""){
FileList1->Caption=TempStr+"(&1)";
FileList1->Visible=true;
}else{
FileList1->Visible=false;
}
TempStr=reg->ReadString("2");
if(TempStr!=""){
FileList2->Caption=TempStr+"(&2)";
FileList2->Visible=true;
}else{
FileList2->Visible=false;
}
TempStr=reg->ReadString("3");
if(TempStr!=""){
FileList3->Caption=TempStr+"(&3)";
FileList3->Visible=true;
}else{
FileList3->Visible=false;
}
}
}__finally{
reg->CloseKey();
delete reg;
}
}
//---------------------------------------
void __fastcall TForm1::MainMenu1Change(TObject *Sender, TMenuItem *Source, bool Rebuild)
{
FileList1Click(Sender);
}
//---------------------------------------
[解决办法]
#include <Registry.hpp>
AnsiString GetRegistryValue(AnsiString KeyName)
{
AnsiString S;
TRegistry *Registry = new TRegistry(KEY_READ);
try
{
Registry->RootKey = HKEY_LOCAL_MACHINE;
// False because we do not want to create it if it doesn't exist
Registry->OpenKey(KeyName,false);
S = Registry->ReadString("VALUE1");
}
__finally
{
delete Registry;
}
return S;
}
[解决办法]
将unsigned char vari[90];
改为char vari[90];
即可,不过你为什么要写90呢..浪费空间啊..
最后
String str;
str=*vari;
Edit3->Text=*vari;
这里又做了件无意义的事情
str变量没有做任何事情..