Results 1 to 17 of 17

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

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    [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?

    Please share your thoughts:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const lngShellRun As Long = 0  'The job is still running
    4.  
    5. Private Declare Sub Sleep Lib "KERNEL32.DLL" (ByVal dwMilliseconds As Long)
    6.  
    7. Public Function ExecuteDosCommand(ByVal strCommand As String, _
    8.                         Optional strShellErr As String, _
    9.                         Optional WindowStyle As VbAppWinStyle = vbMinimizedFocus) _
    10.                         As Boolean
    11.  
    12. Dim objShell       As Object 'As WSScript.Shell
    13. Dim objExec        As Object 'As WshScriptExec
    14. Dim strReturn      As String
    15. Dim lngReturn      As Long
    16. Dim strCurrent     As String
    17.  
    18.  
    19. Err.Clear
    20.  
    21. ' Set default values
    22. strShellErr = ""
    23.  
    24. Set objShell = CreateObject("WScript.shell", vbNullString) ' Create the shell object
    25.  
    26. Set objExec = objShell.Exec("CMD /K " & strCommand)
    27.  
    28. Do While objExec.Status = lngShellRun ' Wait for the command to finish
    29.    Sleep 100
    30. Loop
    31.  
    32. ' Get the return from the command that executed
    33. ' NOTE: The "objExec.StdOut" property returns a "Scripting.TextStream" object
    34.  
    35. If objExec.StdOut.AtEndOfStream = False Then
    36.  
    37.     Do While objExec.StdOut.AtEndOfStream = False
    38.        strShellErr = strShellErr & objExec.StdOut.ReadLine
    39.     Loop
    40.    
    41. End If
    42.  
    43. ' Get any errors that occured from the command that executed
    44. ' NOTE: The "objExec.StdErr" property returns a "Scripting.TextStream" object
    45.  
    46. If objExec.StdErr.AtEndOfStream = False Then
    47.     'strShellErr = strShellErr & "ERROR: "
    48.     Do While objExec.StdErr.AtEndOfStream = False
    49.        strShellErr = strShellErr & objExec.StdErr.ReadLine
    50.     Loop
    51. End If
    52.  
    53. ' Clean up
    54. Set objExec = Nothing
    55. Set objShell = Nothing
    56. ExecuteDosCommand = True
    57. Exit Function
    58.  
    59. End Function
    Last edited by sakreg1; Sep 22nd, 2005 at 08:10 AM. Reason: Resolved

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. objShell.Exec("CMD /C " & strCommand)

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Hiding DOS Window (NOT using Shell Command)

    Quote 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. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Hiding DOS Window (NOT using Shell Command)

    Quote 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. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Hiding DOS Window (NOT using Shell Command)

    Oh, ok. Then its easiest to just hide the window completely if thats ok.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_HIDE As Long = 0
    7. Private Const SW_SHOWNORMAL As Long = 1
    8.  
    9. Private Sub Command1_Click()
    10.     ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /C " & strCommand, "C:\", SW_HIDE
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    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. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Hiding DOS Window (NOT using Shell Command)

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

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Hiding DOS Window (NOT using Shell Command)

    Quote 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. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. For x = 0 To UBound(str)
    2.     if instr(str(x),"Login Failed") > 0 then
    3.        msgbox "Failed"
    4.        exit for
    5.     endif
    6.   Next x

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, st As String
    5.   Dim ff As Integer
    6.   Dim strBuff As String
    7.   Dim str() As String
    8.   ff = FreeFile
    9.   Open App.Path & "\to do.txt" For Input As #ff
    10.     strBuff = Input(LOF(ff), ff)
    11.   Close #ff
    12.   str() = Split(strBuff, vbCrLf)
    13.   MsgBox "There are " & UBound(str) + 1 & " lines in the file"
    14.   For x = 0 To UBound(str)
    15.     st = st & str(x) & vbCrLf
    16.   Next x
    17.   MsgBox st
    18. End Sub

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    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. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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. #14

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Hiding DOS Window (NOT using Shell Command)

    Quote 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?
    Quote 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. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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...
    Last edited by RobDog888; Sep 22nd, 2005 at 08:57 AM. Reason: Removing exe's in attachment

  16. #16

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Hiding DOS Window (NOT using Shell Command)

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

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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.
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width