Red de conocimiento informático - Material del sitio web - ¿Cómo utilizar vb6.0 para escribir un programa para modificar la dirección IP local?

¿Cómo utilizar vb6.0 para escribir un programa para modificar la dirección IP local?

BOOL GetAdapterInfo()

{

// El código aquí es adecuado para WINDOWS2000. Para NT, debe leer 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)

devuelve FALSO;

DWORD dwIndex = 0;

DWORD dwBufSize = 256

DWORD dwDataType

char szSubKey[; 256] ;

carácter sin firmar szData[256];

while(RegEnumKeyEx(hKey, dwIndex, szSubKey y dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)

p>

{

//AfxMessageBox(szSubKey);

if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ y 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) // Determinar si es una tarjeta Ethernet

{

dwBufSize = 256;

p>

if(RegQueryValueEx(hSubKey, "DriverDesc", 0, & dwDataType, szData, & dwBufSize) == ERROR_SUCCESS)

{

ADAPTER_INFO *pAI = new ADAPTER_INFO

;

p>

pAI-gt; strDriverDesc = (LPCTSTR)szData;

//AfxMessageBox((LPCTSTR)szData);

dwBufSize = 256; >

if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, & dwDataType, szData, & dwBufSize) == ERROR_SUCCESS)

{

pAI-gt; LPCTSTR )szData;

//AfxMessageBox((LPCTSTR)szData);

RegGetIP(pAI, (LPCTSTR)szData

//AfxMessageBox(( LPCTSTR) )szData);

}

AdapterInfoVector.push_back(pAI); // Agregar al contenedor

}

}

p>

}

RegCloseKey(hNdiIntKey);

}

RegCloseKey(hSubKey);

}

dwBufSize = 256;

} /* fin del tiempo */

RegCloseKey(hKey);

/*

// Puede utilizar GetAdaptersInfo para obtener información de la tarjeta de red, pero el nombre mostrado no es muy específico

ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO

IP_ADAPTER_INFO *pAdapterInfoBkp, * pAdapterInfo = (IP_ADAPTER_INFO* )new char[ulAdapterInfoSize];

if( GetAdaptersInfo(pAdapterInfo, amp; ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // El búfer no es lo suficientemente grande

{

eliminar pAdapterInfo

pAdapterInfo;

= (IP_ADAPTER_INFO*)nuevo carácter[ulAdapterInfoSize]

pAdapterInfoBkp = pAdapterInfo;

}

if( GetAdaptersInfo(pAdapterInfo, amp; ulAdapterInfoSize) == ERROR_SUCCESS )

{

hacer {

if (pAdapterInfo-gt; Tipo == MIB_IF_TYPE_ETHERNET)

{

ADAPTER_INFO *pAI = nuevo ADAPTER_INFO;

pAI-gt; strDriverDesc = pAdapterInfo-gt; Descripción

pAI-gt;

RegGetIP(pAI, (LPCTSTR)pAdapterInfo-gt; AdapterName); // Debido a que IP_ADAPTER_INFO no contiene información de máscara, simplemente leemos el registro directamente

AdapterInfoVector.push_back(pAI

}

pAdapterInfo = pAdapterInfo-gt; Siguiente

} while(pAdapterInfo

}

eliminar); pAdapterInfoBkp;

*/

devuelve VERDADERO

}

//------------ -------------------------------------------------- ---

// Obtener la información de IP en el registro

// nIndex aún no se ha procesado

//-------- ------------------------------------------------- - ------

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

{

ASSERT( pAI );

HKEY hKey;

string strKeyName = "SISTEMA\\CurrentControlSet\\Services\\Tcpip\\Parameters\\"; > 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-gt;

dwBufSize = 256

if(RegQueryValueEx(hKey, "SubnetMask", 0, & dwDataType, szData, & dwBufSize) == ERROR_SUCCESS)

pAI -gt; strNetMask = (LPCTSTR)szData;

dwBufSize = 256;

if(RegQueryValueEx(hKey, "DefaultGateway", 0, y dwDataType, szData, y dwBufSize) == ERROR_SUCCESS)

pAI-gt; strNetGate = (LPCTSTR)szData;

RegCloseKey(hKey);

devuelve TRUE; > }

//---------------------------------- ----- --------------------------

// Establecer información de IP en el registro

// -------------------------------------------------- -------- ---------------------

BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask , LPCTSTR pNetGate)

{

HKEY hKey

string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Param

eters\\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); (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, (carácter sin firmar*)mszNetMask, nMask);

RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (carácter sin firmar*)mszNetGate, nGate); p>

RegCloseKey(hKey);

devuelve VERDADERO

}

//------------ - ---------------------------------------

------------

//Notificar cambios de dirección 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,

p>

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)

bResultado = VERDADERO

FreeLibrary(hDhcpDll

return bResultado

}

// ---- ---------------------------------------------- ---- -----------

// Establecer dirección IP

// Si solo hay una IP vinculada, nIndex = 0, una tarjeta de red El enlace aún no se ha procesado. Varias direcciones

//--------------------------------- -------- --------------------------

BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)

{

if(!RegSetIP(lpszAdapterName, nIndex, pIP

Dirección, pNetMask, pNetGate))

devuelve FALSO;

if(!NotifyIPChange(lpszAdapterName, nIndex, pIPAddress, pNetMask))

devuelve FALSO

p>

devuelve VERDADERO

}