当前位置:编程学习 > VB >>

如何用VB执行cmd

我想做一个这样的程序。
运行程序
用cmd 里的net send 命令语句  net send ip “要说的话”
用户输入IP
然后输入要说的话。
结束。
--------------------编程问答-------------------- 你先 生成这个命令字符串,然后用shell 函数。 --------------------编程问答-------------------- shell "cmd /c net send " & IP & " """ & sText & """"

IP与sText都是字符串变量. --------------------编程问答-------------------- shell
shellexecute
createprocess
winexec
...
好多办法。
另外netsend貌似有对应的API,可以直接调用。
具体搜索下。 --------------------编程问答-------------------- 试一试:
shell "cmd /c net send " & " " & IP & " " &  "要说的话"  --------------------编程问答-------------------- 用shell --------------------编程问答-------------------- vbs脚本似乎更有交互性,但是我不知道用
--------------------编程问答-------------------- 貌似都不行啊。我是想用text1.text来接受用户输入的ip
                   text2.text来接受用户输入的话。但是text2必须是“text2.text” 有个双引号 --------------------编程问答--------------------
引用 7 楼 xiaogaicai1 的回复:
貌似都不行啊。我是想用text1.text来接受用户输入的ip
                    text2.text来接受用户输入的话。但是text2必须是“text2.text” 有个双引号

什么意思???
--------------------编程问答--------------------
Shell "cmd /c net send " & Text1.Text & " '" & Text2.Text & "'"
--------------------编程问答-------------------- Shell "cmd /c net send " & Text1.Text & " "" & Text2.Text & """ --------------------编程问答-------------------- 我无语!!!!!! --------------------编程问答--------------------
Shell "cmd /c net send " & Text1.Text & " “" & Text2.Text & "”"
--------------------编程问答--------------------
引用 3 楼 caozhy 的回复:
shell
shellexecute
createprocess
winexec
...
好多办法。
另外netsend貌似有对应的API,可以直接调用。
具体搜索下。

UP --------------------编程问答-------------------- 楼主这个方法基本行不通,因为这个命令需要windows开启MESSAGE服务,而这个服务自XP开始就是默认禁止的. 而win2000中是默认开启的 --------------------编程问答-------------------- shell啊 --------------------编程问答-------------------- up
--------------------编程问答-------------------- 用shell("net start Messenger")启动MESSAGE服务
用shell("sc ...")可以配置有关服务的设置 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 17 楼 zhao4zhong1 的回复:
用shell("net start Messenger")启动MESSAGE服务
用shell("sc ...")可以配置有关服务的设置


设置为"禁用"的服务也能这样启动么? 我倒是真的没试过. zhao4zhong1测试的是设为"手动"的还是"禁用"的? --------------------编程问答-------------------- echo 要说的话 Y| 这个是CMD的自动回复 --------------------编程问答-------------------- shell ("cmd /c net send 127.0.0.1 hello") --------------------编程问答--------------------

Private Sub Command1_Click()
    ip = InputBox("输入IP")
    Text = InputBox("输入消息内容")
    Shell ("cmd /c net send " & ip & Text)
End Sub
--------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- ding  --------------------编程问答-------------------- shell 函数实现调用执行外部程序 --------------------编程问答-------------------- 路过 --------------------编程问答-------------------- --------------------编程问答-------------------- 在VB中将文本中的命令存为bat,再shell运行bat,最后撤除临时文件bat --------------------编程问答-------------------- 纯顶!! --------------------编程问答-------------------- 将命令写成批处理,用VB执行就好了 --------------------编程问答--------------------
引用 2 楼 myjian 的回复:
shell "cmd /c net send " & IP & " """ & sText & """"

IP与sText都是字符串变量.


同上 --------------------编程问答-------------------- ding --------------------编程问答-------------------- 有方法不显示cmd窗口吗? --------------------编程问答--------------------
引用 19 楼 wallescai 的回复:
引用 17 楼 zhao4zhong1 的回复:
用shell("net start Messenger")启动MESSAGE服务
用shell("sc ...")可以配置有关服务的设置


设置为"禁用"的服务也能这样启动么? 我倒是真的没试过. zhao4zhong1测试的是设为"手动"的还是"禁用"的?

不仅可以设置手动/自动/禁用,还可以删除和安装呢。请参考以下sc.exe的帮助:
DESCRIPTION:
        SC is a command line program used for communicating with the
        NT Service Controller and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

        The option <server> has the form "\\ServerName"
        Further help on commands can be obtained by typing: "sc [command]"
        Commands:
          query-----------Queries the status for a service, or
                          enumerates the status for types of services.
          queryex---------Queries the extended status for a service, or
                          enumerates the status for types of services.
          start-----------Starts a service.
          pause-----------Sends a PAUSE control request to a service.
          interrogate-----Sends an INTERROGATE control request to a service.
          continue--------Sends a CONTINUE control request to a service.
          stop------------Sends a STOP request to a service.
          config----------Changes the configuration of a service (persistant).
          description-----Changes the description of a service.
          failure---------Changes the actions taken by a service upon failure.
          qc--------------Queries the configuration information for a service.
          qdescription----Queries the description for a service.
          qfailure--------Queries the actions taken by a service upon failure.
          delete----------Deletes a service (from the registry).
          create----------Creates a service. (adds it to the registry).
          control---------Sends a control to a service.
          sdshow----------Displays a service's security descriptor.
          sdset-----------Sets a service's security descriptor.
          GetDisplayName--Gets the DisplayName for a service.
          GetKeyName------Gets the ServiceKeyName for a service.
          EnumDepend------Enumerates Service Dependencies.

        The following commands don't require a service name:
        sc <server> <command> <option>
          boot------------(ok | bad) Indicates whether the last boot should
                          be saved as the last-known-good boot configuration
          Lock------------Locks the Service Database
          QueryLock-------Queries the LockStatus for the SCManager Database
EXAMPLE:
        sc start MyService
Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]: y
QUERY and QUERYEX OPTIONS :
        If the query command is followed by a service name, the status
        for that service is returned.  Further options do not apply in
        this case.  If the query command is followed by nothing or one of
        the options listed below, the services are enumerated.
    type=    Type of services to enumerate (driver, service, all)
             (default = service)
    state=   State of services to enumerate (inactive, all)
             (default = active)
    bufsize= The size (in bytes) of the enumeration buffer
             (default = 1024)
    ri=      The resume index number at which to begin the enumeration
             (default = 0)
    group=   Service group to enumerate
             (default = all groups)
SYNTAX EXAMPLES
sc query                - Enumerates status for active services & drivers
sc query messenger      - Displays status for the messenger service
sc queryex messenger    - Displays extended status for the messenger service
sc query type= driver   - Enumerates only active drivers
sc query type= service  - Enumerates only Win32 services
sc query state= all     - Enumerates all services & drivers
sc query bufsize= 50    - Enumerates with a 50 byte buffer.
sc query ri= 14         - Enumerates with resume index = 14
sc queryex group= ""    - Enumerates active services not in a group
sc query type= service type= interact - Enumerates all interactive services
sc query type= driver group= NDIS     - Enumerates all NDIS drivers --------------------编程问答--------------------
引用 33 楼 a123062220a 的回复:
有方法不显示cmd窗口吗?

不显示cmd窗口我有两种方法:
1.Shell "执行程序名",vbHide
2.NirCmd.exe exec hide 执行程序名
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,