Hottest Forum Q&A on CodeGuru for the week of April 4th, 2004

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


How do I find whether or not a process is running? (top)  

Thread:

ashwini73 is working with CreateProcess() to start to a new process. Now he needs to know whether or not the process is still running! Do you know how to do that? Take a look at this question:

In my MFC Aplication I have started a new process using the
"Create Process" function. The process starts up and runs, but how
do I determine whether that process is still running?

I came across the function:
BOOL GetProcessTimes(
HANDLE hProcess,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);
In above function's arguments, "lpExitTime" is "Pointer to a FILETIME
structure that receives the exit time of the process." But MSDN
library also says that if the process has not exited, the content
of this structure is undefined.

So, I am stuck. Any help?

You can use GetExitCodeProcess to find the state of a process if you do not want to wait for the process to end but just check up on it. It will give you a STILL_ACTIVE notifier if the process is still running. Something like this:

DWORD dwExitCode = 0;

do
{
   ::Sleep(100);

   if(!::GetExitCodeProcess(hHandle, &dwExitCode))
      // Error -> call 'GetLastError()'
}
while(dwExitCode == STILL_ACTIVE)

// Process has terminated
// 'dwExitCode' contains the exit code
// 'hHandle' is returned within the process information structure
// which is passed to 'CreateProcess()'

But what if I need more information ? Isn't there any possiblity that the process can tell me if the thread is currently running, or is in a suspended state ? Well, for that purpose you can use the native api. Take a look at the NtQuerySystemInformation function. This may help you to determine the status.

Besides that, you also should look at the CodeGuru Visual C++ FAQ:


How can I convert binary numbers to decimal or to char? (top)  

Thread:

bladernr has a file with binary numbers. Now, he needs to convert them to decimal or to a char.

can anyone help me out with a bit of a conversion problem? I can't
seem to find a way to successfully do this, so hopefully someone
here can point me in the right direction.

Basically, I have a file that has a list of binary numbers, like
this:
01001001
00100000
01110011
01101000
01101111
01110000
And I have this code so far:
#include <iostream>
#include <string.h>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
   string binstring;
   ifstream binList;
   int bytecount = 0;
   binList.open("binary.txt");
   if (binList) {
      cout<<"File opened successfully."<<endl;
   }
   if (!binList) {
      cerr<<"Unable to open the file."<<endl;
      exit(1);
   }
   //start reading data and act on it
   binList>>binstring;
   bytecount++;
   cout<<bytecount<<" "<<binstring<<endl;
   while (binList) {
      binList>>binstring;
      bytecount++;
      cout<<bytecount<<" "<<binstring<<endl;
      cout<<endl;
   }
   binList.close();
   cout<<"Finished"<<endl;
   return 0;
}
Now, of course, right now this little bit of code does nothing
useful. It just opens the file, reads in a line, and prints it out.

What I want to accomplish is to take each line (in the string form
01001010) and convert that to its ascii character.

So if the program reads this, 01000001, it outputs ascii char 65, or
'a'.

Any suggestions? The hard part is the conversion of the string
binstring to an integer or hex that == the actual binary value of
01000001 and not the value of the char string 01000001 (if that
makes any sense).

Any suggestions?

Using std::bitset will help you to solve your problem in an elegant way. Something like this:

#include <iostream>
#include <fstream>
#include <bitset>

int main()
{
   std::ifstream InFile("binary.txt");
   std::bitset<8> Byte;

   if(!InFile)
   {
      std::cerr << "Unable to open the file." << std::endl;
      return 1;
   }

   while(InFile >> Byte)
   {
      std::cout << Byte << ": " << 
      static_cast<char>(Byte.to_ulong()) << std::endl;
   }

   system("pause");
   return 0;
}

Also, change the #include <string.h> to #include <string>.If you need more information about the STL and vectors, you should take a look at the following articles and resource:


How do I read a window's text from an another window without using the mouse? (top)  

Thread:

mce want to read the text or controls text from another application. Is that possible?

I need to read text or control's text from another application's
windows. My application is operating from background (hidden), and
tries to read other application's window text. Also, there is no
mouse used, so I believe I can't use mouse hook or SetCapture API...
etc for this matter.

So, I guess I need to capture the other windows' application
control's text based on some supplied screen coordinates.

In this case, how do I read get the window's handle based on the
coordinates? And subsequently obtain the text? Assuming that I know
the other application's name.

It depends on the type of window, but you should be able to use Spy++ to find out about the window and window structure used in that application. Then, you can use FindWindow to find the window and child windows until you get to the window containing the text you want. Then, you can use SendMessage and the HWND returned from FindWindow to get the text. Besides that, you can also use the WindowFromPoint and ChildWindowFromPoint functions.

Also, if you know the ClassName and WindowName of the main application window and the control is a direct child of the main application window, you could access the control by using its Control ID, etc. You also could use EnumChildWindows to try and find the window that contains the control yourself. But, as already mentioned, it always depends on the target application. Take a look at the thread to get more information about this intersting topic.


Why does lower_bound() not work for me? (top)  

Thread:

waverdr9 is working with the lower_bound function, but unfortunately it does not work as excepted. Take a look.

I am currently working on a simulation program that uses discrete
event simulation. Basically, the program generates events (in this
case, the arrival and departure of customers in a store). So, the
events are placed in a priority queue, ordered by the times that the
events are to occur (i.e. an event that is supposed to occur at time
25 would have a higher priority than an event that is supposed to
occur at time 30). To make this ordering easy, I decided to use the
vector STL to implement the queue.

To insert things in the order I want, I use the <algorithm>
function, lower_bound(line.begin(), line.end(), e), where line is
the vector, and Event* e is inserted in the appropriate location,
based on ascending event time order. I have used lower_bound()
before, and for it to work, I had to overload the == and <
operators for the object (in this case, Event). However, when I
run my program with the debugger, the overloaded == and < are
never accessed and, consequently, the events are not ordered.
Here are the relevant sections of code.

In Simulation class:
void Simulation::schedule(Event *e) {
   //Create an iterator for the vector queue
   vector<Event *>::iterator pos;
   //Determine the position to insert the event
   pos = lower_bound(eQueue.begin(), eQueue.end(), e);
   //Insert the event at the determined position
   eQueue.insert(pos, e);
}
In Event class:
bool Event:operator==(const Event& rhs) const {
   //Return true if the event times are equal; false otherwise
   return (time == rhs.time);
}
bool Event:operator<(const Event& rhs) const {
   //Return true if this event time is less than rhs event time;
   //false otherwise
   return (time < rhs.time);
}
Do you have any idea why the lower_bound() function isn't looking
for those two operators? Can you think of a better way to do this?

A better suggestion would be to use the std::priority_queue because it already implements what you need. But, because you can not use the std::priority, here is what you need to do: The problem why your operaters are not called is that you have a container of pointers. You should try this instead (code and information provided by Souldog):

struct PtrEventInTimeCompare:public std::binary_function<const
Event*, const Event*, bool>
{
   bool operator()(const Event* lhs, const Event* rhs) const
   {
      return *lhs.time < *rhs.time;
      //or whatever accessor you have
   }
}

//and your code becomes
eQueue.insert(lower_bound(eQueue.begin(), eQueue.end(), e, 
PtrEventInTimeCompare()), e);

And where does he need to put this code? Do you know? Take a look at the thread and you will get the answer.




About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds