Solved

determine if a program is running (exe)

Posted on 1998-08-21
10
185 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
Comment Utility
I have code, we would be happy to e-mail...
0
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
Comment Utility
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
Comment Utility
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
 

Author Comment

by:thien
Comment Utility
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
Comment Utility
ignore the extra Form_load!!!
Thien

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:Dalin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your answers!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now