導航:首頁 > 軟體知識 > 二道門遠傳程序如何修改ip地址

二道門遠傳程序如何修改ip地址

發布時間:2024-04-15 22:06:16

❶ 如何使用vb6.0編寫一個程序,用來修改本機ip地址

BOOL GetAdapterInfo()
{
// 這里的代碼適合WINDOWS2000,對於NT需要讀取HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
HKEY hKey, hSubKey, hNdiIntKey;

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;

DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];

while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
//AfxMessageBox(szSubKey);
if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判斷是不是乙太網卡
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
//AfxMessageBox((LPCTSTR)szData);
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
pAI->strName = (LPCTSTR)szData;
//AfxMessageBox((LPCTSTR)szData);
RegGetIP(pAI, (LPCTSTR)szData);
//AfxMessageBox((LPCTSTR)szData);
}
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hNdiIntKey);
}
RegCloseKey(hSubKey);
}

dwBufSize = 256;
} /* end of while */

RegCloseKey(hKey);

/*
// 可以使用GetAdaptersInfo來取得網卡信息,但其顯示的名稱不是很具體

ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);
IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 緩沖區不夠大
{
delete pAdapterInfo;
pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
pAdapterInfoBkp = pAdapterInfo;
}
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
{
do {
if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = pAdapterInfo->Description;
pAI->strName = pAdapterInfo->AdapterName;
RegGetIP(pAI, (LPCTSTR)pAdapterInfo->AdapterName); // 因為IP_ADAPTER_INFO中未包含掩碼信息,所以乾脆直接讀注冊表
AdapterInfoVector.push_back(pAI);
}
pAdapterInfo = pAdapterInfo->Next;
} while(pAdapterInfo);
}
delete pAdapterInfoBkp;
*/
return TRUE;
}

//-----------------------------------------------------------------
// 得到注冊表中的IP信息
// nIndex暫時未處理
//-----------------------------------------------------------------

BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);

HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;

unsigned char szData[256];
DWORD dwDataType, dwBufSize;

dwBufSize = 256;
if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;

dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;

dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;

RegCloseKey(hKey);
return TRUE;
}

//-----------------------------------------------------------------
// 設置注冊表中的IP信息
//-----------------------------------------------------------------

BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;

char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];

strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);

int nIP, nMask, nGate;

nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);

*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;

*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;

*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;

RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);

RegCloseKey(hKey);

return TRUE;
}

//-----------------------------------------------------------------
// 通知IP地址的改變
//-----------------------------------------------------------------

BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];

MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);

if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL)
return FALSE;

if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)
bResult = TRUE;

FreeLibrary(hDhcpDll);
return bResult;
}

//-----------------------------------------------------------------
// 設置IP地址
// 如果只綁定一個IP,nIndex = 0,暫時未處理一個網卡綁定多個地址
//-----------------------------------------------------------------

BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
if(!RegSetIP(lpszAdapterName, nIndex, pIPAddress, pNetMask, pNetGate))
return FALSE;

if(!NotifyIPChange(lpszAdapterName, nIndex, pIPAddress, pNetMask))
return FALSE;

return TRUE;
}

閱讀全文

與二道門遠傳程序如何修改ip地址相關的資料

熱點內容
雅培降脂產品叫什麼 瀏覽:806
兩個數據差異不顯著怎麼辦 瀏覽:373
iot行業主要有哪些產品 瀏覽:172
微信的信息怎麼一條一條回復 瀏覽:872
美團發信息怎麼沒有聲音 瀏覽:295
招商證券交易費率哪裡查到 瀏覽:732
大荔西北職業技術學校多少學生 瀏覽:165
怎麼掙錢代理 瀏覽:359
理工男技術男什麼意思 瀏覽:668
數據線如何變成指示燈 瀏覽:406
品牌產品如何合作 瀏覽:292
500克珍珠粉市場售價多少錢 瀏覽:795
梁溪區公證信息咨詢服務哪個好 瀏覽:198
制葯行業的技術有哪些 瀏覽:273
黨建信息宣傳稿一般包括哪些 瀏覽:229
大學生當兵學什麼技術 瀏覽:789
天刀原木怎麼交易 瀏覽:920
退社保需什麼程序 瀏覽:107
如何投資交易網站 瀏覽:572
陳村蘭花市場有什麼買 瀏覽:856