Hottest Forum Q&A on CodeGuru for the Week of April 18th, 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:


What is the difference between ASSERT and VERIFY? (top)  

Thread:

MikeB, a senior member of CodeGuru, needs to know the difference between ASSERT and VERIFY.

What is the difference between VERIFY and ASSERT?
Which should I use, and why not the other?

The ASSERT expression is used only in the debug version of your application. It does not work in release mode. The VERIFY expression can be used in both modes, but it does not check the result in release mode. Let's consider the following example:

ASSERT(pPointer = pPassedPointer);

Here, we are checking pPointer to determine whether or not it is non-zero. For debug builds, this will be work, but in release mode, the pPointer = pPassedPointer is not compiled at all. So, further down in your code if you have something relying on pPointer, you see that it is not valid because the statement within ASSERT did not get executed because it was not even a statement during compile time. Thanks to Kirants, who supplied the great reply.

Besides that, you should look at the following articles:


How do I implement a popup menu that opens on a mousemove when the cursor is on a specific region of a client window? (top)  

Thread:

Emster needs to implement the following:

I am trying to implement a popup menu that opens on a
mousemove message rather than a right mouse click. When the
cursor is in a particular region of the client window, the
popup menu should be displayed. If the mouse moves to a
different region, the original popup menu should 'disappear'
and a modified menu should appear that is specific to that region.

I am finding this quite difficult to get working. Does anyone
have any ideas for me? This has been my approach so far:

In the OnMouseMove handler, I check the cursor coordinates and
create/load the popup menu specific to the region. Because I am
using TrackPopupMenu to display the popup menu, no other mouse
move messages were getting through until I closed that menu or
select an item from it.

My attempt at a solution (after searching on CodeGuru) was to use
a thread specific hook to catch the mousemove message. To do this,
I used:
SetWindowsHookEx(WH_MOUSE, MouseHook, NULL, GetCurrentThreadId());
The MouseHook function is declared as:
LRESULT CALLBACK MouseHook(int nCode, WPARAM wParam,
                           LPARAM lParam)
In this function I check if it is a mousemove message and then
call the same code to check the region and create a new popup menu.

My problem is that the popup menu will display the first time and then
will remain on the screen until I close it or select an item from it.
If I step into the code, the MouseHook function is being called and
sometimes fires the code to create a new menu, but it doesn't seem to
work. I am not sure if my problem lies in how I am implementing the
creation of the menu or how I am trying to create and use the windows
hook.

Sorry for the lengthy explanation, but can anyone shed some light on
this problem? Or perhaps suggest a better way to achieve the same
result?

Well, the problem is that you need to dismiss the first open menu before you can pop up the next one. There are different ways to dismiss the menu:

Now, are you wondering whether or not this worked? Take a look at the thread and you will know the answer.


Can I use WaitForSingleObject on windows rather than processes? (top)  

Thread:

drm15 wants to use WaitForSingleObject on Windows. Do you know whether or not that is possible?

Does anyone know if you can use WaitForSingleObject on a window?
I have a handle to the window (HWND) and I want to know when the
user closes the window. WaitForSingleObject seems to only work on
processes and not on windows.

The only solution if you only have a window handle would be to use GetWindowThreadProcessId. This will get the thread identifier and process identifier. After that, you can use the retrieved value to terminate the process. Something like this:

DWORD dwProcessID = 0;
DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
if(0 != dwThreadID)
{
   HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
   if(NULL != hProcess)
   {
      while(::IsWindow(hWnd) 
)
      {
         ::Sleep(200);
      }
      ::TerminateProcess(hProcess, 0);
      ::CloseHandle(hProcess);
   }
}


Why does this constructor-usage not compile? (top)  

Thread:

highhead is working on a simple structure, but unfortunaly it does not work correctly. Please take a look.

Assume a simple class like this (string-header has been included
and "using namespace std" has been used):
class someclass
{
    private:
      string m_string;

    public:
      someclass(string& str){m_string = str;}
};
now I want to use this class in a programm like this:
someclass* cinst = new someclass(string("test"));
But, this doesn't compile without error. Have you got any idea why?
The compiler-error occures at the line in the program where I want
to instantiate "someclass."

If I use the following everything works fine:
string str       = "test";
someclass* cinst = new someclass(str);

The above code compiles without any error on VC6, but gives an error on Borland and Comeau. So what can be the problem? The problem is that your constructor (string &str) would take a reference of a non-constant temporary and hence it's disallowed. You don't use that reference later, but it's still wrong. Change the your constructor to the following and it should work:

someclass(const string& str){m_string = str;}

As a side note: You should not specify using namespace std in the header file. Thanks to YvesM for his support.




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

  • how to create a form dynamically

    Posted by raviraj on 04/27/2004 10:51am

    sdfdsfdfdfdf

    • Re:

      Posted by Sonu Kapoor on 04/28/2004 04:54pm

      What do you mean ? sonu

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds