|
-
Aug 12th, 2010, 10:54 AM
#1
Thread Starter
Member
[Help] Series of Question about Running 3rd Party Applications
Hi to all...
I am making a personal project for my self. Its been a very long time since i last touch VB6 so I mostly forgot most of the codes that I have learned. Now I am having problems in my current project...
I hope someone can help me here...
My program will be used to run 7 specific applications and hide it from the taskbar.
I am using shellexecute API to run this 7 applications, set its starting directory and hide it from the taskbar...
My problem now comes from here... After I run the program and Hide it from the taskbar, I wanted to show it up again.. I know I can do this using the ShowWindow API but I cannot do it without the handle of the applications that I hid...
This are my problems at the moment.
1. Run the individual applications, set it starting directory and hide it.
(this is attainable by using the shellexecute API)
2. Show the hidden application whenever needed.
(I know it can be done by ShowWindow if I have the handle of the application)
3. Set the window handle of the application after it execute using the shellexecute API into a variable, - for hiding and showing purpose -
(This is the one thing that I dont know)
4. Before running the next application check if the executed application is running and ready.
(This is my biggest problem)
5. Close the application whenever needed.
(I found that using SendMessage API can be used to achieve this if I have the handle of course)
6. Save and Retrieve settings from&to an INI file on the application path.
(Done with GetPrivateProfileString and WritePrivateProfileString API)
7. [minor problem] Deleting a shortcut file from Stat Menu\Programs\Startup.
(this is my minor problem... I cant figure out how to do this on VB6. I know already how to make a shortcut but not deleting)
Text in red are my problems.
I guess those are my current problems that I have to solve first...
I hope someone kind enough to give me a hand here...
-
Aug 12th, 2010, 01:48 PM
#2
Re: [Help] Series of Question about Running 3rd Party Applications
Not all applications have a window, but if it does, then this routine could help.
Regarding whether an application is running and ready? You'll have to describe exactly what "ready" means. If the function below doesn't return an hWnd then one of 4 things happened
1. app did not start
2. app started but closed itself quickly
3. app didn't create a window within the hardcoded timeout in the routine
4. app doesn't have any hWnds assigned to its thread
Code:
' must be placed in a module not a form or class
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByRef lpProcessAttributes As Any, ByRef lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByRef lpEnvironment As Any, ByVal lpCurrentDriectory As String, ByRef lpStartupInfo As STARTUPINFO, ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByRef lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type STARTUPINFO
cbSize As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Function hWndFromShellExec(ByVal CommandLine As String, _
Optional ByVal ShowHide As Long = 1, _
Optional ByVal WorkingDir As String = vbNullString, _
Optional ByRef ProcessHandleOut As Long) As Long
Dim siStartupInfo As STARTUPINFO
Dim ProcessInfo As PROCESS_INFORMATION
Dim lPid As Long
Const STARTF_USESHOWWINDOW = &H1
Const NORMAL_PRIORITY_CLASS As Long = &H20
With siStartupInfo
.cbSize = LenB(siStartupInfo)
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = ShowHide
End With
If CreateProcess(vbNullString, _
CommandLine, _
ByVal 0, _
ByVal 0, _
False, _
NORMAL_PRIORITY_CLASS, _
ByVal 0, _
WorkingDir, _
siStartupInfo, _
ProcessInfo) = 0 Then
Else
ProcessHandleOut = ProcessInfo.hProcess
lPid = ProcessInfo.dwProcessId
Call WaitForSingleObject(ProcessHandleOut, 500&) ' 500=nr ms to wait before returning
Call CloseHandle(ProcessInfo.hThread)
Call CloseHandle(ProcessInfo.hProcess)
Call EnumWindows(AddressOf EnumWindowsProc, lPid)
If lPid <> ProcessInfo.dwProcessId Then hWndFromShellExec = lPid
End If
End Function
Private Function EnumWindowsProc(ByVal hWnd As Long, ByRef lParam As Long) As Long
Dim lPid As Long
Call GetWindowThreadProcessId(hWnd, lPid)
If lPid = lParam Then
lParam = hWnd
Else
EnumWindowsProc = 1
End If
End Function
Last edited by LaVolpe; Aug 16th, 2010 at 09:25 AM.
Reason: typo
-
Aug 14th, 2010, 01:57 PM
#3
Thread Starter
Member
Re: [Help] Series of Question about Running 3rd Party Applications
Wow.... Thanks for the code.... I tested it and it works like a charm...
But... I dont quite get the code... could you please explain how this code works... so I wont forget it again.... I wanna know what each line means and how they work and what values they return....
My only problem now is how to check if the program is finished loading(or finished initializing) before running another application...
-
Aug 16th, 2010, 09:18 AM
#4
Re: [Help] Series of Question about Running 3rd Party Applications
To try to answer your questions.
1) For an explanation of the APIs used, their return values and parameter requirements, open MSDN.com and enter the API in the search engine. I'm not going to take the time to reiterate what is already published by MSDN.
2) If you need to wait for the application to finish, that is a different scenario and is a bit confusing; one of the following scenarios may apply? If it needs to finish, why do you need to send it a Close command because it will close by itself when finished?
a) If target needs to finish and you will be doing nothing else with your app until target finishes, change the timeout value from 500 in the posted example to -1. Your app will wait until the other app closes. You should also do away with the EnumWindows stuff
b) If target needs to run while your app also needs to do other stuff until target app is finished (i.e., don't wait on it closing), then you'll need to periodically (timer) search for the window handle to see if it exists or not (closed). The timer can use the IsWindow or GetWindowThreadProcessId APIs for this purpose.
Last edited by LaVolpe; Aug 16th, 2010 at 09:27 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|