[RESOLVED]Hiding DOS Window (NOT using Shell Command)-VBForums

# Thread: [RESOLVED]Hiding DOS Window (NOT using Shell Command)

1. ## [RESOLVED]Hiding DOS Window (NOT using Shell Command)

The command I am passing to strCommand is

<ftp -s:C:\WINDOWS\Temp\FTP.txt > C:\WINDOWS\Temp\FTPLOG.txt &exit>

As per the code the function will trigger a CMD Prompt with a set FTP Instructions as in FTP.txt
My problem is how we hide the DOS Window?

VB Code:
Option Explicit Private Const lngShellRun As Long = 0  'The job is still running Private Declare Sub Sleep Lib "KERNEL32.DLL" (ByVal dwMilliseconds As Long) Public Function ExecuteDosCommand(ByVal strCommand As String, _                        Optional strShellErr As String, _                        Optional WindowStyle As VbAppWinStyle = vbMinimizedFocus) _                        As Boolean Dim objShell       As Object 'As WSScript.ShellDim objExec        As Object 'As WshScriptExecDim strReturn      As StringDim lngReturn      As LongDim strCurrent     As String  Err.Clear ' Set default valuesstrShellErr = "" Set objShell = CreateObject("WScript.shell", vbNullString) ' Create the shell object Set objExec = objShell.Exec("CMD /K " & strCommand) Do While objExec.Status = lngShellRun ' Wait for the command to finish   Sleep 100Loop ' Get the return from the command that executed' NOTE: The "objExec.StdOut" property returns a "Scripting.TextStream" object If objExec.StdOut.AtEndOfStream = False Then     Do While objExec.StdOut.AtEndOfStream = False       strShellErr = strShellErr & objExec.StdOut.ReadLine    Loop    End If ' Get any errors that occured from the command that executed' NOTE: The "objExec.StdErr" property returns a "Scripting.TextStream" object If objExec.StdErr.AtEndOfStream = False Then    'strShellErr = strShellErr & "ERROR: "    Do While objExec.StdErr.AtEndOfStream = False       strShellErr = strShellErr & objExec.StdErr.ReadLine    LoopEnd If ' Clean upSet objExec = NothingSet objShell = NothingExecuteDosCommand = TrueExit Function End Function

2. ## Re: Hiding DOS Window (NOT using Shell Command)

I'm not sure how to hide it, but you could have it close itself after it finishes.

VB Code:
objShell.Exec("CMD /C " & strCommand)

3. ## Re: Hiding DOS Window (NOT using Shell Command)

Well your using "CMD /K" which will Keep the window open. If you use the /C then it will close after processing. Is this enough of a solution r do you need it totally hidden?

4. ## Re: Hiding DOS Window (NOT using Shell Command)

Originally Posted by RobDog888
Well your using "CMD /K" which will Keep the window open. If you use the /C then it will close after processing. Is this enough of a solution r do you need it totally hidden?
The windows closes after it finish execution. I want that to be totally hidden. This is a security measure. The DOS Window will display the FTP.txt file path which when revealed will help the user to browse the temp folder to fetch the user id and password for the server.

So I want the DOS window to be totally hidden. As I have &exit, the window will close as soon as the FTP job is done

5. ## Re: Hiding DOS Window (NOT using Shell Command)

i believe that you can use "@Echo Off" to turn off the displaying of text in the command window. Have it as your first dos command.

6. ## Re: Hiding DOS Window (NOT using Shell Command)

Originally Posted by RobDog888
i believe that you can use "@Echo Off" to turn off the displaying of text in the command window. Have it as your first dos command.
My concern is the DOS WINDOW TITLE name which will display the entire path. I am Sorry I think I missed saying Title in my Previous Post.

The Dos Window Title would be something like:
C:\Windows\System32\cmd.exe - ftp -s:C:\WINDOWS\Temp\FTP.txt.

7. ## Re: Hiding DOS Window (NOT using Shell Command)

