一个VFP调用外部命令的问题(超烦的问题)
现在有如下命令需要通过VFP调用:
netsh interface ip set address "本地连接" source= static addr=192.168.48.78 mask=255.255.255.0 gateway=192.168.48.56 gwmetric=1
netsh interface ip set dns "本地连接" static 192.168.48.85 primary
2句命令,是通过命令设置IP地址和DNS
由于IP地址是不固定的,我首先考虑通过run的方式实现,但run(".....")失败,原来上面命令中【本地连接】二侧必须用双引号,不能使用单引号。
那我有想到做个脚本,然后VFP调用,但问题是我用run("c:\ip.bat")也失败,请问题原因。
由于我的IP地址不固定,我后台有个地址表,最好可以从表中读取,请问有办法吗?
[解决办法]
方法一
通过代码修改IP地址或网关、子网掩码、首选DNS、备用DNS
*--------------------
****调用方式*****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. ) > 0ENDFUNCFUNCTION 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 RetCodeENDFUNC*-----------------------------------------------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 retustrENDFUNCFUNCTION 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 RetuValENDFUNC