Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Shell hidden does not work with all (work around)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Shell hidden does not work with all (work around)

    It looks like some programs can't be started hidden using SHELL

    If the caption is known this hides the program.
    Can't start my own VB program hidden using SHELL.

    Is there an easier way than having to search for your own runncé€,program?
    Sure seems like a lot of extra work.
    This code might come in handy in the future.

    Note: Also tried using CreateProcess
    There is a bunch of useful code in here
    WaitShell and enumerate all running programs

    Suggestions welcome.

    Code:
    #COMPILE EXE
      #DIM ALL
      #INCLUDE "WIN32API.INC"
      GLOBAL AllPrograms() AS STRING '(0,0)name  (0,1)handle
      GLOBAL Org AS LONG
      'Hide show running programs
    FUNCTION PBMAIN AS LONG
      LOCAL result AS LONG
      LOCAL pid AS LONG
      LOCAL sProgramName AS STRING
      LOCAL sCaption     AS STRING
      sProgramName = "doty.exe"
      sCaption     = "doty"
      result = SHELL(sProgramName)  'start program
      SLEEP 200                     'be sure it is enough, value unknown
      pid = IsRunning(sCaption)     'get pid from caption
      IF pid THEN
        ? "%SW_HIDE " + $DQ + sCaption + $DQ
        ShowWindow(pid,%SW_HIDE)
        ? "%SW_NORMAL " + $DQ + sCaption + $DQ
        ShowWindow(pid,%SW_NORMAL)
      ELSE
        ? "Not running"
      END IF
     
    END FUNCTION
    '-----------------------------------------------------------------------------------------------------------
    FUNCTION WaitShell(BYVAL CmdLine AS STRING, TimeOutInMilliseconds AS LONG) AS DWORD
      'Shells to another process  with optional wait.  Use %INFINITE to wait until done
      LOCAL Si        AS STARTUPINFO
      LOCAL Pi        AS PROCESS_INFORMATION
      LOCAL Pid       AS DWORD
      Si.cb = SIZEOF(Si)
      Si.dwFlags = %STARTF_USESHOWWINDOW
      Si.wShowWindow = %SW_SHOWNORMAL
      'Si.WshowWindow = %SW_HIDE         'This doesn't work
      Pid = CreateProcess("", _
                     BYVAL STRPTR(CmdLine), _
                     BYVAL %NULL, _
                     BYVAL %NULL, _
                     0, _
             &sp%3p;       %NORMAL_PRIORITY_CLASS, _
    <.121t;                 BYVAL %NULL, _
                     BYVAL %NULL, _
                     Si, _
                     Pi)
      IF Pid THEN
        'Call WaitForInputIdle(pi.hProcess, %IGNORE)       'not needed
         FUNCTION =  WaitForSingleObject(pi.hProcess, TimeOutInMilliseconds)
         CALL CloseHandle(pi.hProcess)
         CALL CloseHandle(pi.hThread)
       ELSE
          ? "Unable to CreateProcess, error" + STR$(GetLastError)
       END IF
    END FUNCTION
     'ShowWindow() Commands
    '%SW_HIDE            = 0
    '%SW_SHOWNORMAL      = 1
    '%SW_NORMAL          = 1
    '%SW_SHOWMINIMIZED   = 2
    '%SW_SHOWMAXIMIZED   = 3
    '%SW_MAXIMIZE        = 3
    '%SW_SHOWNOACTIVATE  = 4
    '%SW_SHOW            = 5
    '%SW_MINIMIZE        = 6
    '%SW_SHOWMINNOACTIVE = 7
    '%SW_SHOWNA          = 8
    '%SW_RESTORE         = 9
    '%SW_SHOWDEFAULT     = 10
    '%SW_FORCEMINIMIZE   = 11
    '%SW_MAX = 11
     
    FUNCTION IsRunning(SearchFor AS STRING) AS LONG
       DIM search_for AS ASCIIZ * 255
       REDIM AllPrograms (0 TO 255, 0 TO 255)
       DIM S AS LONG
       DIM foundIt AS LONG
       DIM x AS STRING
       LOCAL org AS LONG
       org = 0
       search_for = SearchFor 'Search text passed as a pointer in user defined lparam&
       'My_FindWindow finds first window with search_for string in title
       s&=EnumWindows(CODEPTR(My_FindWindow),VARPTR(search_for))
       FoundIt = 0
       FOR org = 0 TO 255
           'Show running programs and handles
           'IF LEN(AllPrograms(0,org)) THEN  'Window name
           '   MSGBOX STR$(org) + CHR$(34)+ AllPrograms(0,org)+ CHR$(34)
             'PRINT AllPrograms(1,org)       'Window handle
           'END IF
          'See if program running can be found
          FoundIt = INSTR(AllPrograms(0,org),SearchFor)
          IF FoundIt THEN
             'MSGBOX "Found in element" + STR$(org)+ "at bite" + STR$(FoundIt)  _
             '       + CHR$(34) + Allprograms(0,org) + CHR$(34) + "Handle " + ALLprograms(1,org)
             FUNCTION = VAL(AllPrograms(1,org))
             'MSGBOX STR$(HandleToDDocWindow)
             'CALL SetForeGroundWindow(HandleToDDocWindow)
             'FUNCTION = 1
             EXIT FOR
          END IF
       NEXT
    END FUNCTION
    FUNCTION My_FindWindow(BYVAL whnd&,BYVAL lparm&) AS LONG
       DIM temp_title AS ASCIIZ *255
       DIM temp_search_text AS ASCIIZ PTR
       DIM s AS LONG
       DIM org AS LONG
       IF lparm&>0 THEN
          temp_search_text=lparm&   'set pointer to search text using lparm&
          s&=GetWindowText(whnd&,temp_title,(255))
          IF LEN(temp_title) THEN
             AllPrograms(0,org) = temp_title     'program name
             All> rams(1,org) = STR$(whnd&)    'program handle
             INCR org
          END IF
       END IF
       FUNCTION = %true  'continue searching
    END FUNCTION
    Last edited by Mike Doty; 31 May 2009, 10:11 AM.
    How long is an idea? Write it down.

  • #2
    Afaik VB follows the startupinfo settings.
    You may try to explitly load and show the form.

    But then, why hide and still want to show a form?
    hellobasic

    Comment


    • #3
      I continually get garbage in my postings.
      This time I'm leaving it and reporting to PowerBASIC.
      The CreateProcess code has some extra junk in there.

      &sp%3p; %NORMAL_PRIORITY_CLASS, _
      <.121t; BYVAL %NULL, _
      How long is an idea? Write it down.

      Comment


      • #4
        I am shelling to the program, not loading it.

        I do not want to show the form.
        I wrote this code to hide the form and added a test to show it.
        Hiding and showing forms can be useful.

        I wanteding be able to SHELL to my own program with a window style of hidden.

        I think you hit on it.
        Do not use a default startup form, instead use SUB Main in Visual Basic.

        Still wondering if there is another way to hide a window without searching for its caption if you started the program with Shell.
        Last edited by Mike Doty; 31 May 2009, 11:06 AM.
        How long is an idea? Write it down.

        Comment


        • #5
          It's not normal to execute it hidden.
          Side-effects may occure but then, this is your own VB app isn't?
          You can adapt it to allow it be used hidden.

          If you create a shortcut to an app you'll have 3 options, normal, minimized or maximized.
          There is no hidden.

          I run console apps hidden.
          Last edited by Edwin Knoppert; 31 May 2009, 11:52 AM.
          hellobasic

          Comment


          • #6
            Originally posted by Mike Doty View Post
            I continually get garbage in my postings.
            This time I'm leaving it and reporting to PowerBASIC.
            The CreateProcess code has some extra junk in there.
            _
            I had the same problem just recently, Mike. The was a specific sequence of characers the forum editor just wouldn't accept and/or would change both within the code /code tags and outside. I don't recall specifically what it was right now but I ended up having to upload the code as an attachment.

            ==================================
            "A man with a new idea is a crank
            until the idea succeeds."
            Mark Twain
            ==================================
            It's a pretty day. I hope you enjoy it.

            Gösta

            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

            Comment


            • #7
              Did you try to move it out of the screen view port?

              ...
              Patrice Terrier
              www.zapsolution.com
              www.objreader.com
              Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).

              Comment


              • #8
                That's the reason your first "ShowWindow()" command is supposed to use the 'nCmdShow' passed as parameter four to WinMain.... so the calling program's display preference is used.

                It is, however, the called application's responsibility to respect that choice. If it doesn't there's not a heck of a lot you can do about it without jimmying around with process handles.
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  The message you have entered is too short. Please lengthen your message to at least 2 characters.

                  The above message just occurred after typing a response.

                  Thank you for the suggestions. No, I didn't try moving outside the
                  vie0MR/t using a SHELL (nor do I know how to do that.)
                  Yes, I can control the VB6 program and that is a good idea!

                  I may not even do this, but found it curious VB6 does not like the style hidden on a shell.

                  Gosta,
                  Thank you for mentioning you have seen garbage in posts..
                  I thought this was fixed and may have to start posting differently again.
                  Select all, copy, paste, submit.

                  I am about to click Submit Reply. Hopefully, I will not get the message saying "Message is too short"
                  How long is an idea? Write it down.

                  Comment


                  • #10
                    > may not even do this, but found it curious VB6 does not like the style hidden on a shell.

                    VB does not allow you to specify how your primary window is to be shown?

                    Just as a logical extension of this... I would think DDT programmers would want to control how their first DIALOG SHOW statement is issued if that program is going to be launched via SHELL or something equivalent.
                    e.g
                    Code:
                    FUNCTION WINMAIN (BYVAL hInstance     AS LONG, _
                                      BYVAL hPrevInstance AS LONG, _
                                      BYVAL lpCmdLine     AS ASCIIZ PTR, _
                                      BYVAL iCmdShow      AS LONG) AS LONG
                    
                      DIALOG NEW...
                      CONTROL ADD...
                    
                      DIALOG SHOW STATE   iCmdShow     ' << NEW LINE HERE 
                    
                      DIALOG SHOW MODAL|MODELESS.....
                    This does require 9x+ compiler. From PB/Win 9.01 help:
                    Restrictions
                    In previous versions of PowerBASIC, the DIALOG SHOW STATE was not permitted to be executed before a DIALOG SHOW MODAL or DIALOG SHOW MODELESS statement had been executed for that specific dialog. Starting with this version of PowerBASIC, DIALOG SHOW STATE may be executed before or after the dialog is activated with DIALOG SHOW MODAL or DIALOG SHOW MODELESS statement.
                    This new feature allows you to easily respect your caller's preference.

                    It does require you use the "WinMain" long form for yoru entry point function in lieu of the "PBMAIN" short form, but you can store that in your 'template' file.

                    MCM
                    Michael Mattias
                    Tal Systems Inc. (retired)
                    Racine WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      VB6 has a normal, minimized, maximized.

                      If I ever need to do a shell like this, I might be able to load the program
                      out of the viewable area like Patrice suggested.
                      This was more of a thing found than a thing that will be done.
                      If there is a way to change the way VB loads using the API or some
                      advanced VB feature is very possible.

                      Michael,
                      Thanks for the info.
                      How long is an idea? Write it down.

                      Comment


                      • #12
                        >VB6 has a normal, minimized, maximized.

                        Nice set of choices.

                        I guess then you'd have no choice in your VB program other than to ...

                        1. Create the main window hidden
                        2. Save iCmdShow.
                        3. In WM_CREATE (or whatever 'event' VB equates to that), posting yourself a message to change the 'show state' to the saved iCmdShow... or maybe just call ShowWindow there. (I'm not so sure calling ShowWindow is a good idea under WM_CREATE, so I'd probably go for the message).

                        That way if "hidden" is specified, you won't get that annoying "flash" of a screen you'd get if you allowed the window to be shown normally, then hidden because of the value of iCmdShow.

                        Worth a try perhaps?
                        Michael Mattias
                        Tal Systems Inc. (retired)
                        Racine WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment

                        Working...
                        X