Oh, ok. Then its easiest to just hide the window completely if thats ok.
VB Code:
Option Explicit Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private Const SW_HIDE As Long = 0Private Const SW_SHOWNORMAL As Long = 1 Private Sub Command1_Click()    ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /C " & strCommand, "C:\", SW_HIDEEnd Sub

8. ## Re: Hiding DOS Window (NOT using Shell Command)

ThankYou RobDog888,

The code solved the purpose of hiding the DOS Windows, BUT my current code have the functionality of trapping the DOS FTP Failures. Adding to that I have to Hide the DOS Window. If ShellExecute can solve both the purpose of capturing the DOS FTP failures and hiding the dos windows your valuable guidence is appreciated

9. ## Re: Hiding DOS Window (NOT using Shell Command)

Use > to copy it to a file, and read the file to test it.

10. ## Re: Hiding DOS Window (NOT using Shell Command)

Originally Posted by dglienna
Use > to copy it to a file, and read the file to test it.
ThankYou. You mean to ShellExecute, Pipe and then Read the File for some specified text. Is it easy to implement that?

How do you search for a text "Login Failed" in output.txt, assuming I pipe the DOS Window Console to c:\output.txt

11. ## Re: Hiding DOS Window (NOT using Shell Command)

This reads in the whole file. Then you could look for text using:
This would search every line for the text. If it is found, it would indicate the starting position. If it isn't found, it will return 0.

VB Code:
For x = 0 To UBound(str)    if instr(str(x),"Login Failed") > 0 then       msgbox "Failed"       exit for    endif  Next x

VB Code:
Option Explicit Private Sub Form_Load()  Dim x As Integer, st As String  Dim ff As Integer  Dim strBuff As String  Dim str() As String  ff = FreeFile  Open App.Path & "\to do.txt" For Input As #ff    strBuff = Input(LOF(ff), ff)  Close #ff  str() = Split(strBuff, vbCrLf)  MsgBox "There are " & UBound(str) + 1 & " lines in the file"  For x = 0 To UBound(str)    st = st & str(x) & vbCrLf  Next x  MsgBox stEnd Sub

12. ## Re: Hiding DOS Window (NOT using Shell Command)

After looking forward this approach, I am stuck as the Shell Command (as given by RobDog888) is not waiting. Searching this post gave me some result, but as a beginner I find it difficult to understand.

Can someone point out how to start the file search as given by dglienna after Shell Command (as given by RobDog888) is completely done in a Simple Fashion.

Value that is passed in the strCommand is
"ftp -s:C:\WINDOWS\Temp\FTP.txt > C:\WINDOWS\Temp\FTPLOG.txt &exit"

13. ## Re: Hiding DOS Window (NOT using Shell Command)

You can use the START /W process.exe to wait until a process finishes. You have to create a batch file for it to have any meaning, but the other alternative is a page of API's, which I would be able to show you, if I'm still awake.

14. ## Re: Hiding DOS Window (NOT using Shell Command)

Originally Posted by dglienna
You can use the START /W process.exe to wait until a process finishes. You have to create a batch file for it to have any meaning,
How do you do this?
Originally Posted by dglienna
but the other alternative is a page of API's, which I would be able to show you, if I'm still awake.
API is what is what that is making me afraid, But if you have the time to combine everything, that would be of great help. I do not want to trouble you much. I will have a look of the API

15. ## Re: Hiding DOS Window (NOT using Shell Command)

I think you have to shell a batch file, that calls another batch file, using Start /w, as I couldn't get it to pass any info using shell, shellexecute, or a dos command. I know it works, you can check it with Start /? from a command prompt.

Here is the alternative...

16. ## Re: Hiding DOS Window (NOT using Shell Command)

Thank You for sharing all your thoughts. I feel I have learned a lot today.

17. ## Re: [RESOLVED]Hiding DOS Window (NOT using Shell Command)

dglienna, here is your attachment without the executables. The exe's are not allowed to be uploaded in zips.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured