Hottest Forum Q&A on CodeGuru - October 10th

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 to prevent keyboard hook? (top)

Thread:

DoDucTruong is developing a security application that has an input-password field. He wants to prevent other spy programs so that nobody can steal the passwords.There are several tools that can run in the background to spy the password; in other words, from a Hotmail account. So, the question is how to detect that such a tool is running in the background?

I am deverloping a security app now, my app has a
input-password form, so I need a module to prevent another
spy program stealing my password. Hence, there is a HARD
question in my mind now, that is, how can detect if there is
any spy program running in back ground which uses low level
keyboard hook to steal my password.

Here is one sample of spy program: Sample KeyLogger
and my job is to detect this kind of software, which is
currently running in the background?

The first suggestion came from puzzolino. Puzzolino suggested to register his own low-level hook before showing the input-password form, and NOT call CallNextHookEx, thus preventing ther hooks from getting notifications. Of course, you will need to release the hook when your form closes.

Although it is a good solution, there is still a problem. The sample keylogger and mostly other keylogger applications have a timer that re-hooks after some seconds. After reading the suggestion, you could use a trick to get your solution:

Register your own low level keyboard hook and and NOT call CallNextHookEx when users type in my form - as Puzzolino mentioned. Hook the API function: SetWindowsHookEx to detect if any spy app try to install keyboard hook (call SetWindowsHookEx) within the user-typing-password time. If yes, re-register the own low level keyboard hook to make sure it is in lower layer than spy app, and also let user known about the spy app (kill it or not).

Another idea from Krishnaa is to develop your own password box that does not support giving text to anybody with WM_GETTEXT. Then, have a thin keyboard filter driver that waits on EVENTs. Now, whenever the password box has the focus, you trigger the event so that the keyboard filter driver starts reading the keys. (It should read the keys but not let 'em reach to any app. I mean don't pass the keystokes onward from the filter driver.)


How to debug when using std::string? (top)

Thread:

avi123 is trying to debug a std::string. Although it is not easy to debug a,std::string, he is trying his luck.

When I debug my code containing std::string, many times I see
garbage in the std::string in the debuuger but if I call c_str
I see the correct value.

If I have std::string myString I see myString at the debugger
as garbage.

Then I do (for example)
char szMyString[100];
strcpy(szMyString,MyString.c_str());

I see szMyString just fine in the debugger that mostly happens
when I have chars like & { at the debbuger } but not only.

Any explanation?
Do I have to worry about that?

Well, std::string is a class that has its own representation for the underlying string somewhere, but the debugger doesn't necessarily know about it. So, no; you should not worrry about this "garbage."

Unfortunately, this makes debugging a bit harder, but it's rather a question of the debugger, not std::string. Also, different environments deal differently with this. For example, in my VC6 STLPort configuration, it's quite ugly (and a hack is to watch s._M_start), but in VC.NET 2002 std::strings show up correctly under the debugger. So, your mileage may vary.

An another interesting part is provided by Mick. The expansion is controlled by the autoexp.dat in your <install dir>\common\msdev98\bin.


How to decrease an iterator? (top)

Thread:

c94wjpn want to know how you get an iterator pointing to the previous thing EASILY?

There are different types of iterators: Random access iterators (for example, for std::vector or std::string), bidirectional iterators (such as std::list, std::map, std::set), or forward iterators (as in istream or ostream).

The random iterators have that thing (iter-1). However, because c94wjpn is using a map, it's not possible. So, what can be done here? What do you think?

You can express it as "--iter". std::map::iterator is a bidirectional iterator, so it has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.


How to find & remove object from a std::list? (top)

Thread:

Thresher is trying to find an object in an std::list of objects based on the name.

I'm trying to find an object in an std::list of objects, and
remove it and I was going to look for the NAME of the object as
a basis of comparison, something like this:

class myObj
{
    string name;
    int A;
    int B;
    //......
    int Z;
}

//then, later in the code....

list<myObj> myList;
list<myObj>::iterator itr;

itr = find(myList.begin(),myList.end(), /*look for myObj name */ );
if (itr != myList.end())
{
    itr = myList.erase(itr);
}

So the question is, how do I look for just the NAME using the
find command?

Philip Nicoletti gaves the first suggestion with a straightforward code example. The best thing is to use find_if instead of find because you are not checking the entire Object for equality, just part of the object. The tricky part in creating the predicate is that you want to pass it the name that you are looking for. There is a "bnd2nd()" function that does this. I think this sample code will make it clearer:

#include <list>
#include <string>
#include <functional>  // for binary_function and bnd2nd()
#include <algorithm>   // for find_if

using namespace std;

class myObj
{
public:

    string name;
    int A;
    int B;
    int Z;

};

struct CompareName : public std::binary_function
                     <myObj,std::string,bool >
{
   bool operator () (const myObj& obj, const std::string& name)
        const
   {
       return obj.name == name;
   }
};

int main()
{
    list<myObj> myList;
    list<myObj>::iterator itr;

    // add to list

    // remove FIRST occurence of object with name of "Jones"
    itr = find_if(myList.begin(),myList.end(),
                  bind2nd(CompareName(),"Jones"));
    if (itr != myList.end())
    {
        itr = myList.erase(itr);
    }

    return 0;
}

For some reason, Graham had always some problems with bind2nd, so he usually rewrites the functon so that the item to be compared against is a member variable:

struct CompareName : public std::unary_function<myObj, bool >
{
    CompareName(const std::string& n) : name(n) {}
    CompareName(const char* s) : name(s) {}

    bool operator () (const myObj& obj) const
    {
         return obj.name == name;
    }
private:
     std::string name;
};

Then you call it like this:

itr = find_if(myList.begin(),myList.end(), CompareName("Jones"));


How to check if the value of a double contains an int? (top)

Thread:

avi123 does have a big double number in which he wants to check whether or not the module's operator is 0.

I have a very big number (up to 15 digits) which is stored as a
double. I need to know if this number is integer or not, but the
% function doesn't exist for a double. I wanted to do a check
whether myDoubleNum%1 is not equal to 0.

Given that doubles cannot represent all values with absolute precision, you might find that you assign (or enter) an integer (say 15.0), but that the double represents it as 14.999999999999. The problem with this is that the fractional part may not be exactly zero if you have worked with the number a bit. This is a standard problem when handling floating point numbers and the usual solution is to treat everything smaller than a given epsilon as zero.

#include <math.h>

const double epsilon = 0.00001;  // you should adjust this as needed

bool IsInteger(double d)
{
  double intpart;
  return fabs(modf(d, &intpart)) < epsilon;
}



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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds