| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1044 人关注过本帖
标题:求助:急!希望高手进来帮下小弟的忙.
取消只看楼主 加入收藏
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
求助:急!希望高手进来帮下小弟的忙.
本人菜鸟一个,最近要使用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
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
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
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
zpc860111
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-8-29
收藏
得分:0 
回复 10楼 bczgvip
是的,我不懂为什么command点下之后会连续的调用几次,这个问题在调用别的不需要手动输入参数的EXE的时候并没有出现,请问这个问题该怎么解决谢谢!
2010-09-03 15:22
快速回复:求助:急!希望高手进来帮下小弟的忙.
数据加载中...
 
   



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

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