2,723
社区成员
发帖
与我相关
我的任务
分享
****调用方式****
*SetLocalIP( tcCard,tcAddr, tcMask, tcGateWay,tcDNS,tcAddDNS )
*例如:
SetLocalIP( '本地连接','192.168.0.180','255.255.255.0','192.168.0.1','192.168.0.1','192.168.0.3' )
*!* *---------------------------------------------
FUNCTION SetLocalIP( tcCard, tcAddr, tcMask, tcGateWay, tcDNS, tcAddDNS )
tcCard = IIF(TYPE([tcCard])=[C], tcCard, [本地连接]) && 连接名称
tcAddr = IIF(TYPE([tcAddr])=[C], tcAddr, []) && IP地址, 空表示动态获取
tcMask = IIF(TYPE([tcMask])=[C], tcMask, [255.255.255.0]) && 掩码
tcGateWay = IIF(TYPE([tcGateWay])=[C], tcGateWay, [none]) && 网关
tcDNS = IIF(TYPE([tcDNS])=[C], tcDNS, NULL) && 主DNS
tcAddDNS = IIF(TYPE([tcAddDNS])=[C], tcAddDNS, NULL) && 备用DNS
LOCAL lcCmdStr, lcTempFile, lnReturn, lcCR
lcCR = CHR(13)+CHR(10)
lcTempFile = ADDBS(SYS(2023))+[SetIP.tmp]
= STRTOFILE( [interface ip] + lcCR, lcTempFile )
IF EMPTY(tcAddr)
* 指定是通过动态主机配置协议 (DHCP) 服务器配置 IP 地址
= STRTOFILE( [set address name="]+tcCard+[" source=dhcp] + lcCR, lcTempFile, 1 ) &&如果是VFP6参数1改为.T.
ELSE
* 使用静态 IP 地址
= STRTOFILE( [set address name="]+tcCard+[" source=static] + [ addr=]+tcAddr+[ mask=]+tcMask+[ GateWay=]+tcGateWay+[ gwmetric=1] + lcCR, lcTempFile, 1 ) &&如果是VFP6参数1改为.T.
ENDIF
IF !ISNULL(tcDNS)
* 设置 DNS
IF EMPTY(tcDNS)
= STRTOFILE( [set dns name="]+tcCard+[" source=dhcp] + lcCR, lcTempFile, 1 ) &&如果是VFP6参数1改为.T.
ELSE
= STRTOFILE( [set dns name="]+tcCard+[" source=static addr=]+tcDNS + lcCR, lcTempFile, 1 ) &&如果是VFP6参数1改为.T.
ENDIF
ENDIF
IF !ISNULL(tcAddDNS) AND !EMPTY(tcAddDNS)
* 备用 DNS
= STRTOFILE( [add dns name="]+tcCard+[" addr=]+tcAddDNS+[ index=2] + lcCR, lcTempFile, 1 ) &&如果是VFP6参数1改为.T.
ENDIF
lcCmdStr = [netsh exec ] + lcTempFile
RETURN ShellAndWait( lcCmdStr, .T., .F. ) > 0
ENDFUNC
FUNCTION ShellAndWait( tcDosCommand, IsDos, IsShow )
IsDos = IIF(PARAMETERS()>=2 AND TYPE([IsDos])=[L], IsDos, .F.)
IsShow = IIF(PARAMETERS()>=3 AND TYPE([IsShow])=[L], IsShow, !IsDos)
LOCAL lcCmdStr
IF IsDos
IF EMPTY(GETENV([OS]))
lcCmdStr = [Command.com /C ] + tcDosCommand + CHR(0)
ELSE
lcCmdStr = [Cmd.exe /C ] + tcDosCommand + CHR(0)
ENDIF
ELSE
lcCmdStr = tcDosCommand + CHR(0)
ENDIF
#DEFINE NORMAL_PRIORITY_CLASS 32
#DEFINE IDLE_PRIORITY_CLASS 64
#DEFINE HIGH_PRIORITY_CLASS 128
#DEFINE INFINITE -1
#DEFINE REALTIME_PRIORITY_CLASS 1600
DECLARE INTEGER CloseHandle IN kernel32 LONG hObject
DECLARE INTEGER WaitForSingleObject IN kernel32 LONG hHandle, LONG dwMilliseconds
DECLARE INTEGER CreateProcessA IN kernel32 ;
LONG lpApplicationName, ;
STRING lpCommandLine, ;
LONG lpProcessAttributes, ;
LONG lpThreadAttributes, ;
LONG bInheritHandles, ;
LONG dwCreationFlags, ;
LONG lpEnvironment, ;
LONG lpCurrentDirectory, ;
STRING @lpStartupInfo, ;
STRING @lpProcessInformation
LOCAL lcStartupInfo, lcProcessInformation, RetCode, hProcess
lcStartupInfo = Long2Str(68) + REPLICATE(CHR(0), 40) + CHR(IIF(IsShow,0,1)) + REPLICATE(CHR(0), 23)
lcProcessInformation = REPLICATE(CHR(0), 16)
RetCode = CreateProcessA(0, lcCmdStr, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, @lcStartupInfo, @lcProcessInformation )
hProcess = Str2Long(SUBSTR(lcProcessInformation, 1, 4))
RetCode = WaitForSingleObject(hProcess, INFINITE)
RetCode = CloseHandle(hProcess)
RETURN RetCode
ENDFUNC
*-----------------------------------------------
FUNCTION Long2Str( lnLongVal )
PRIVATE i2, retustr
retustr = []
FOR i2 = 24 TO 0 STEP -8
retustr = CHR(INT(lnLongVal/(2^i2))) + retustr
lnLongVal = MOD(lnLongVal, (2^i2))
NEXT
RETURN retustr
ENDFUNC
FUNCTION Str2Long( tcLongStr )
PRIVATE i2, RetuVal
RetuVal = 0
FOR i2 = 0 TO 24 STEP 8
RetuVal = RetuVal + (ASC(tcLongStr) * (2^i2))
tcLongStr = RIGHT(tcLongStr, LEN(tcLongStr) - 1)
NEXT
RETURN RetuVal
ENDFUNC