Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Utility in cmd going into interactive mode and VB code hanging

Status
Not open for further replies.

vmaruv

Programmer
Apr 8, 2002
102
US
Hi,

From my VB6 program, I am executing a utility in command mode using coenspan.exe (Thanks to everyone. got help on doing this from this forum in my earlier post.)
This utility takes user name, password and other inputs from users, executes at the background and pops up the results in message box.

However, I have noticed that when the username or password are incorrect, the utility operates in interactive mode. (Asks for user name and password interactively from the user) Because of this my program is waiting for indefenite period and hanging. I am having to terminate the program each time this is happening.

Is there a way I can resolve this issue ? Maybe wait for the output for a specific period of time and then return the control back to the form displaying a message to the user or something like that. [ponder]

Your suggestions and help is appreciated.

Thanks,
Vidya.
 
I am not familiar with coenspan.exe, but what do you want happen when users input is unacceptable? Do you want to react to it via VB?
If the utility stores these data somewhere that your vb could read, then maybe you could use sendkeys to cancel the entry requests and do something about the wrong entry with VB.

Eman_2005
Technical Communicator
 
Actually, when the user inputs are valid, the utility executes and I'm capturing the output and displaying through text box once the execution is complete.But when the user inputs are not correct, the utility (at the background) asks from the user (eg, username:----- once the user name is entered Password:****
then tries to execute.). Since this is going into interactive mode, I am not able to capture it from my code. My VB code is waiting for the utility to complete its execution and return the results so that it can be shown in the text box provided.

One idea i have in mind is , I can wait for a specified time for my program to execute and if it is taking more than the specified time (in case where it is going into interactive mode) , it should throw a message to the user and terminate.

or if there is any better way, pls. suggest.
 
You capture the output from the utility? So you cannot capture what the user enters as ID and password, if I understood well.
However, if you look at the matter from the user's view too.
If you put a waiting time, it might brutally stop the interaction while the user is typing (or just about to type in) the correct password, for example. In this case, I would put some kind of time lapse display that shows the user how much time is left to retry.

How about reversing actions a little, or should I say to have the front end in VB's hand?
Create an interface with VB that collects the ID and Password and send them to the utility in the background. This way your VB progam can react in a more user-friendly manner, and as long as your VB program did not get the expected kind of output, you know that the utility still needs the correct ID and password, which you can request again from the user but through your VB interface rather than away from it.
What do you think?


Eman_2005
Technical Communicator
 
I think I am not clear...I'm sorry.
I am doing exactly what you have suggested. I am collecting the username and password from VB front end and executing a batch file that will invoke the utility.

But the utility works in such a way that, if the username and password supplied are incorrect, it goes into interactive mode until the user give username and password again. So this is what happens if I supply wrong username and password from my VB code also.

At this stage, my VB code cannot detect that the utility is in interactive mode at the background. And waits indefinitely (as the utility is waiting).

I hope I am clear. Thanks a lot for taking the interest in reading my problem and trying to provide a solution.

Thanks again !
Vidya.
 
Actually, reading your original post, I see that you were quite clear. Sorry to have not read you carefully enough.

If someone in this forum knows better than me how to detect this interaction (that happens in case of incorrect entry), they could advise you better than me.
Meanwhile, you can apply your idea ;-)

Good luck, and sorry for not being of much use.

Eman_2005
Technical Communicator
 
Hi,

I've come to a solution almost but am facing a different problem..If anyone could help.
I have written another utility (say check.exe) that takes the user name and password and tries to login to the application before it executes the main utility (backup.exe).

I am executing both the utilities from a batch file at a location (C:\run.bat). Initially I am trying to execute the batch file for check.exe and then use the same location to execute backup.exe. I am getting a type mismatch and file already open errors. Below is my code. Please suggest what mistake I'm doing.
Thanks in Advance.


Dim root As String
Dim data As String
Dim username As String
Dim password As String
Dim msg0 As String
Dim msg1 As String
Dim msg2 As String
Dim msg3 As String
Dim msg4 As String
Dim msg5 As String
Dim location As Stringlocation = "c:\run.bat" '

msg0 = "cd c:\"
msg1 = "set ROOT=" + root
msg2 = "set DATA=" + data
msg3 = "call" + " " + data + "\vars"
msg4 = "check.exe -u=" + username + " " + "-p=" + password
msg5 = "backup.exe -u=" + username + " " + "-p=" + password
'Open the file if it exists, if not, create it.
Open location For Output As #1
Print #1, msg1
Print #1, msg2
Print #1, msg3
Print #1, msg4

ExecuteApp (location)

If InStr(sOutput, Error) > 0 Then
MsgBox "Incorrect Parameters; Please check"
Else
Close location
sOutput = ""
Open location For Output As #1

'Write the messege into the file
Print #1, msg0
Print #1, msg1
Print #1, msg2
Print #1, msg3
Print #1, msg5
Close #1

ExecuteApp (location)

**************************

Private Function ExecuteApp(sCmdline As String) As String
Dim proc As PROCESS_INFORMATION, ret As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long 'The handle used to read from the pipe.
Dim hWritePipe As Long 'The pipe where StdOutput and StdErr will be redirected to.
'Dim sOutput As String
Dim Ask As Byte
Dim lngBytesRead As Long, sBuffer As String * 256
sOutput = ""
sa.nLength = Len(sa)
sa.bInheritHandle = True

ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox "CreatePipe failed. Error: " & Err.LastDllError
Exit Function
End If

start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
' Redirect the standard output and standard error to the same pipe
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
start.wShowWindow = SW_HIDE

' Start the shelled application:
' if the program has to work only on NT you don't need the "conspawn"
ret = CreateProcessA(0&, "conspawn " & sCmdline, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

If ret = 0 Then
MsgBox "CreateProcess failed. Error: " & Err.LastDllError
Exit Function
End If

' The handle wWritePipe has been inherited by the shelled application
' so we can close it now
CloseHandle hWritePipe

' Read the characters that the shelled application
' has outputed 256 characters at a time
Do
ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&)
sOutput = sOutput & Left$(sBuffer, lngBytesRead)
Loop While ret <> 0 ' if ret = 0 then there is no more characters to read

CloseHandle proc.hProcess
CloseHandle proc.hThread
CloseHandle hReadPipe

ExecuteApp = sOutput



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top