Calling Instance Methods in WMI

It’s pretty rare that I wind up using WMI from a C++ application, but when I’ve done so in the past, it’s been a straightforward process. The documentation in MSDN is generally adequate, and their example projects tend to lead me in the right direction. However, I was helping a friend out with a side project and I needed to call an instance method of a WMI object. It turns out that MSDN’s documentation on the subject is sorely lacking, and their example product is misleading. They focus only on the easiest possible case — how to call a static method on an object.
Continue reading

Posted in Win32 | Tagged | 3 Comments

Reconstructing a Corrupted Stack Crawl

For my day job, I frequently look at reports that come out of WinQual from Microsoft. These reports contain crash dumps that I can use to determine what’s going wrong with the software I’ve been working on. All in all, it’s a fantastic system and I highly recommend any ISV sign up (especially considering that it’s free for anyone with properly signed executables). Recently, I received a crash dump which had an corrupted stack crawl. I wanted to cover how you can use WinDbg to reconstruct a corrupted stack crawl.
Continue reading

Posted in Win32 | Tagged , , | 4 Comments

Move Semantics

One of the new features in C++0x is a way to express move semantics. This is a sensible piece of sibling functionality to copy semantics, which you’ve likely already run into. When writing copy semantics for a class, the idea is to take data from the right-hand object and copy it into the left-hand object. You’ve seen this in many forms, such as a copy constructor, assignment operator or even a Clone method. Move semantics are similar, except instead of copying the data from the right-hand object, you move it from the right-hand object into the left-hand object.
Continue reading

Posted in C/C++ | Tagged , | Leave a comment

The Things You Spot During Code Reviews

I was doing an informal code review the other day, and I ran into some code that I thought would make a fascinating blog post. It had to do with a pure virtual destructor declaration, followed by the same destructor’s definition.
Continue reading

Posted in C/C++ | Tagged , | Leave a comment

Hiding in Plain Sight

Sometimes, the hardest bugs to find are the ones that hide in plain sight. They’re the sort of bug where your eyes skim over the offending code and your brain continuously says “yup, fine, right, good, yup” and you can’t spot the problem.

The other night I was working on some “fun code”, and ran into one of these bugs. While I’m happy to admit it didn’t cause me days of anguish, I still figured it was a good example to share with others as a simple warning: when debugging code, you must employ critical thinking skills!
Continue reading

Posted in C/C++ | Tagged , , , | Leave a comment

Remote Thread Injection on Windows

I happened to have a legitimate case where I needed to inject a thread into another process, and in the process of solving this problem I realized there’s very little accurate information on the topic of remote thread injection available on the web. Most of the information out there on sites like Code Project and various “hacker” websites is riddled with antiquated information, or incorrect assumptions. This post is going to correct a lot of that information.
Continue reading

Posted in Win32 | Tagged , | 2 Comments

Function Pointer Basics

At their core, functions are nothing more than a location in memory with some special semantics attached to them. The assumption is that the location in memory is the start of some machine code to be executed, and that there are conventions to follow when jumping to that address. But at the end of the day, functions are just locations in memory. As such, they can be viewed in one of two ways: as an entity which executes instructions to manipulate some program state, or as data. I want to discuss the times when we view functions as data.
Continue reading

Posted in C/C++ | Tagged | Leave a comment

Allocations and Exceptions

One of the things I dislike about many programming languages are exceptions. They go against the natural flow of thinking for most programmers. We tend to think of code as flowing in one direction only: forward. But with exceptions, code can flow in two directions. When conditions are normal, code continues to flow forward. But when conditions are exceptional, code starts to flow backwards as the stack unwinds. Much like with multithreaded code, this makes code safety considerably more difficult. One of the exception-based gotchas I see over and over has to do with allocations, which is what I’d like to cover today.
Continue reading

Posted in C/C++ | Tagged , , | Leave a comment

When Should You Use const_cast?

C++ provides an explicit casting mechanism called const_cast, and yet the question pops up: when would I ever use this? You can always assign a non-const value to a const value without requiring a cast operation. So you don’t need to use const_cast to *add* constness to a value. It must mean you use const_cast to remove constness from a value! Or not…
Continue reading

Posted in C/C++ | Tagged , , | 7 Comments

When Const isn’t Const, it’s Mutable

I’m a programmer who strives to write const-correct code. I’ll admit that it can be challenging, but once you get used to doing it, your code is generally cleaner and more maintainable. This happens at the expense of flexibility. When you say “this is a const class method”, you are no longer able to mutate the underlying object. This is a good thing, of course, because the caller may be using a const object which cannot be modified! But what if you really do want to mutate the underlying object?
Continue reading

Posted in C/C++ | Tagged , | Leave a comment