Solved

Replacement for GetModuleUsage

Posted on 1998-10-26
9
221 Views
Last Modified: 2013-12-03
As we know GetModuleUsage is obsolete in the Windows 32-bit API. What is a good method of determining if another application (16 or 32 bit) that was shelled is still running and will work in an application written in VB5 when running on Windows95/98 and NT?
0
Comment
Question by:EEI
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1415631
You can use WaitForSingleObject() on the shelled process's process handle.  The wait procedure will return when the process has terminated (or the time limit has expired).  Your code can use a 0 wait time for a "quick check" or can use a longer wait time (evien infinite) if it needs to actually wait.

Let me know if you have questions.
0
 

Author Comment

by:EEI
ID: 1415632
This seems that it would be a great solution, simple and effective. Except that everything hangs until the time runs out. It is just as if WaitForSingleObject needed DoEvents.
0
 

Author Comment

by:EEI
ID: 1415633

What I said in the last comment got me to thinking that I could put WaitForSingleObject into a Do Loop  with the time set to zero and watch for a return value of other than 258. That seemed to do the trick!!! I also noticed that I could do the same with GetExitCodeProcess. I wonder if there would be advantages of using one over the other?

Thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415634
Use WaitForSingleObject() over GetExitCodeProcess().  It will be more efficient.  but you don't want to keep repeating with a short wait interval (0)--that can use too much processor time needlessly.  Can you post your code?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 20

Expert Comment

by:Madshi
ID: 1415635
How about creating a thread that does nothing but call WaitForSingleObject? When returning from that call the thread could post a message to your main thread with the ExitCode as parameter and then terminate itself (simply by running out of the function).

Regards, Madshi.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415636
Madshi's suggestions is probably the most common way that this is used, but there are others depending on your needs.
0
 

Author Comment

by:EEI
ID: 1415637
Here is what I have. Launch is a good size proceedure that does a number of things (reads & writes ini files etc.) but it basically contains "Shdl=OpenProcess(&H400,0,Shell("File.exe",1)".

Dim Shdl as Long

Shdl = Launch((SetUp.GCodePath.Text))
Do
      RetVal = WaitForSingleObject(Shdl, 0)
      DoEvents: Sleep 100
Loop While RetVal = 258

If this looks ok then I think I have it.

Thank you very much for your help.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1415638
That is one way of using it.  I would make the following improvements.

Don't look for "258"  You are not guarantteed that it will be 258 on all platforms!  (but it probably will be)  Instead look for "WAIT_TIMEOUT".  i.e

while (RetVal == WAIT_TIMEOUT)

or better yet,  wait until successfull.

while (RetVal != WAIT_OBJECT_O)

either will be an improvement.  Next, if you keep this design, why not put the "sleep wait" into the WaitForSingleObject().  i.e. drop the sleep(100) and wait inside WaitForSingleObject() for 100?  The main advantage of this is that when the process ends, WaitForSingleObject will return without waiting the full 100.  If the process ends while you are in the sleep(), you will have to finish the full sleep before you can start the wait which will ends immediately.

One more thing, if "DoEvents" just involves handling messages.  Then there is a different wait() procedure you should use.  It allows you to wait and handle messages "at the same time" sorf of. If DoEvents involves lots of different things, then this is the way to go.  (Or you can go with madshi's suggestion if (a) you are writing a multithreaded application and (b) the waiting code (DoEvents)  handles mesages and could be notified by a message sent from the waiting thread.
0
 

Author Comment

by:EEI
ID: 1415639
I appreciate the timeliness and the dilligence on the part of both of you to provide a most helpful solution. Look foward to working with you.
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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

762 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

21 Experts available now in Live!

Get 1:1 Help Now