WinDBG – Visualizing STL containers and strings

With the help of STL extension, this extension can’t visualize each and every containers or classes in STL, rather it displays the mostly used string, wstring, vector<[w]string>, list<[w]string>

for more information give !stl -? in WinDBG input or check in the help file.
See the example below and the output in the debugger console.

// SampleSTL.cpp : Defines the entry point for the console application.
// Compiled with Microsoft Visual C++ 9.0 compiler

#include "stdafx.h"
#include 
#include 
#include

#include 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	string str1("Quick");
	string str2("Brown" );

	string str3 = str1 + " " + str2;

	vector vec;

	vec.push_back(str1);
	vec.push_back(str2);
	vec.push_back(str3);

	list lst;

	lst.push_back( str1 );
	lst.push_back( str2 );
	lst.push_back( str3 );

	for( vector::iterator iter = vec.begin(); iter != vec.end(); ++iter)
		cout << (*iter).c_str() << endl;

	cout <::iterator iter = lst.begin(); iter != lst.end(); ++iter)
		cout << (*iter).c_str() << endl;

	return 0;
}

WinDGB output

0:000> !stl str1
[da 0x16fb18]
0016fb18  "Quick"

0:000> !stl str2
[da 0x16faf0]
0016faf0  "Brown"

0:000> !stl str3
[da 0x16fac8]
0016fac8  "Quick Brown"

0:000> !stl vec
	Element 0
[da 0x558358]
00558358  "Quick"
	Element 1
[da 0x558378]
00558378  "Brown"
	Element 2
[da 0x558398]
00558398  "Quick Brown"

0:000> !stl lst
The list has 00000003 elements
	Element 0
[da 0x558400]
00558400  "Brown"
	Element 1
[da 0x558468]
00558468  "Quick Brown"
	Element 2
[da 0xffffffffcdcdcdcd]
cdcdcdcd  "????????????????????????????????"
cdcdcded  "????????????????????????????????"
cdcdce0d  "????????????????????????????????"
cdcdce2d  "????????????????????????????????"
cdcdce4d  "????????????????????????????????"
cdcdce6d  "????????????????????????????????"
cdcdce8d  "????????????????????????????????"
cdcdcead  "????????????????????????????????"
cdcdcecd  "????????????????????????????????"
cdcdceed  "????????????????????????????????"
cdcdcf0d  "????????????????????????????????"
cdcdcf2d  "????????????????????????????????"

C++: How to clear the input stream?

What do you expect from the following program?

char x = 0;
std::cout << "Enter a character: " << std::endl;
std::cin >> x;
std::cout << "You entered " << x << std::endl;

int y;
std::cout << "Enter a number: " << std::endl;
std::cin >> y;
std::cout << "You entered " << y << std::endl;

If you enter multiple characters for first input (cin >> x), the stream will take the first character and make it not to prompt for the next input as the input buffer contains additional data. How to deal with such a situation?

There’s a high chance to misinterpret cin.clear() which is actually supposed to sets a new value for the error control state. Standard C/C++ input streams are buffered. The keypresses are buffered by operating system, not by the program. If you’re sure that unnecessary data is present in input stream. You can make use of istream::ignore function to extract and discard the remaining data in the input stream. (Alternatively, you can recursively call cin.get() to make sure that all the data in the buffer are consumed). See the snippet below

char x = 0;
std::cout << "Enter a character: " << std::endl;
std::cin >> x;
std::cout << "You entered " << x << std::endl;

// Clear the stream
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// alternative method
// while( !cin.eof() && cin.get() == '\n' );

int y;
std::cout << "Enter a number: " << std::endl;
std::cin >> y;
std::cout << "You entered " << y << std::endl;

2010-06-29 – 4:46 PM IST – Updated Angelo’s comment

How to check mouse button status without message handler?

We can map WM_LBUTTON or WM_RBUTTON down to know whether user has pressed the left or right button respectively on top of the window or not. But sometimes, we have to know this information while we’re processing other handlers like painting/drawing the window.

#define SHIFTED 0x8000
void GetMouseButtonState()
{
   if ((GetKeyState(VK_LBUTTON) & SHIFTED))
   {
      printf( "Left button is pressed" );
   }

   if ((GetKeyState(VK_RBUTTON) & SHIFTED))
   {
      printf( "Right button is pressed" );
   }
}

Please note that this information is a system wide information. By knowing this status doesn’t mean that the button is pressed on top of your window. For that you will have to know the current cursor position using GetCursorPosition and check if it’s within the Window Rect or not.

You can see the similar keyboard sample at MSDN website. – Using Keyboard Input

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.