Solved

determine if a program is running (exe)

Posted on 1998-08-21
10
190 Views
Last Modified: 2010-04-30
Here is an overview:

I use vb with access.  the program monitors certain directories for new textfiles.  when the file arrives it imports it into access.  If I have access running it will usually give me an error message.

I need code to detect if Access is running.

I would prefer code that will determine if it is running  and shut it down if it is running and continue with the process.

Thanks
0
Comment
Question by:thien
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1430265
I have code, we would be happy to e-mail...
0
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
ID: 1430266
Thien,
In the form's declaration area, add:

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


When you need to check, add:
Dim lHandle as long
lHandle = FindWindow(0&, "Microsoft Access")
If lHandle<> 0 Then
   ' Access is running
   'Do something?
   End If


Regards
Dalin
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1430267
Sorry, I did not realize you wanted to kill Access idf you see it.

Use the following code:

'In the form's general Declaration area, add:

Private Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const WM_CLOSE = &H10

Dim Target As String

'Add a New function

Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long

    ' Get the window's title.
    length = GetWindowText(app_hWnd, buf, Len(buf))
    title = Left$(buf, length)

    ' See if this is the target window.
    If InStr(title, Target) <> 0 Then
        ' Kill the window.
        SendMessage app_hWnd, WM_CLOSE, 0, 0
    End If
   
    ' Continue searching.
    EnumCallback = 1
End Function

'When you want to check, add
    Target = "Microsoft Access"
    EnumWindows AddressOf EnumCallback, 0

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:thien
ID: 1430268
Dalin,

I posted the first part of the code, I have access open, but it is not working, what is wrong?

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

Private Sub Form_Load()
Private Sub Form_Load()
Dim lHandle As Long
   lHandle = FindWindow(0&, "Microsoft Access")
   If lHandle <> 0 Then
   MsgBox ("open")
   End If
   
End Sub

In this example it it does nothing if open or closed.

Thien
0
 

Author Comment

by:thien
ID: 1430269
ignore the extra Form_load!!!
Thien

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1430270
Thien,
Try:

lHandle = FindWindow(vbNullString, "Microsoft Access")

Everything else stay the same.

Let me know if you have any more problems.
Regards
Dalin
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1430271
You don't need to use EnumWindows function ans stuff like that....Why making it hard when you can do it the easy way and it is....

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

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
     lParam As Any) As Long

Private Sub Form_Load()
Dim WinHandle as long
  WinHandle = FindWindow(vbNullString, "Microsoft Access")
  If WinHandle<> 0 Then
    SendMessage IHandle, WM_CLOSE, 0, 0
  End If
end sub

Since I'm a Delphi programer I don't know about the vbNullString,but in Delphi we use nil

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1430272
You don't need to use EnumWindows function ans stuff like that....Why making it hard when you can do it the easy way and it is....

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

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
     lParam As Any) As Long

Const WM_CLOSE = &H10

Private Sub Form_Load()

Dim WinHandle as long

  WinHandle = FindWindow(vbNullString, "Microsoft Access")
  If WinHandle<> 0 Then
    SendMessage IHandle, WM_CLOSE, 0, 0
  End If

end sub

Since I'm a Delphi programer I don't know about the vbNullString,but in Delphi we use nil

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1430273
Opppss a little mistake over there...

SendMessage WinHandle, WM_CLOSE, 0, 0

this is the right way =)

Regards,
Viktor Ivanov
0
 

Author Comment

by:thien
ID: 1430274
Thank you for your answers!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

770 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