Announcement

Collapse
No announcement yet.

run batch file from EXE

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

  • run batch file from EXE

    Hi,
    Probably a simple question but I'm trying to create a file uploader for web pages and (in proc IIS ) dlls (PB DLLs that is).
    Anyway, the trick is to have a command from a webpage that is handled in the underlying dll. This command starts a simple PB exe.

    If I run this Exe from the Explorer manually, all works well. If I start it from the Dll using SHELL, the exe is started, but it doesn't do the trick.
    The Exe creates a .bat file that has to be executed.

    Code:
    OPEN Root + "\IISSTOP.BAT" FOR OUTPUT AS #2
    PRINT #2, "NET STOP IISADMIN / Y"
    PRINT #2, "MTXSTOP"
    CLOSE #2
    
    SLEEP 5000
    
    SHELL BasicRoot + "\IISSTOP.BAT"
    
    BEEP
    BEEP
    This .bat file should be executed, no? Well, it doesn't.

    I also tried this (without any luck), found it on the forum
    Code:
        LOCAL cmd AS STRING, cmdParam AS STRING
        cmd = "command.com"
        cmdParam = BasicRoot + "\IISSTOP.BAT"
        
        ShellExecute BYVAL %NULL, "open", BYVAL STRPTR(cmd), BYVAL STRPTR(cmdParam), BYVAL STRPTR(BasicRoot), %SW_SHOWNORMAL
    Or this:
    Code:
        DIM rcProcess AS PROCESS_INFORMATION
        DIM rcStart AS STARTUPINFO
        DIM RetC AS LONG
    ' Initialize the STARTUPINFO structure:
        rcStart.cb = LEN(rcStart)
    ' Start the shelled application:
        RetC = CreateProcess(BYVAL %NULL, BasicRoot + "\IISSTOP.BAT", BYVAL %NULL, BYVAL %NULL, 1&, %CREATE_NEW_CONSOLE OR %NORMAL_PRIORITY_CLASS, BYVAL %NULL, BYVAL %NULL, rcStart, rcProcess)
    ' Wait for the shelled application to finish:
        RetC = WaitForSingleObject(rcProcess.hProcess, %INFINITE)
        CALL GetExitCodeProcess(rcProcess.hProcess, RetC)
        CALL CloseHandle(rcProcess.hProcess)
    Any ideas? I've seen more people struckle with this. A good explanation of all details (what options do you have, when do you use what) would be great, I think.

    Hope someone can help.

    Greetings
    Jeroen

    ------------------

  • #2
    There is a GREAT GREAT sharware DLL under construction. I have a licensed copy of it now and use it to do my IIS stop/restart...
    Trust me, I tried the batch file thing, it doesn't work and you won't get error responses very well.
    This DLL is all of 8k or so in size and allows PB to interact with the AX module (It does the work for you)...

    I'll email the author when I get home, if he is not up here reading already and see if you can get a copy.

    I don't know if it's officially shareware or if he's willing to give it out, I don't know.....

    It will make life easier for you for sure...
    TNGCOnfig inside of TNGMP3 now uses this and it works flawless..

    Scott

    ------------------
    Scott
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      Jeroen --
      It's not clear, what are you doing, but probably troubles linked with async. starting.
      For example, a.bat includes
      b.exe - GUI
      c.exe - dos

      OS starts b.exe and don't wait. That's why I use START /w b.exe in batch file


      [This message has been edited by Semen Matusovski (edited May 21, 2001).]

      Comment


      • #4
        Agreed, if you want to pursue this method that sounds a lot more reliable..
        But I thought there was a problem wiht shelling to a BAT file specifically? I know you can shell to EXE's and .COM's etc...
        Perhaps I misunderstood something, PBDos did it well but long ago I thought I read up here where you can't use a certain kind of shell to a .BAT file.


        Meanwhile, the DLL I refer to uses these functions:

        Code:
        '*********************************************************************
        '*CreateVirtualRoot Parameters:
        '*szName = Name of Virtual Path
        '*szPath = Actual Physical Path
        '*Permissions:
        '*The following values are for the common permission flags. 
        '*These may be added together to form a combination of permissions 
        '*for the virtual root.
        '*      1 = Read Access
        '*      2 = Write Access
        '*      4 = Execute Access (including Script)
        '*      512 = Script Access
        '*Site:     Refers to site under the server in your IIS Manager.  (Usually 1)
        '*Auth:
        '*      MD_AUTH_ANONYMOUS               1
        '*      MD_AUTH_BASIC                   2
        '*      MD_AUTH_NT                      4    // Use NT auth provider (like NTLM)
        '*szStatus:
        'Holds the error string for the call.
        'Returns 0 if Failed / 1 if Succeeded
        Declare Function CreateVirtualRoot Lib "isssetup.dll" Alias "CreateVirtualRoot" (szName As Asciiz, szPath As Asciiz, ByVal Permissions As Dword, ByVal Site As Dword, ByVal Auth As Dword, szStatus As Asciiz) As Integer
        
        '*********************************************************************
        '*DeleteVirtualRoot Parameters:
        '*szName = Name of Virtual Path
        '*Site      Refers to site under the server in your IIS Manager.  (Usually 1)
        'Returns 0 if Failed / 1 if Succeeded
        Declare Function DeleteVirtualRoot Lib "isssetup.dll" Alias "DeleteVirtualRoot" (szName As Asciiz, ByVal Site As Dword) As Integer
        
        '*********************************************************************
        'Stops the IIS Server
        '*StopIIS Parameters:
        '*Site      Refers to site under the server in your IIS Manager.  (Usually 1)
        'Returns 0 if Failed / 1 if Succeeded
        Declare Function StopIIS Lib "isssetup.dll" Alias "StopIIS" (ByVal Site As Integer) As Integer 
        
        
        '*********************************************************************
        'Starts the IIS Server
        '*StartIIS Parameters:
        '*Site      Refers to site under the server in your IIS Manager.  (Usually 1)
        'Returns 0 if Failed / 1 if Succeeded
        Declare Function StartIIS Lib "isssetup.dll" Alias "StartIIS" (ByVal Site As Integer) As Integer 
        
        '*********************************************************************
        'Sends Command to server
        '*CommandIIS Parameters:
        '*Site      Refers to site under the server in your IIS Manager.  (Usually 1)
        '*Cmd   Refers to one of below mentioned parameters
        '   1 = Start
        '   2 = Stop
        '   3 = Pause
        '   4 = Coninue
        'Returns 0 if Failed / 1 if Succeeded
        Declare Function CommandIIS Lib "isssetup.dll" Alias "CommandIIS" (ByVal Site As Integer, ByVal Cmd As Dword) As Integer 
        
        
        '*********************************************************************
        'Get's status from server
        '*GetStatus Parameters:
        '*Site      Refers to site under the server in your IIS Manager.  (Usually 1)
        'Returns the servers status
        '   0 = Call Failed
        '   1 = Server starting. 
        '   2 = Server started. 
        '   3 = Server stopping. 
        '   4 = Server stopped. 
        '   5 = Server pausing. 
        '   6 = Server paused. 
        '   7 = Server continuing. 
        'Returns Status
        Declare Function GetStatus Lib "isssetup.dll" Alias "GetStatus" (ByVal Site As Integer) As Dword 
        
        
        'Valid Commands for CommandIIS
        %MD_SERVER_COMMAND_START         = 1
        %MD_SERVER_COMMAND_STOP          = 2
        %MD_SERVER_COMMAND_PAUSE         = 3
        %MD_SERVER_COMMAND_CONTINUE      = 4
        
        'Valid Status for GetStatus
        %MD_SERVER_STATE_STARTING        = 1
        %MD_SERVER_STATE_STARTED         = 2
        %MD_SERVER_STATE_STOPPING        = 3
        %MD_SERVER_STATE_STOPPED         = 4
        %MD_SERVER_STATE_PAUSING         = 5
        %MD_SERVER_STATE_PAUSED          = 6
        %MD_SERVER_STATE_CONTINUING      = 7
        
        'Valid Permission Types for CreateVirtualRoot.
        %MD_ACCESS_READ                 = &H00000001    '// Allow for Read
        %MD_ACCESS_WRITE                = &H00000002    '// Allow for Write
        %MD_ACCESS_EXECUTE              = &H00000004    '// Allow for Execute
        %MD_ACCESS_SCRIPT               = &H00000200    '// Allow for Script execution
        %MD_ACCESS_NO_REMOTE_WRITE      = &H00000400    '// Local host access only
        %MD_ACCESS_NO_REMOTE_READ       = &H00001000    '// Local host access only
        %MD_ACCESS_NO_REMOTE_EXECUTE    = &H00002000    '// Local host access only
        %MD_ACCESS_NO_REMOTE_SCRIPT     = &H00004000    '// Local host access only
        
        ' Valid values for AUTHORIZATION
        %MD_AUTH_ANONYMOUS               = &H00000001
        %MD_AUTH_BASIC                   = &H00000002
        %MD_AUTH_NT                      = &H00000004    '// Use NT auth provider (like NTLM)
        ------------------
        Scott
        Scott Turchin
        MCSE, MCP+I
        http://www.tngbbs.com
        ----------------------
        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

        Comment


        • #5
          Hi,

          Thanks for all the response.

          Semen, what I'm trying to do is run a dos .bat file from a PB exe. The thing is: when I run the PB Exe manually (double click in Windows Explorer), it works perfectly.
          Yet, when I start the execute from another program using the SHELL function, the Pb Exe starts but doesn't execute the .bat file.

          I don't really understand the difference between a 'automated' start and a manual start. Yet, the results are different.
          It sounds to me this must be possible (although Scott might be right... )

          To illustrate:

          PB Exe (let's call it STOP1.EXE):
          FUNCTION PBMain() AS LONG

          OPEN Root + "\IISSTOP.BAT" FOR OUTPUT AS #2
          PRINT #2, "NET STOP IISADMIN / Y"
          PRINT #2, "MTXSTOP"
          CLOSE #2

          SLEEP 5000

          SHELL "IISSTOP.BAT"
          BEEP
          BEEP

          END FUNCTION


          If I just double click the exe, it works fine: it runs the created .bat file and - therefore - stops IIS.


          When I use a different program to start this STOP1.EXE it looks like this

          MyDll:

          result& = SHELL( "STOP1.EXE" )

          This is async. and it has to be that way. The dll is linked to a web page and returns a message that the exe has started (and IIS will be stopped).


          Hope someone can help,

          Regards

          Jeroen

          ------------------


          [This message has been edited by jeroen brouwers (edited May 22, 2001).]

          Comment


          • #6
            To run a BAT file requires the command interpreter (CMD.EXE or COMMAND.COM) to be running. There is no DOS layer "underneath" a PB/DLL executable, so you have to explicitly launch it and pass the BAT file as a parameter.

            In your initial code, you queries the COMSPEC environment variable, but you did not add a space between that and the BAT file name... I've not tested this, but it could be one cause of your problems.

            In your above code, you did not launch the command interpreter.

            Therefore, try something like this:
            Code:
            SHELL ENVIRON$("COMSPEC") + " " + "/c" + " " + batfilename$
            Note that I explicitly separated the space characters for clarity... you could concatenate them thus:
            Code:
            SHELL ENVIRON$("COMSPEC") + " /c " + batfilename$
            I hope this helps!

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              jeroen,

              This one has been running for a long time with np problems at all.

              Regards,

              [email protected]

              Code:
              '##########################################################################
                
              SUB Run_Program
                
                  LOCAL fname$
                  LOCAL rv&
                
                  fname$ = GetFileName(hWnd,"Run Program or Help File","*."+runf$)
                  if fname$ = "" then exit sub
                
                  Select Case lcase$(right$(fname$,3))
                    Case "exe"
                      ! jmp rpRun
                    Case "bat"
                      ! jmp rpRun
                    Case "com"
                      ! jmp rpRun
                    Case "pif"
                      ! jmp rpRun
                    Case "hlp"
                      runhlp$="winhelp "+fname$
                      WinExec ByCopy runhlp$, 1
                      ! jmp rpOut
                    Case Else
                        MessageBox hWnd,ByCopy "File must be EXE, BAT, COM, HLP or PIF", _
                                           "Cannot run that file",%MB_ICONINFORMATION
                        ! jmp rpOut
                  End Select
                
                  rpRun:
                  WinExec ByCopy fname$, 1
                
                  rpOut:
                
              END SUB
              
              ' #########################################################################
              ------------------
              hutch at movsd dot com
              The MASM Forum - SLL Modules and PB Libraries

              http://www.masm32.com/board/index.php?board=69.0

              Comment


              • #8
                Thanks Lance, that did the trick.

                I will post some source code on the forum which is basically a file uploader that also works for dll or exe. You can use it in combination with any ASP Upload Component.


                Regards
                Jeroen

                ------------------

                Comment


                • #9
                  CHDIR is needed before the shell if the .BAT file
                  is not in the current directory or the PATH from my testing.



                  ------------------
                  The world is full of apathy, but who cares?

                  Comment


                  • #10
                    Mike, that should only be necessary if the batch file contains commands that do not include an explicit path. That is, a batch file CAN be executed from another folder.

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment

                    Working...
                    X