Showing tag results for Code

Jan 22, 2009
Post comments count0
Post likes count1

Why do I get the error REGDB_E_IIDNOTREG when I call a method that returns an interface?

Raymond Chen

This is another manifestation of the missing marshaller problem. IContextMenu *pcm; HRESULT hr = psf->GetUIObjectOf(hwnd, 1, &pidl, IID_IContextMenu, NULL, &pcm); // fails with REGDB_E_IIDNOTREG The IContextMenu interface does not have a proxy/stub factory (as of this writing). Recall that shell objects, as a rule, a...

Code
Jan 21, 2009
Post comments count0
Post likes count1

If you have full trust, then you can do anything, so don’t be surprised that you can do bad things, too

Raymond Chen

This is another example of the dubious security vulnerability known as wrapping a simple idea inside layers of obfuscation and then thinking that somehow the obfuscation is the source of the problem. First of all, consider this: Suppose a program calls one of its own functions but gets the calling convention wrong and ends up corrupting its sta...

Code
Jan 12, 2009
Post comments count0
Post likes count1

How does PostQuitMessage know which thread to post the quit message to?

Raymond Chen

Commenter bav016 asks how functions like and know which thread the messages should go to. Unlike some functions such as which have a window handle parameter that lets you say which window you want to operate on, and don't say which thread the or message should go to. How do they decide? The messages go to the current thread; that is, they...

Code
Jan 7, 2009
Post comments count0
Post likes count1

When debugging a stack overflow, you want to focus on the repeating recursive part

Raymond Chen

When your program breaks into the debugger with a stack overflow, you will get a ridiculously huge stack trace because your program has gone into some sort of recursive death. (This is not a statement of metaphysical certitude, but it is true with very high probability.) But the place where the program crashed is usually not interesting at all. He...

Code
Jan 5, 2009
Post comments count0
Post likes count1

Even if you have code to handle a message, you’re allowed to call DefWindowProc, because you were doing that anyway after all

Raymond Chen

Just because you write case WM_SOMETHING: doesn't mean that you have to handle all possible parameters for the WM_SOMETHING message. You're still allowed to call the DefWindowProc function. After all, that's what you did when you didn't have a case WM_SOMETHING: statement in the first place. switch (uMsg) { case WM_CHAR: OnChar(...); ret...

Code
Jan 1, 2009
Post comments count0
Post likes count1

How do I write a program that can be run either as a console or a GUI application?

Raymond Chen

You can't, but you can try to fake it. Each PE application contains a field in its header that specifies which subsystem it was designed to run under. You can say to mark yourself as a Windows GUI application, or you can say to say that you are a console application. If you are GUI application, then the program will run without a console. The s...

Code
Dec 29, 2008
Post comments count0
Post likes count1

Undecorating names to see why a function can’t be found

Raymond Chen

Here's a problem inspired by actual events. When I build my project, it compiles fine, but it fails during the link step with an unresolved external: program.obj : error LNK2001: unresolved external symbol "public: virtual wchar_t const * __thiscall UILibrary::PushButton::GetName(class UILibrary::StringHolder * *)" (?GetName@PushButton@UILibr...

Code
Dec 19, 2008
Post comments count0
Post likes count1

What is the mysterious fourth message box button?

Raymond Chen

When you call the MessageBox function, you pass flags specifying which of a fixed set of button patterns you want (for example, Yes/No and OK/Cancel) and which button you want to be the default (MB_DEFBUTTON1 through MB_DEFBUTTON4.) Wait a second. What's with this MB_DEFBUTTON4? None of the button patterns are four-button patterns. The highest n...

Code
Dec 18, 2008
Post comments count0
Post likes count1

How do I obtain the computer manufacturer’s name?

Raymond Chen

One customer wanted a way to determine the name of the computer manufacturer. For example, they wanted to make some function call and get back "IBM" or "Compaq" or "Dell". I don't know why they wanted this information, and for the moment, I don't care. And of course, when you're looking for information, you don't search MSDN; that's for crazy pe...

Code
Dec 11, 2008
Post comments count0
Post likes count1

Don’t use global state to manage a local problem

Raymond Chen

We've seen a few instances where people have used a global setting to solve a local problem. For example, people who use the function to prevent a window from redrawing, toggle a global setting to see what its value is, or who change the system time zone as part of an internal calculation. To this, I'll add as an example a program which figures...

Code