Showing tag results for Code

Nov 14, 2013
Post comments count0
Post likes count0

Why is my FormatMessage call crashing trying to read my insertion parameter?

Raymond Chen
Raymond Chen

A customer was looking for assistance in debugging a crash in their product. The stack trace looked like this: The string being formatted is , and the insertion is a long (but valid) string. A unit test which passes a similarly long object name to does not crash. What is the problem? There are clues in the stack trace. The natural place to ...

Code
Nov 8, 2013
Post comments count0
Post likes count0

What's the point of the various …WhenCallbackReturns functions?

Raymond Chen
Raymond Chen

The thread pool provides a number of functions named . What's the point of all these functions? Why can't you just do the operation yourself immediately before returning? We saw last time. What's the point of the others? Basically, the same thing as . It's a way to release a resource after execution has left the function and the callback is mar...

Code
Nov 7, 2013
Post comments count0
Post likes count0

Partially eliminating the need for SetThreadpoolCallbackLibrary and reducing the cost of FreeLibraryAndExitThread

Raymond Chen
Raymond Chen

Update: Daniel points out that there is still a race condition here, so this trick won't work. Rats. The documentation for the says This prevents a deadlock from occurring when one thread in DllMain is waiting for the callback to end, and another thread that is executing the callback attempts to acquire the loader lock. If the DLL containi...

Code
Nov 6, 2013
Post comments count0
Post likes count0

CoUninitalize will ask a DLL if it is okay to unload now, but the answer is a foregone conclusion

Raymond Chen
Raymond Chen

The entry point is exported by COM in-proc servers. COM host applications call periodically to ask COM to do DLL housecleaning, and in response, COM asks each DLL if it is okay to be unloaded. If so, then COM unloads the DLL. What is not well-known is that COM also does DLL housecleaning when you shut down the last apartment by calling . When ...

Code
Nov 5, 2013
Post comments count0
Post likes count0

What is the point of FreeLibraryAndExitThread?

Raymond Chen
Raymond Chen

The function seems pointless. I mean, all the function does is Who needs such a trivial function? If I wanted to do that, I could just write it myself. And then you discover that occasionally your program crashes. What's going on? Let's rewind and look at the original problem. Originally, you had code that did something like this: Th...

Code
Nov 4, 2013
Post comments count0
Post likes count0

Manipulating the zone identifier to specify where a file was download from

Raymond Chen
Raymond Chen

When you download a file via Internet Explorer, the file is tagged with a little bit of information known as a zone identifier which remembers where the file was downloaded from. This is what tells Explorer to put up the "Yo, did you really want to run this program?" prompt and which is taken into account by applications so that they can do things...

Code
Nov 1, 2013
Post comments count0
Post likes count0

On the various ways of getting the current time and date in Win32

Raymond Chen
Raymond Chen

There are a number of functions in Win32 that obtain the current date and time. Here's how they fit together: The starting point is . This returns the current time in UTC in the form of a structure. This also happens to be the time format used internally by the system, so this value can be retrieved with a minimum of fuss. You can also call ...

CodeTime
Oct 28, 2013
Post comments count0
Post likes count0

Using GetLogicalProcessorInformationEx to see the relationship between logical and physical processors

Raymond Chen
Raymond Chen

Today's Little Program uses the function to print the mapping of logical processors to physical processors, as well as the mapping of logical processors to packages. (A dual-core processor is a single package with two cores. If those cores are themselves dual-hyperthreaded, then you have four logical processors total.) The helper function tak...

Code
Oct 25, 2013
Post comments count0
Post likes count0

My, those threads start up really fast nowadays

Raymond Chen
Raymond Chen

Here's a little puzzle inspired by an actual bug: Can the assertion at the start of ever fire? Naturally, the answer is Yes, otherwise it wouldn't be a very interesting article. The assertion can fire if the worker thread starts running before the call the returns. In that case, the caller hasn't yet received the handle or ID of the newly...

Code