| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1044 人关注过本帖
标题:求助:急!希望高手进来帮下小弟的忙.
只看楼主 加入收藏
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:11 
求助:急!希望高手进来帮下小弟的忙.
本人菜鸟一个,最近要使用VB编写一个界面实现调用几个无源程序的.EXE文件的功能。已经用Shell函数调用.EXE成功,但是调用后需要输入一些参数.EXE才能继续执行。我在窗体上设置几个Text,想将其中的内容传递给.EXE程序以便其能继续执行。此处一窍不通,想请教高手如何实现这一功能。另:如何判断一个.EXE是否已经执行完成呢?恳求大家赐教。。。
搜索更多相关主题的帖子: 一窍不通 源程序 
2010-08-29 09:58
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
收藏
得分:0 
  神啊 赐我一个大侠来指教一下吧!!!!
2010-08-29 10:32
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:5 
1、把参数写在exe 的后面就可以吧。如:Shell "shutdown.exe /s /f /t 18000"。只不过"/s"、"/f"等参数是由Text1.Text输入的而已.

2、如何判断一个.EXE是否已经执行完成。 --> waitForSingleObject

    Const SYNCHRONIZE = &H100000
    Const INFINITE = &HFFFF

    Dim lPid As Long
    Dim lHnd As Long
    Dim lRet As Long

    lPid = Shell(你的程序路径, vbNormalFocus)

    If lPid <> 0 Then

        'Get a handle to the shelled process.
        lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
      
        'If successful, wait for the application to end and close the handle.
        If lHnd <> 0 Then
            lRet = WaitForSingleObject(lHnd, INFINITE)
            CloseHandle lHnd
        End If

        MsgBox "Just terminated.", vbInformation, "Shelled Application"
    End If
2010-08-29 11:57
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
收藏
得分:0 
回复 3楼 jiashie
jiashie 兄能不能详细点?第一步关于参数传递的问题,比如我想调用fortran.exe,但是调用成功后过程是这样的:
1.EXE读入一个参数,然后回车;
2再度入参数,再回车。。。。
然后程序计算,这里的我的问题就是如何将在窗体中TEXT1中输入的参数传递给.EXE,免去人工输入的步骤.具体问题就是这样。
本人是菜鸟接触VB的时间不长,又要急着实现这些功能,能否详细一点怎么能实现这个想法,若能附上代码更是不胜感激

[ 本帖最后由 zpc860111 于 2010-9-1 10:20 编辑 ]
2010-09-01 10:19
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:0 
你说的这个“参数”原来是在exe运行时由用户输入的,而不是启动参数哦。
那就麻烦了,大约是CreatePipe 、CreateProcess进行输入输出重定向,然后ReadFile/writeFile。
(C#中倒是很简单,Process类的 StartInfo.RedirectStandardInput 和 StartInfo.RedirectStandardOutput ,然后就可以直接读取/写入cmd窗口的命令)

[ 本帖最后由 jiashie 于 2010-9-1 12:48 编辑 ]
2010-09-01 12:47
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
收藏
得分:0 
回复 5楼 jiashie
能详细点吗?我看到网上很多人都说要用到“管道技术”?
这个管道技术我找到了一段代码,但是怎么看也没有看明白.代码如下:

'新建工程,添加两个文本框txtMessage与txtCommand.
'前者的MultLine=True,用于显示命令行程序的回显;
'后者用于输入命令.
Option Explicit

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any,  
lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO,  
lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long

Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As  
Any) As Long

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As  
Long

Private Declare Function SetHandleInformation Lib "kernel32" (ByVal hObject As Long, ByVal dwMask As Long, ByVal dwFlags As Long) As Long

Private Declare Function SetNamedPipeHandleState Lib "kernel32" (ByVal hNamedPipe As Long, lpMode As Long, lpMaxCollectionCount As Long, lpCollectDataTimeout As Long) As Long

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Private Const STARTF_USESTDHANDLES = &H100
Private Const HANDLE_FLAG_INHERIT = 1
Private Const DETACHED_PROCESS = &H8
Private Const PIPE_NOWAIT = &H1

Dim hReadPipe As Long
Dim hWritePipe As Long
Dim hChildReadPipe As Long
Dim hChildWritePipe As Long

'上面是公共部分.

Private Sub Form_Load()
txtCommand.Text = ""
txtMessage.Text = ""
txtMessage.Locked = True

' 创建管道
CreatePipe hReadPipe, hWritePipe, ByVal 0, ByVal 0
CreatePipe hChildReadPipe, hChildWritePipe, ByVal 0, ByVal 0
SetHandleInformation hWritePipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT
SetHandleInformation hChildReadPipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT
Dim dwMode As Long
dwMode = PIPE_NOWAIT
SetNamedPipeHandleState hReadPipe, dwMode, ByVal 0, ByVal 0

' 创建CMD进程
Dim stProcessInfo As PROCESS_INFORMATION
Dim stStartInfo As STARTUPINFO
stStartInfo.cb = LenB(stStartInfo)
stStartInfo.dwFlags = STARTF_USESTDHANDLES
stStartInfo.hStdError = hWritePipe
stStartInfo.hStdOutput = hWritePipe
stStartInfo.hStdInput = hChildReadPipe

Dim strExe As String
strExe = "cmd"
If False = CreateProcess(ByVal vbNullString, ByVal strExe, ByVal 0, ByVal 0, ByVal True, ByVal DETACHED_PROCESS, ByVal 0, ByVal vbNullString, stStartInfo, stProcessInfo) Then
MsgBox "启动进程失败!"
Exit Sub
Else
CloseHandle stProcessInfo.hThread
CloseHandle stProcessInfo.hProcess
End If
ReadFromChildPipe
End Sub

Private Sub Form_Unload(Cancel As Integer)
CloseHandle hReadPipe
CloseHandle hWritePipe
CloseHandle hChildReadPipe
CloseHandle hChildWritePipe
End Sub

Private Sub txtCommand_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Dim nWrite As Long
Dim strBuffer As String
strBuffer = txtCommand.Text & vbCrLf
Dim bResult As Boolean
bResult = WriteFile(ByVal hChildWritePipe, ByVal strBuffer, ByVal Len(strBuffer), nWrite, ByVal 0)
If bResult = True Then
ReadFromChildPipe
Else
MsgBox "写入失败."
End If
txtCommand.Text = ""
End If
End Sub

Private Sub ReadFromChildPipe()
Dim nRead As Long
Dim strBuffer As String
Dim nBufferLen As Long
nRead = -1
Do While nRead <> 0
nBufferLen = 65536
strBuffer = String(nBufferLen, Chr(0))
Sleep 10
ReadFile hReadPipe, ByVal strBuffer, ByVal nBufferLen, nRead, ByVal 0
Sleep 10
If nRead <> 0 Then
strBuffer = Left(strBuffer, nRead)
txtMessage.Text = txtMessage.Text & strBuffer
txtMessage.SelStart = Len(txtMessage.Text)
End If
Loop
End Sub
!===============================================================
没看明白是什么意思,兄弟能否告诉我一下这段代码是否能实现我想要的功能 ?如果可以应当怎么改呢?
多谢!
2010-09-01 16:34
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:5 
DOS 好像支持 < 和 > 转向输入输出.
你自己试一下.

如果你的程序是 命令行 输入输出的.那么可以这样调用.

可执行程序名 参数 <输入的TXT文件名

程序执行时,会到 输入的TXT文件名 一个字符一个字符的读取,然后放到程序里去.就向键盘输入一样的.


你自己试一下吧.以前 DEBUG 程序就可以用这种方法来.

授人于鱼,不如授人于渔
早已停用QQ了
2010-09-01 17:59
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
收藏
得分:0 
回复 7楼 风吹过b
先谢谢版主。但是我按照你的意思试过了如下:
Private Sub Command3_Click()
DoEvents
Shell "st.exe<01.txt", vbNormalFocus
End Sub
Private Sub Text3_Change()
Open "01.txt" For Output As #1
Print #1, Text3.Text
Close #1
End Sub
先将TEXT3中的信息写入到01.TXT中,然后按你说的,但是好像不行啊 ?
另有一问题:我调用这个ST.EXE时,程序运行了,但是不知道怎么回事,Command3点下去之后调用了很多次?
这个ST.EXE 调用之后需要操作者手动输入计算参数才可以运行,是不是和这个有关系?应该怎样改进。盼回复
2010-09-02 19:50
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
这种方法并不能适用所的 CMD 程序,我只知道这是一种方法。

DOS 的转向 输入输出 功能。 我现在没有条件去测试啊。。。。。。。。。。

授人于鱼,不如授人于渔
早已停用QQ了
2010-09-02 21:30
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
收藏
得分:5 
st.exe 是控制台程序吗?(黑白窗口)
2010-09-03 07:15
快速回复:求助:急!希望高手进来帮下小弟的忙.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030669 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved