Announcement

Collapse
No announcement yet.

SHELL / CreateProcess inconsistent

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

  • SHELL / CreateProcess inconsistent

    I'm not sure how to takle debugging this. I have a largish program which as one of its functions will accept a users command and issue it via SHELL. What I am seeing is that it is only working while running in the PowerBasic IDE, it does NOT run from the compiled EXE file.

    So I extract the small bit of code which does the shell to a test program. It functions perfectly within the IDE, but it ALSO runs fine as a compiled EXE.

    What I see when it fails is the briefest flash of a DOS window for just an instant.
    Here's the small test pgm.
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32Api.inc"
    
    FUNCTION PBMAIN () AS LONG
    LOCAL lclCmd AS ASCIZ * 100
    LOCAL rcProcess AS PROCESS_INFORMATION
    LOCAL rcStart AS STARTUPINFO
    LOCAL RetC AS LONG
       lclCmd = ENVIRON$("COMSPEC") + " /C " + "TestBatch.BAT Operand1 Operand2"    ' Setup command
       rcStart.cb = LEN(rcStart)                             ' Initialize the STARTUPINFO structure:
    ' Try using the SHELL function
       RetC = SHELL(lclCmd,1)
    ' Try using CreateProcess
       RetC = CreateProcess(BYVAL %NULL, BYCOPY lclCmd, BYVAL %NULL, BYVAL %NULL, 1&, _
                            %CREATE_NEW_CONSOLE OR %NORMAL_PRIORITY_CLASS, BYVAL %NULL, _
                            BYVAL %NULL, rcStart, rcProcess)
       CALL CloseHandle(rcProcess.hThread)
       CALL CloseHandle(rcProcess.hProcess)
    END FUNCTION
    and here's the tiny BAT file I'm testing with.
    Code:
    @echo off
    Echo Starting batch file
    echo Parameter One is %1
    echo Parameter Two is %2
    echo End of batch file
    pause
    So -- the test program works both IN the IDE and as a compiled EXE.

    The same code in the main program works fine in the IDE, but not in the compiled EXE.

    Any ideas what the difference is? The main program does have 1 dialog window open when it does the SHELL, the little test program does not.

    Displaying ERR always shows no error occurred. Note the test program has both SHELL and CreateProcess. They BOTH work fine.

    They also both work fine in the full program within the IDE, but not from the EXE.

    ????? I've gotta be missing something real simple here.

    George

  • #2
    Try
    SHELL lclCmd,1
    instead which will not return to the program until the child process is finished.

    I understand this may not be desirable but at least for testing you may be
    able to pinpoint some problem.
    Client Writeup for the CPA

    buffs.proboards2.com

    Links Page

    Comment


    • #3
      Does EXE program 'start in' the same directory as does the IDE? Won't find batch file if it doesn't.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Guess the "Brief-est" flash is the Dos Window opening and closing
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          Michael:
          All programs, source, IDE, EXE, BAT are in the same directory. Both the test pgm and the large main pgm have everything here.

          Cliff: Right, I wish it could be kept open to see if it says anything useful.

          Fred: I'll give your suggestion a try.

          George

          Comment


          • #6
            The 'best':

            AllocConsole()
            Shell "My.bat"
            FreeConsole()
            hellobasic

            Comment


            • #7
              George if it does the same thing using the shell statement id have to agree with Cliff as to the 'flash' and that michael is probably correct that it is probably not finding the bat file.
              Client Writeup for the CPA

              buffs.proboards2.com

              Links Page

              Comment


              • #8
                Displaying ERR always shows no error occurred. Note the test program has both SHELL and CreateProcess. They BOTH work fine.
                That means the CMD shell was succesfully launched; but not necessarily whatever you asked CMD to do.

                >All programs, source, IDE, EXE, BAT are in the same directo

                So what? That is not the same as "current directory." You could always display CURDIR$ to see where you really are.

                > Cliff: Right, I wish it could be kept open to see if it says anything useful.

                Try using "/K" instead of /C." That should leave the command-shell console open; maybe you will find those useful messages.

                Or, while I'm sure the CMD file in real life does something more than display the passed parameters, maybe you could get the cmd file 'out of the loop' by executing Shell/CreateProcess for each task the cmd file does, or just replacing command-line utilties with in-line code.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  OK Guys,

                  I originally said

                  ????? I've gotta be missing something real simple here.

                  and it was true!

                  I had icons on my desktop for the main program and the test program which were created with a normal Right-Click -> Send to desktop (shortcut). These were what I was using to test the compiled EXEs.

                  Well the main program one was faulty, I'd created it pointing at my distribution directory and not the development one.

                  So Michael was quite right, it wasn't finding the BAT file.



                  Sorry for the interruption, but thanks for helping me see over the rut I was in.

                  George

                  Comment

                  Working...
                  X