您的位置:程序门 -> vb -> 网络编程



如何通过vb编程,读取/修改本机ip地址和子网掩码及网关地址?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何通过vb编程,读取/修改本机ip地址和子网掩码及网关地址?[已结贴,结贴人:tjs1688]
发表于:2007-03-26 17:05:38 楼主
内容同上?高分赠送
发表于:2007-03-26 17:06:361楼 得分:0
读取本机ip地址已经有了,如何通过vb编程读取/修改本机子网掩码及网关地址?
发表于:2007-03-27 08:33:472楼 得分:20
strcomputer   =   ". "
set   objwmiservice   =   getobject( "winmgmts:\\ "   &   strcomputer   &   "\root\cimv2 ")
set   colnetadapters   =   objwmiservice.EXECquery   _
        ( "select   *   from   win32_networkadapterconfiguration   where   ipenabled=true ")


for   each   objnetadapter   in   colnetadapters
sip=objnetadapter.ipaddress
stripaddress   =   sip             '保持原来的ip,如果需要修改ip为192.168.0.200:stripaddress   =   array( "192.168.0.200 ")
strsubnetmask   =   array( "255.255.255.0 ")
strgateway   =   array( "192.168.0.254 ")           '修改网关为192.168.0.254
strgatewaymetric   =   array(1)

        errenable   =   objnetadapter.enablestatic(stripaddress,   strsubnetmask)
        errgateways   =   objnetadapter.setgateways(strgateway,   strgatewaymetric)
        if   errenable   =   0   then
              msgbox   "the   ip   address   has   been   changed. "
        else
                msgbox     "the   ip   address   could   not   be   changed. "
        end   if
        exit   for             '只修改第一个网卡的设置
next
发表于:2007-03-27 09:00:453楼 得分:10
http://www.vbgood.com/viewthread.php?tid=25879


http://topic.csdn.net/t/20020915/16/1025383.html
发表于:2007-03-27 10:42:104楼 得分:0
刚刚测试了一下,提示无法修改网关
发表于:2007-03-27 14:31:195楼 得分:0
那位高手能够   读取子网掩码及网关地址   ?
发表于:2007-03-28 22:15:546楼 得分:0
獲取網卡狀態信息

declare   function   getnetworkparams   lib   "iphlpapi.dll "   (fixedinfo   as   any,   poutbuflen   as   long)   as   long
declare   function   getiftable   lib   "iphlpapi.dll "   (byref   piftable   as   mib_iftable,   byref   pdwsize   as   long,   _
                                                                                                byval   border   as   long)   as   long
declare   function   getifentry   lib   "iphlpapi.dll "   (pifrow   as   mib_ifrow)   as   long
       
type   mib_ifrow                 '保存結果信息
          wszname(0   to   511)   as   byte   '接口名稱的unicode字符串,必須為512字節
          dwindex                       as   long   '接口編號
          dwtype                         as   long   '接口類型,參看ip_adapter_info類型的type成員
          dwmtu                           as   long   '最大傳輸單元
          dwspeed                       as   long   '接口速度(字節)
          dwphysaddrlen           as   long   '由bphysaddr獲得的物理地址有效長度
          bphysaddr(0   to   7)   as   byte   '物理地址
          dwadminstatus           as   long   '接口管理狀態
          dwoperstatus             as   long   '操作狀態,以下值之一:
          dwlastchange             as   long   '操作狀態最后改變的時間
          dwinoctets                 as   long   '總共收到(字節)
          dwinucastpkts           as   long   '總共收到(unicast包)
          dwinnucastpkts         as   long   '總共收到(non-unicast包),包括廣播包和多點傳送包
          dwindiscards             as   long   '收到后丟棄包總數(即使沒有錯誤)
          dwinerrors                 as   long   '收到出錯包總數
          dwinunknownprotos   as   long   '收到后因協議不明而丟棄的包總數
          dwoutoctets               as   long   '總共發送(字節)
          dwoutucastpkts         as   long   '總共發送(unicast包)
          dwoutnucastpkts       as   long   '總共發送(non-unicast包),包括廣播包和多點傳送包
          dwoutdiscards           as   long   '發送丟棄包總數(即使沒有錯誤)
          dwouterrors               as   long   '發送出錯包總數
          dwoutqlen                   as   long   '發送隊列長度
          dwdescrlen                 as   long   'bdescr部分有效長度
          bdescr(0   to   255)     as   byte   '接口描述
end   type
 
type   mib_iftable             '包含結果表
          dwnumentries   as   long                 '當前网絡接口的總數
          mib_table(9)   as   mib_ifrow       '指向一個包含mib_ifrow類型的指針
end   type

sub   test()
        dim   net   as   mib_iftable
        lenift   =   len(net)
        rvalue   =   getiftable(net,   lenift,   true)
        '由net.dwnumentries獲得接口數量,然后用for…next循環獲取每個接口信息:
        for   i   =   1   to   net.dwnumentries   -   1
              with   net.mib_table(i)
                        debug.print
                        debug.print   "---- "   &   time   &   "---- "
                        debug.print   "接口編號: "   &   .dwindex
                        debug.print   "接口速度: "   &   .dwspeed
                        debug.print   "接口狀態: "   &   .dwadminstatus
                        debug.print   "操作狀態: "   &   .dwoperstatus
                        debug.print   "收到字節: "   &   .dwinoctets
                        for   xi   =   1   to   .dwphysaddrlen   -   1
                                txx   =   txx   &   format(hex(.bphysaddr(xi)),   "00 ")
                        next
                        if   txx   <>   " "   then   debug.print   "网卡地址: ";   txx
              end   with
        next
end   sub
发表于:2007-03-28 22:16:177楼 得分:20
'dwoperstatus   as   long操作狀態,以下值之一:
'常量名稱                                                               說明
'-----------------------------------------------------------------------------
mib_if_oper_status_non_operational   =   0   '网絡适配器被禁止,例如:地址沖突
mib_if_oper_status_unreachable   =   1           '沒有連接
mib_if_oper_status_disconnected   =   2         '局域网:電纜未連接;廣域网:無載波信號
mib_if_oper_status_connecting   =   3             '廣域网适配器連接中
mib_if_oper_status_connected   =   4               '廣域网适配器連接上遠程對等點
mib_if_oper_status_operational   =   5           '局域网适配器默認狀態
发表于:2007-04-11 09:21:168楼 得分:0
up
发表于:2007-04-11 11:03:459楼 得分:0
该回复于2007-12-28 17:45:53被管理员或版主删除


快速检索

最新资讯
热门点击