create a batch file from vb6

Posted on 2005-03-03
Medium Priority
Last Modified: 2012-08-13
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

Question by:xodos
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 96

Expert Comment

by:Bob Learned
ID: 13448735

Accepted Solution

abbdan earned 2000 total points
ID: 13451894
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
LVL 22

Expert Comment

ID: 13456384
>>>  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

Author Comment

ID: 13456769
NO ,  it is not a typo ,
i made the process name explorerr.exe ,

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question