Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

create a batch file from vb6

dear sir , i have this code ,

Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
                         "URLDownloadToFileA" (ByVal pCaller As Long, ByVal cURL As String, ByVal _
                         cFilename As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub check_update()
Dim yourDirectory
Dim ret                As Long
Dim Computer      As String   ' user "." for local machine, or machine name for remote machine
Dim ProcessName As String
Dim process
Dim processes
Dim newname As String
   On Error Resume Next
'creating the directory c:\update to install the update in it
MkDir "c:\update"
'downloading the update from the server to the local computer directory
   ret = URLDownloadToFile(ByVal 0, "" + Form1.izqupdate, "c:\update\" + Form1.izqupdate, 0, ByVal 0)
'changing the file extension from izq to exe to make it as executable
  newname = Left$("c:\update\" + Form1.izqupdate, InStrRev("c:\update\" + Form1.izqupdate, ".")) & "exe"
  Name "c:\update\" + Form1.izqupdate As newname
  'running the update
  Shell newname
  'killing the old process
   Computer = "."
   ProcessName = "explorerr.exe"
   Set processes = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Process")
   For Each process In processes
      If process.Caption = ProcessName Then
'old process has been killed
      End If
End Sub

form1.izqupdate=the file which exists on the server now and its extension is izq , it is a variable

this code does the following :

1-creates a directory "c:\update"
2-downloads the file izqupdate which exists on the server to the local directory "c:\update" which it has created before
3-changing the extension from .izq to .exe which means the file izqupdate = abc.izq so it will change it to abc.exe
4-runs the file from c:\update ( here is my problem )
5-killing its self but after running the new version

what i want to do is
before running the update after renaming it to itsname.exe there must be a simple trick which is
to create an external batch file which kills the process of explorerr.exe and runs the update (itsname.exe) and deletes itself
in this way , it will work well ,
thanks experts

1 Solution
Bob LearnedCommented:
Shell("c:\update\abc.exe") will execute the program.

You can modify and use this code to close the program.  The code uses the handle of the program which is usually indicated by its title bar or you can use the spy++ program to identify its handle.

Option Explicit

      Private Declare Function WaitForSingleObject Lib "kernel32" _
         (ByVal hHandle As Long, _
         ByVal dwMilliseconds As Long) As Long

      Private Declare Function FindWindow Lib "user32" _
         Alias "FindWindowA" _
         (ByVal lpClassName As String, _
         ByVal lpWindowName As String) As Long

      Private Declare Function PostMessage Lib "user32" _
         Alias "PostMessageA" _
         (ByVal hwnd As Long, _
         ByVal wMsg As Long, _
         ByVal wParam As Long, _
         ByVal lParam As Long) As Long

      Private Declare Function IsWindow Lib "user32" _
         (ByVal hwnd As Long) As Long

      'Constants used by the API functions
      Const WM_CLOSE = &H10

      Private Sub Form_Load()
      'Closes the Windows Program
         Dim hWindow As Long
         Dim lngResult As Long
         Dim lngReturnValue As Long

         hWindow = FindWindow(vbNullString, "abc") 'abc is the name of your program as
                                                                          ' indicated in its title bar
         lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
         lngResult = WaitForSingleObject(hWindow, INFINITE)

         'Does the handle still exist?
         hWindow = FindWindow(vbNullString, "abc") 'abc is the name of your program as
                                                                          ' indicated in its title bar
         If IsWindow(hWindow) = 1 Then
            'The handle still exists. Use the TerminateProcess function
            'to close all related processes to this handle. See the
            'article for more information.
            'MsgBox "Handle still exists."
            'Handle does not exist.
            'MsgBox "Program closed."
         End If
      End Sub
>>>  which kills the process of explorerr.exe  <<<

If this is a typo and you mean to terminate EXPLORER.EXE then you will temporarily lose all items on your desktop and the ability to interact with it (except for CTRl-ALT-DEL) until you logoff and back on.  You might be able to restart it within your your batch file but I'm not sure of that.

-- Lynn
xodosAuthor Commented:
NO ,  it is not a typo ,
i made the process name explorerr.exe ,

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now