从MSDN找到的,有空在给你试一下吧
改IP地址可以用AddIPAddress
DWORD AddIPAddress(
IPAddr Address // IP address to add
IPMask IpMask //subnet mask for IP address
DWORD IfIndex // index of adapter
PULONG NTEContext // Net Table Entry context
PULONG NTEInstance // Net Table Entry Instance );
改网关:
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW pRow = NULL;
ULONG dwSize = 0;
BOOL bOrder = FALSE;
DWORD dwStatus = 0;
DWORD NewGateway = 0xDDBBCCAA; // this is in host order Ip Address AA.BB.CC.DD is DDCCBBAA
// Find out how big our buffer needs to be
dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
OutputMessage(TEXT("Malloc failed, Out of Memory!\r\n"));
exit(1);
}
// Now get the table
dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}
if (dwStatus != ERROR_SUCCESS) {
OutputMessage(TEXT("GetIpForwardTable Failed, ERROR %d\r\n"), dwStatus);
if (pIpForwardTable)
free(pIpForwardTable);
exit(1);
}
// Search for the row in the table we want. The default gateway has a destination
// of 0.0.0.0 - Notice that we continue looking through the table, but only copy
// one row. This is so that if there happen to be multiple default gateways, we can
// be sure to delete them all.
for (unsigned long int i=0; i < pIpForwardTable->dwNumEntries; i++) {
if (pIpForwardTable->table[i].dwForwardDest == 0) {
// We've found the default gateway
if (!pRow) {
// Allocate some memory to store the row in - this is easier than filling
// in the row structure ourselves and we can be sure we only change the
// gateway address.
pRow = (PMIB_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
OutputMessage(TEXT("Malloc failed, Out of Memory!\r\n"));
exit(1);
}
// Copy the row
memcpy(pRow, &(pIpForwardTable->table[i]), sizeof(MIB_IPFORWARDROW));
}
// Delete the old default gateway entry
dwStatus = DeleteIpForwardEntry(&(pIpForwardTable->table[i]));
if (dwStatus != ERROR_SUCCESS) {
OutputMessage(TEXT("Could not delete old gateway\n"));
exit(1);
}
}
}
// Set the nexthop field to our new gateway - all the other properties of the route will
// be the same as they were previously.
pRow->dwForwardNextHop = NewGateway;
// Create a new route entry for the default gateway.
dwStatus = CreateIpForwardEntry(pRow);
if (dwStatus == NO_ERROR)
OutputMessage(TEXT("Gateway changed successfully\n"));
else if (dwStatus == ERROR_INVALID_PARAMETER)
OutputMessage(TEXT("Invalid Parameter\n"));
else
DisplayErrorMessage(dwStatus);
// Free resources
if (pIpForwardTable)
free(pIpForwardTable);
if (pRow)
free(pRow);