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

rodger_yanglq 2010-11-06 09:18:50
现在有如下命令需要通过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地址不固定,我后台有个地址表,最好可以从表中读取,请问有办法吗?

...全文
205 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
十豆三 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 rodger_yanglq 的回复:]
十豆三,谢谢你的答复,现在我有个疑问,

我使用ShellExecute也可以实现,但遇到一个怪现象。
语句1:ShellExecute(0, "open", "netsh", "interface ip set address 本地连接 static 192.168.0.45 255.255.255.0 192.168.0.1 1", "", 0)

语句2: ShellExecute……
[/Quote]

改在这样:
ox=Createobject("wscript.shell")
ox.Run("netsh interface ip set address 本地连接 static 192.168.0.45 255.255.255.0 192.168.0.1 1",0,1)
ox.Run("netsh interface ip set dn 本地连接 static 192.168.0.1 primary",0,1)
rodger_yanglq 2010-11-15
  • 打赏
  • 举报
回复
十豆三,谢谢你的答复,现在我有个疑问,

我使用ShellExecute也可以实现,但遇到一个怪现象。
语句1:ShellExecute(0, "open", "netsh", "interface ip set address 本地连接 static 192.168.0.45 255.255.255.0 192.168.0.1 1", "", 0)

语句2: ShellExecute(0, "open", "netsh", "interface ip set dn 本地连接 static 192.168.0.1 primary", "", 0);

语句1单独设置ip没问题,语句2单独设置dns没问题,要同时设置ip和dns就有问题了。请问用ShellExecute调用netsh.exe怎么同时设置ip和dns啊?
YMYANGJIE 2010-11-09
  • 打赏
  • 举报
回复
十豆三是真正的大侠!!
jllemon 2010-11-08
  • 打赏
  • 举报
回复
学习了,十豆3三好厉害啊。
我是小数位 2010-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 apple_8180 的回复:]
方法三

新建一脚本:0.sh
int ip
set address name="本地连接" source=static addr=192.168.0.199 mask=255.255.255.0


在命令窗口中输入:
! /n7 NETSH EXEC 0.SH
[/Quote]學習了!
十豆三 2010-11-06
  • 打赏
  • 举报
回复
方法三

新建一脚本:0.sh
int ip
set address name="本地连接" source=static addr=192.168.0.199 mask=255.255.255.0


在命令窗口中输入:
! /n7 NETSH EXEC 0.SH
十豆三 2010-11-06
  • 打赏
  • 举报
回复
方法二
将以下代码存为.BAT文件,如 ModifyIP.BAT,此批处理可修改IP地址或网关、子网掩码、首选DNS、备用DNS


@echo off
rem eth //eth 为网卡名称,可在网络连接中查询,如"本地链接"
set eth="本地链接"
rem ip //ip 为你想更改的IP
set ip=192.168.0.180
rem gateway //gateway 为网关地址
set gateway=192.168.0.1
rem netmasks //netmasks 为子网掩码
set netmasks=255.255.255.0
rem dns //dns 为首选DNS
set dns=192.168.0.1
rem dns2 //dns2 为备用DNS
set dns2=192.168.0.2
echo 正在将本机IP更改到: %ip% 请等候...
rem
if %gateway%==none netsh interface ip set address %eth% static %ip% %netmasks% %gateway% > nul
if not %gateway%==none netsh interface ip set address %eth% static %ip% %netmasks% %gateway% 1 > nul
if %dns%==none netsh interface ip set dns %eth% static %dns%> nul
if not %dns%==none netsh interface ip set dns %eth% static %dns%> nul
if %dns2%==none netsh interface ip add dns %eth% %dns2%> nul
if not %dns2%==none netsh interface ip add dns %eth% %dns2% 2> nul
echo.........................
echo 检查当前本机IP:
ipconfig
echo.........................
echo 成功将本机IP更改为%ip%!
//pause
exit



然后在VFP中调用这个批处理:

RUN /N7 C:\ModifyIP.BAT
十豆三 2010-11-06
  • 打赏
  • 举报
回复
方法一
通过代码修改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. ) > 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
hil2000 2010-11-06
  • 打赏
  • 举报
回复
学习了

2,723

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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