Showing tag results for Code

Jan 28, 2004
Post comments count0
Post likes count2

Another reason not to do anything scary in your DllMain: Inadvertent deadlock

Raymond Chen

Your DllMain function runs inside the loader lock, one of the few times the OS lets you run code while one of its internal locks is held. This means that you must be extra careful not to violate a lock hierarchy in your DllMain; otherwise, you are asking for a deadlock. (You do have a lock hierarchy in your DLL, right?) The loader lock is tak...

Code
Jan 27, 2004
Post comments count0
Post likes count1

Some reasons not to do anything scary in your DllMain

Raymond Chen

As everybody knows by now, you're not supposed to do anything even remotely interesting in your DllMain function. Oleg Lvovitch has written two very good articles about this, one about how things work, and one about what goes wrong when they don't work. Here's another reason not to do anything remotely interesting in your DllMain: It's common ...

Code
Jan 26, 2004
Post comments count0
Post likes count0

The hollow brush

Raymond Chen

What is the hollow brush for? The hollow brush is a brush that doesn't do anything. You can use it when you're forced to use a brush but you don't want to. As one example, you can use it as your class brush. Then when your program stops responding and Windows decide to do the "white flash" (see yesterday's entry), it grabs the hollow brush and en...

Code
Jan 20, 2004
Post comments count0
Post likes count1

ia64 – misdeclaring near and far data

Raymond Chen

As I mentioned yesterday, the ia64 is a very demanding architecture. Today I'll discuss another way that lying to the compiler will come back and bite you. The ia64 does not have an absolute addressing mode. Instead, you access your global variables through the r1 register, nicknamed "gp" (global pointer). This register always points to your globa...

Code
Jan 16, 2004
Post comments count0
Post likes count1

How can a program survive a corrupted stack?

Raymond Chen

Continuing from yesterday: The x86 architecture traditionally uses the EBP register to establish a stack frame. A typical function prologue goes like this: This establishes a stack frame that looks like this, for, say, a __stdcall function that takes two parameters. Parameters can be accessed with positive offsets from EBP; for example, par...

Code
Jan 15, 2004
Post comments count0
Post likes count1

What can go wrong when you mismatch the calling convention?

Raymond Chen

Believe it or not, calling conventions is one of the things that programs frequently get wrong. The compiler yells at you when you mismatch a calling convention, but lazy programmers will just stick a cast in there to get the compiler to "shut up already". And then Windows is stuck having to support your buggy code forever. The window procedure ...

Code
Jan 12, 2004
Post comments count0
Post likes count0

Why can't I GetProcAddress a function I dllexport'ed?

Raymond Chen

The dllexport attribute tells the linker to generate an export table entry for the specified function. This export entry is decorated. This is necessary to support ing of overloaded functions. But it also means that the string you pass to needs to be decorated. As we learned earlier, the decoration scheme varies from architecture to architectur...

Code
Jan 1, 2004
Post comments count0
Post likes count0

Don't trust the return address

Raymond Chen

Sometimes people ask, "So I know how to get my return address [use the _ReturnAddress() intrinsic]; how do I figure out what DLL that return address belongs to?" Beware. Even if you figure out which DLL the return address belongs to [use Get­Module­Handle­Ex(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)], that doesn't mean that that is a...

Code
Dec 30, 2003
Post comments count0
Post likes count0

What's the difference between CreateMenu and CreatePopupMenu?

Raymond Chen

CreateMenu creates a horizontal menu bar, suitable for attaching to a top-level window. This is the sort of menu that says "File, Edit", and so on. CreatePopupMenu creates a vertical popup menu, suitable for use as a submenu of another menu (either a horizontal menu bar or another popup menu) or as the root of a context menu. If you get the two con...

Code