专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > VFP

一个VFP调用外部命令的有关问题(超烦的有关问题)

发布时间:2010-05-24 22:03:56 文章来源:www.iduyao.cn 采编人员:星星草
一个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

*--------------------

SQL code
****调用方式****

*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
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: