|
各位,我现在想把接收到的IP,网关,子网等(放在char recvBuf[20]数组里)写到注册表里去,但现在的问题是有时可以有时不可以,出现乱码等,怀疑是写进去前没有把原来的键值清空,但RegDeleteValue后还是不行,各位帮我看一下,谢谢!
int nLen = strlen(recvBuf) + 1;
int nwLen = MultiByteToWideChar(CP_ACP,0,recvBuf, nLen, NULL,0);
WCHAR mszIPAddress[50];
MultiByteToWideChar(CP_ACP, 0,recvBuf, nLen, mszIPAddress, nwLen);//char to wchar
HKEY hKey;
DWORD dwDisposition;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Comm\\CS89001 \\Parms\\TcpIp"),0,KEY_WRITE,&hKey) != ERROR_SUCCESS)
AfxMessageBox(_T("error"));
int nIP = sizeof(mszIPAddress);
if(mszIPAddress[0]=='G')//输入的是G192.168.0.1表示网关,
{
long ret=RegDeleteValue(hKey,_T("Comm\\CS89001\\Parms\\TcpIp\\DefaultGateway"));//删除原来的键值
if(ret == ERROR_SUCCESS)
{
AfxMessageBox(_T("can not delete"));
}
for(int i=0;i
{
pGateway=mszIPAddress[i+1];
if(mszIPAddress[i+1]=='\0')
{ pGateway='\0';
break;
}
}
int j=0;
for(int i=0;i
{
if(pGateway!='\0')
j+=2;
else
break;
}
RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("Comm\\CS89001\\Parms\\TcpIp"),0,NULL,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition);
if(RegSetValueEx(hKey,TEXT("DefaultGateway"),0,REG_MULTI_SZ,(const unsigned char*)pGateway,j) != ERROR_SUCCESS)//写入网关
{
AfxMessageBox(_T("error"));
}
}
大家请帮帮忙,搞了几天了,还有问题,谢谢! |
|