// // Loop through in case the information from RAS changes between calls. // while (dwRetries--) { // // If the memory is allocated, free it. // if (NULL != lpRasEntryName) { HeapFree(GetProcessHeap(), 0, lpRasEntryName); lpRasEntryName = NULL; } // // Allocate the size need for the RAS structure. // lpRasEntryName = (RASENTRYNAME*)HeapAlloc(GetProcessHeap(), 0, dwCb); if (NULL == lpRasEntryName) { dwErr = ERROR_NOT_ENOUGH_MEMORY; break; } // // Set the structure size for version checking purposes. // lpRasEntryName->dwSize = sizeof(RASENTRYNAME); // // Call the RAS API, bail on the loop if we are successful or an unknown // error occurs. // dwErr = RasEnumEntries( NULL, NULL, lpRasEntryName, &dwCb, &dwEntries); if (ERROR_BUFFER_TOO_SMALL != dwErr) { break; } } // // In the success case print the names of the entries. // if (ERROR_SUCCESS == dwErr) { DWORD i; for (i = 0; i < dwEntries; i++) { m_listbox.InsertString(i,lpRasEntryName[i].szEntryName); } } else { CString errMsg; errMsg.Format("RasEnumEntries failed: Error = %d\n",dwErr); AfxMessageBox(errMsg); } // // Free the memory if necessary. // if (NULL != lpRasEntryName) { HeapFree(GetProcessHeap(), 0, lpRasEntryName); lpRasEntryName = NULL; } } /*通过LISTBOX,获取PHONEBOOK ENTRY的名称*/ void CRasDemoDlg::OnBnClickedButton2() //Update the selected Entry's Properties { int index = m_listbox.GetCurSel(); if ( -1 != index ) { int msgBoxResult = MessageBox("Are you sure?","Attention Please",MB_YESNO|MB_ICONQUESTION); if( msgBoxResult == IDYES ) { CString EntryName; m_listbox.GetText(index,EntryName); if( TRUE == UpdateRasEntry(EntryName) ) { AfxMessageBox("Updated succeeded!"); } else AfxMessageBox("Updated failed!"); } } else { AfxMessageBox("Please select a phonebook entry first!"); } } /*通过从LISTBOX获取的ENTRY NAME,更新指定的ENTRY(注意:由于只是测试代码,没有加入异常处理)*/ BOOL CRasDemoDlg::UpdateRasEntry(LPCTSTR lpszEntry) { /* RasSetEntryProperties */ RASENTRY RasEntry; RasEntry.dwSize = sizeof(RASENTRY);//can not be ommitted! dwSize identifies the version of RASENTRY DWORD dwEntryInfoSize = 0; RasGetEntryProperties(NULL,lpszEntry,NULL,&dwEntryInfoSize,NULL,NULL);//Get the EntryInfoSize RasGetEntryProperties(NULL,lpszEntry,&RasEntry,&dwEntryInfoSize,NULL,NULL);//Get the RasEntry
RasSetEntryProperties(NULL,lpszEntry,&RasEntry,dwEntryInfoSize,NULL,NULL);//Update the RasEntry
/* RasSetEntryDialParams */ RASDIALPARAMS rasdialparams; rasdialparams.dwSize = sizeof(RASDIALPARAMS); BOOL fPassword; strcpy(rasdialparams.szEntryName,lpszEntry);//can not be ommitted! szEntryName identifieds the very entry
RasGetEntryDialParams(NULL,&rasdialparams,&fPassword);//Get username and password
to XmagicX(与code无缘) :我也想改注册表,但找不到ip信息存在哪里啊!
------------------------------------------------------------------------------
设计思想:
通过RasGetEntryProperties()获取Phonebook Entry信息,然后改变其中的某些项的值,再通过RasSetEntryProperties()更新Phonebook Entry。用到的头文件和库文件:
ras.h,raserror.h,Rasapi32.libAPI原型:
DWORD RasGetEntryProperties(
LPCTSTR lpszPhonebook,
LPCTSTR lpszEntry,
LPRASENTRY lpRasEntry,
LPDWORD lpdwEntryInfoSize,
LPBYTE lpbDeviceInfo,
LPDWORD lpdwDeviceInfoSize
);
DWORD RasSetEntryProperties(
LPCTSTR lpszPhonebook,
LPCTSTR lpszEntry,
LPRASENTRY lpRasEntry,
DWORD dwEntryInfoSize,
LPBYTE lpbDeviceInfo,
DWORD dwDeviceInfoSize
);
主要实现代码:
/*获取LOCAL MACHINE上所有拨号连接,显示在一个LISTBOX中*/
void CRasDemoDlg::GetRasEntries()
{
DWORD dwCb = sizeof(RASENTRYNAME);
DWORD dwErr = ERROR_SUCCESS;
DWORD dwRetries = 5;
DWORD dwEntries = 0;
RASENTRYNAME* lpRasEntryName = NULL;
int count = m_listbox.GetCount();
for(int i = 1;i <= count;i++)
{
m_listbox.DeleteString(0);
}
//
// Loop through in case the information from RAS changes between calls.
//
while (dwRetries--)
{
//
// If the memory is allocated, free it.
//
if (NULL != lpRasEntryName)
{
HeapFree(GetProcessHeap(), 0, lpRasEntryName);
lpRasEntryName = NULL;
}
//
// Allocate the size need for the RAS structure.
//
lpRasEntryName = (RASENTRYNAME*)HeapAlloc(GetProcessHeap(), 0, dwCb);
if (NULL == lpRasEntryName)
{
dwErr = ERROR_NOT_ENOUGH_MEMORY;
break;
}
//
// Set the structure size for version checking purposes.
//
lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
//
// Call the RAS API, bail on the loop if we are successful or an unknown
// error occurs.
//
dwErr = RasEnumEntries(
NULL,
NULL,
lpRasEntryName,
&dwCb,
&dwEntries);
if (ERROR_BUFFER_TOO_SMALL != dwErr)
{
break;
}
}
//
// In the success case print the names of the entries.
//
if (ERROR_SUCCESS == dwErr)
{
DWORD i; for (i = 0; i < dwEntries; i++)
{
m_listbox.InsertString(i,lpRasEntryName[i].szEntryName);
}
}
else
{
CString errMsg;
errMsg.Format("RasEnumEntries failed: Error = %d\n",dwErr);
AfxMessageBox(errMsg);
}
//
// Free the memory if necessary.
//
if (NULL != lpRasEntryName)
{
HeapFree(GetProcessHeap(), 0, lpRasEntryName);
lpRasEntryName = NULL;
}
}
/*通过LISTBOX,获取PHONEBOOK ENTRY的名称*/
void CRasDemoDlg::OnBnClickedButton2() //Update the selected Entry's Properties
{
int index = m_listbox.GetCurSel();
if ( -1 != index )
{
int msgBoxResult = MessageBox("Are you sure?","Attention Please",MB_YESNO|MB_ICONQUESTION);
if( msgBoxResult == IDYES )
{
CString EntryName;
m_listbox.GetText(index,EntryName);
if( TRUE == UpdateRasEntry(EntryName) )
{
AfxMessageBox("Updated succeeded!");
}
else
AfxMessageBox("Updated failed!");
}
}
else
{
AfxMessageBox("Please select a phonebook entry first!");
}
}
/*通过从LISTBOX获取的ENTRY NAME,更新指定的ENTRY(注意:由于只是测试代码,没有加入异常处理)*/
BOOL CRasDemoDlg::UpdateRasEntry(LPCTSTR lpszEntry)
{
/* RasSetEntryProperties */
RASENTRY RasEntry;
RasEntry.dwSize = sizeof(RASENTRY);//can not be ommitted! dwSize identifies the version of RASENTRY
DWORD dwEntryInfoSize = 0; RasGetEntryProperties(NULL,lpszEntry,NULL,&dwEntryInfoSize,NULL,NULL);//Get the EntryInfoSize
RasGetEntryProperties(NULL,lpszEntry,&RasEntry,&dwEntryInfoSize,NULL,NULL);//Get the RasEntry
UpdateData(1);
strcpy(RasEntry.szLocalPhoneNumber, m_phonenumber);
RasSetEntryProperties(NULL,lpszEntry,&RasEntry,dwEntryInfoSize,NULL,NULL);//Update the RasEntry
/* RasSetEntryDialParams */
RASDIALPARAMS rasdialparams;
rasdialparams.dwSize = sizeof(RASDIALPARAMS);
BOOL fPassword;
strcpy(rasdialparams.szEntryName,lpszEntry);//can not be ommitted! szEntryName identifieds the very entry
RasGetEntryDialParams(NULL,&rasdialparams,&fPassword);//Get username and password
UpdateData(1);
strcpy(rasdialparams.szUserName,m_username);
strcpy(rasdialparams.szPassword,m_password);
RasSetEntryDialParams(NULL,&rasdialparams,FALSE);
return TRUE;
}