如何获得并修改本机的IP,网关与DNS??一百九十分!!!!!

alwssoan 2001-07-11 02:31:59
如何获得并修改本机的IP,网关与DNS,重要的是如何修改,给出方法,最好是源代码!修改,修改!!好的答案可另外开档加分,我的最高只能给190
...全文
599 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
linzhai 2001-07-12
  • 打赏
  • 举报
回复
老兄: tcp/ip dns 網關都保存在注冊表,不管你用什么方法,改的都是注冊, 不過你可以用程式來修改注冊嗎, 98 nt 2000 加入一個判斷就行了,只是多幾行代碼而已.
kks 2001-07-11
  • 打赏
  • 举报
回复
从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);

月光 2001-07-11
  • 打赏
  • 举报
回复
只有改注册表,改完好像要发个更新注册表的api.
Kingron 2001-07-11
  • 打赏
  • 举报
回复
>>注册表的不要,只要程序修改的,

用程序修改注册表不就行了?不要告诉我连用程序修改注册表都不准?其实都是控制注册表来进行系统配置的,不过修改之后,可能要重新启动哦。
alwssoan 2001-07-11
  • 打赏
  • 举报
回复
取得的不要,只要修改的。注册表的不要,只要程序修改的,因为注册表不同平台下是有所不同的
取得的不要,只要修改的。注册表的不要,只要程序修改的,因为注册表不同平台下是有所不同的
linzhai 2001-07-11
  • 打赏
  • 举报
回复
非常簡單,
在注冊表中有 ip 地址, 網關, dns , 你想怎麼改都可以,不要告訴我你不會修改注冊表.

要我告訴你也可以,不過...
wljcr 2001-07-11
  • 打赏
  • 举报
回复
gz
hof 2001-07-11
  • 打赏
  • 举报
回复
用api gethostname得到机器名,在用机器名调用gethostbyname得到本机的ip
设置应该有gethostname和sethostbyname
具体你自己找一下吧, 挺忙的, 呵
machin 2001-07-11
  • 打赏
  • 举报
回复
PowerSock1 有个LocalIp的属性.

edit1.text := PowerSock1.LocalIp;

其他的我没试过....
路人丁 2001-07-11
  • 打赏
  • 举报
回复
Powersock控件可以获得本机ip
if (Powersock1->LocalIP == "0.0.0.0")
ShowMessage("not connected");
else
ShowMessage(Powersock1->LocalIP);

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