borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Getting Hardware Info)Next Topic (A useful tip for controls) New Topic New Poll Post Reply
AndreaVB Forum : Frequently Asked Questions : Close Exe program
Poster Message
shahidmojid
Level: Professor

Registered: 09-05-2002
Posts: 85

icon Close Exe program

How can I close an application from VB-6?

For example: there is a program running in the system name: Reader.exe
What command shall I give in order to close that exe file?

12-03-2003 at 11:02 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Close Exe program

This could be of help: http://www.andreavb.com/tip020021.html

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

13-03-2003 at 08:21 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
shahidmojid
Level: Professor

Registered: 09-05-2002
Posts: 85
icon Re: Close Exe program

Thans JL. I will give a try with that command.

By the way, is there any other command other than API? For example, i can run a exe file by Shell command. But how can i close that exe file from VB other than API command?

13-03-2003 at 09:37 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Close Exe program

The only 100% way to make sure is with an API. Any other method would be really glitchy and potentially shutdown programs that you don't want to close.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

13-03-2003 at 10:02 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
shahidmojid
Level: Professor

Registered: 09-05-2002
Posts: 85
icon Re: Close Exe program

Well, i am not an API expert. I just copied your code and inserted into my module.bas. But unfortunately, it detects the program but doesnt close.

sub main ()
mainprogram.show 1
..
..

Call KillProcess("datareader.exe")

Shell App.Path & "datareader.exe", vbNormalFocus  'run the program.

end sub

but it doesnt kill the open 'datareader.exe' program. What i have done mistake?

Second: In VB5 when i was working with MSWORD/Excel then i was able to check the program was running or not. If running then just open the file else run exe file.... But i forgot the command. Your API command seems great and traced all program names. But why it doesnt kill?

13-03-2003 at 10:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Close Exe program

Hopefully you ran the program first, then killed it. You posted that you killed the process first, then opened it.

Check the routine as it's running to see if it actually hits the TerminateProcess line, if not, have it display the names that it's checking, the program's process name may be different.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

13-03-2003 at 11:12 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
shahidmojid
Level: Professor

Registered: 09-05-2002
Posts: 85
icon Re: Close Exe program

Hi JL,

It hits the TerminateProcess line. I debug the command i checked every step by step. It founds datareader.exe then hits TerminateProcess line. But datareader.exe doesnt close.

found as follows:
SzExename = "datareader.exe"
NameProcess = "datareader.exe"
PROCESS_ALL_ACCESS = 0
uProcess.th32ProcessID = 1964
MyProcess = 0  'seems here might be the problem
then hits terminateprocess line but datareader.exe still on.  

The reason i killed first then opened because -> every time i exit from my main program it runs datareader. First time this program works fine.

2nd time when i want to start my main prgram then it should check if datareader.exe running then close the datareader.exe and use mainprogram datareaderform.

Any Help?

13-03-2003 at 11:31 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Close Exe program

If you know the program's caption (title in title bar):


' I quickly modified code for here, but it should work

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, lParam As Any) As Long

Const WM_CLOSE = &H10

Private Sub Form_Load()
    Dim WinWnd As Long

    WinWnd = FindWindow(vbNullString, "Title Of Window")

    If WinWnd <> 0 Then
        PostMessage WinWnd, WM_CLOSE, 0&, 0&
    Else
        MsgBox "No window of that name exists."
    End If
End Sub


[Edited by JLRodgers on 13-03-2003 at 06:58 PM GMT]

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

14-03-2003 at 12:58 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
shahidmojid
Level: Professor

Registered: 09-05-2002
Posts: 85
icon Re: Close Exe program

This works great. Thank a lot JL.

14-03-2003 at 02:07 PM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Close Exe program

quote:
JLRodgers wrote:
If you know the program's caption (title in title bar):


' I quickly modified code for here, but it should work

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, lParam As Any) As Long

Const WM_CLOSE = &H10

Private Sub Form_Load()
    Dim WinWnd As Long

    WinWnd = FindWindow(vbNullString, "Title Of Window")

    If WinWnd <> 0 Then
        PostMessage WinWnd, WM_CLOSE, 0&, 0&
    Else
        MsgBox "No window of that name exists."
    End If
End Sub


[Edited by JLRodgers on 13-03-2003 at 06:58 PM GMT]


Out of curiosity, and I guess it will clear the air for a number of people. I always use the SendMessage API when dealing with messages. I always thought PostMessage was an alias for SendMessage or vice versa because they have exaclty the same signature but have noticed in a number of code samples that they are actually 2 different entries in the "user32" library.

What is the difference (if any between these 2) and is there any reason/advantage of using one over the other. Thanx.


____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer
28-01-2004 at 02:06 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Close Exe program

Basic difference between those API's is that SendMessage API send message to another windows procedure and waits for it to return, while PostMessage API queues message in message queue and returns immediately. I will put an example for you to understand it better.

Let's say you are setting some property using SendMessage it will set it, and after its done it will continue with executing your code. If your next line in code is reading this property, it will return new value.

Now, if you change this property with PostMessage it will return immediately, queuing this message. So, if you try to get the value of this property, it will not return its new value, but old one.

So, you would use PostMessage when its execution is not urgent (like some notifications) or when you want to wait for the current proccessing to finish before proccessing this one.

P.S. I almost forgot that there is a FAQ section.      

____________________________
If you find the answer helpful, please mark this topic as solved.

09-02-2004 at 12:18 AM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: Close Exe program

Thanx Goran.

That was very clear and now I have an understanding of the differences. I can now make an informed decision of what API to use in such circumstances. Thank you again.     

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

09-02-2004 at 07:25 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : Frequently Asked Questions : Close Exe program
Previous Topic (Getting Hardware Info)Next Topic (A useful tip for controls) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder