Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unauthorized Applications 2

Status
Not open for further replies.

Alt255

Programmer
May 14, 1999
1,846
US
This may seem like a silly question to some of the more seasoned pros in this forum but my app needs a way to identify every app running on a 9x system and a way to &quot;terminate with judicial prejudice&quot; any unauthorized apps it finds (without shutting-down or otherwise crashing the system).<br>
What calls are made by the Windows &quot;Close Program&quot; box that allow a graceful termination of the selected application?<br>
<br>
Mike? DougP? Edderic?<br>

 
I think you will find every thing you need in here.<br>
Now at first glance you'll say I'm not using nor do I care about Excel but it shows how to get a program and control it and close it. Also look in the Knowledge base for FindWindow API calls.<br>
<br>
This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet (MyXL). It uses the worksheet's Application property to make Microsoft Excel visible, to close it, and so on. Using two API calls, the DetectExcel Sub procedure looks for Microsoft Excel, and if it is running, enters it in the Running Object Table. The first call to GetObject causes an error if Microsoft Excel isn't already running. In the example, the error causes the ExcelWasNotRunning flag to be set to True. The second call to GetObject specifies a file to open. If Microsoft Excel isn't already running, the second call starts it and returns a reference to the worksheet represented by the specified file, mytest.xls. The file must exist in the specified location; otherwise, the Visual Basic error Automation error is generated. Next the example code makes both Microsoft Excel and the window containing the specified worksheet visible. Finally, if there was no previous version of Microsoft Excel running, the code uses the Application object's Quit method to close Microsoft Excel. If the application was already running, no attempt is made to close it. The reference itself is released by setting it to Nothing.<br>
<br>
' Declare necessary API routines:<br>
Declare Function FindWindow Lib &quot;user32&quot; Alias _<br>
&quot;FindWindowA&quot; (ByVal lpClassName as String, _<br>
ByVal lpWindowName As Long) As Long<br>
<br>
Declare Function SendMessage Lib &quot;user32&quot; Alias _<br>
&quot;SendMessageA&quot; (ByVal hWnd as Long,ByVal wMsg as Long _<br>
ByVal wParam as Long _<br>
ByVal lParam As Long) As Long<br>
<br>
Sub GetExcel()<br>
Dim MyXL As Object ' Variable to hold reference<br>
' to Microsoft Excel.<br>
Dim ExcelWasNotRunning As Boolean ' Flag for final release.<br>
<br>
' Test to see if there is a copy of Microsoft Excel already running.<br>
On Error Resume Next ' Defer error trapping.<br>
' Getobject function called without the first argument returns a <br>
' reference to an instance of the application. If the application isn't<br>
' running, an error occurs.<br>
Set MyXL = Getobject(, &quot;Excel.Application&quot;)<br>
If Err.Number &lt;&gt; 0 Then ExcelWasNotRunning = True<br>
Err.Clear ' Clear Err object in case error occurred.<br>
<br>
' Check for Microsoft Excel. If Microsoft Excel is running,<br>
<br>
' enter it into the Running Object table.<br>
DetectExcel<br>
<br>
Set the object variable to reference the file you want to see.<br>
Set MyXL = Getobject(&quot;c:\vb4\MYTEST.XLS&quot;)<br>
<br>
' Show Microsoft Excel through its Application property. Then<br>
' show the actual window containing the file using the Windows<br>
' collection of the MyXL object reference.<br>
MyXL.Application.Visible = True<br>
MyXL.Parent.Windows(1).Visible = True<br>
' Do manipulations of your<br>
' file here.<br>
<br>
' ...<br>
' If this copy of Microsoft Excel was not running when you<br>
' started, close it using the Application property's Quit method.<br>
' Note that when you try to quit Microsoft Excel, the<br>
' title bar blinks and a message is displayed asking if you<br>
' want to save any loaded files.<br>
If ExcelWasNotRunning = True Then <br>
MyXL.Application.Quit<br>
End IF<br>
<br>
Set MyXL = Nothing ' Release reference to the<br>
' application and spreadsheet.<br>
End Sub<br>
<br>
Sub DetectExcel()<br>
<br>
' Procedure dectects a running Excel and registers it.<br>
Const WM_USER = 1024<br>
Dim hWnd As Long<br>
' If Excel is running this API call returns its handle.<br>
hWnd = FindWindow(&quot;XLMAIN&quot;, 0)<br>
If hWnd = 0 Then ' 0 means Excel not running.<br>
Exit Sub<br>
Else <br>
' Excel is running so use the SendMessage API <br>
' function to enter it in the Running Object Table.<br>
SendMessage hWnd, WM_USER + 18, 0, 0<br>
End If<br>
End Sub
 
'Add a module to your project (In the menu choose Project -&gt; Add Module, Then click Open)<br>
'Add 1 List Box and 1 Command Button to your form.<br>
'Insert this code to the module :<br>
<br>
Public Const TH32CS_SNAPPROCESS As Long = 2&<br>
Public Const MAX_PATH As Integer = 260<br>
Public Type PROCESSENTRY32<br>
dwSize As Long<br>
cntUsage As Long<br>
th32ProcessID As Long<br>
th32DefaultHeapID As Long<br>
th32ModuleID As Long<br>
cntThreads As Long<br>
th32ParentProcessID As Long<br>
pcPriClassBase As Long<br>
dwFlags As Long<br>
szExeFile As String * MAX_PATH<br>
End Type<br>
Public Declare Function CreateToolhelpSnapshot Lib &quot;Kernel32&quot; Alias _<br>
&quot;CreateToolhelp32Snapshot&quot; (ByVal lFlags As Long, ByVal lProcessID As Long) As Long<br>
Public Declare Function ProcessFirst Lib &quot;Kernel32&quot; Alias &quot;Process32First&quot; _<br>
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long<br>
Public Declare Function ProcessNext Lib &quot;Kernel32&quot; Alias &quot;Process32Next&quot; _<br>
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long<br>
Public Declare Sub CloseHandle Lib &quot;Kernel32&quot; (ByVal hPass As Long)<br>
<br>
'Insert the following code to your form:<br>
<br>
Private Sub Command1_Click()<br>
Dim hSnapShot As Long<br>
Dim uProcess As PROCESSENTRY32<br>
Dim r As Long<br>
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)<br>
If hSnapShot = 0 Then<br>
Exit Sub<br>
End If<br>
uProcess.dwSize = Len(uProcess)<br>
r = ProcessFirst(hSnapShot, uProcess)<br>
Do While r<br>
List1.AddItem uProcess.szExeFile<br>
r = ProcessNext(hSnapShot, uProcess)<br>
Loop<br>
Call CloseHandle(hSnapShot)<br>
End Sub<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
Thanks for the samples, guys. I think I can integrate the logic in my project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top