Showing tag results for Code

Nov 25, 2013
Post comments count0
Post likes count0

Extracting GPS coordinates from a photo and plotting it on a map

Raymond Chen

Today's Little Program extracts GPS coordinates from a photo and plots it on a map. Remember, Little Programs do little to no error checking, because that's how they roll. We start with a simple function that takes a latitude and longitude and opens a Web page that highlights that coordinate. In a real program, you probably would do something m...

Code
Nov 22, 2013
Post comments count0
Post likes count1

How do I get the effect of CW_USEDEFAULT positioning on a window I've already created?

Raymond Chen

A customer wanted to know how to get the effect of positioning on a window that already exists. In particular, they wanted to be able to reposition a dialog box to get the cascade effect, but since you can't actually pass in a dialog template, the repositioning has to be done after the fact. (Presumably in the handler, which runs before the d...

Code
Nov 21, 2013
Post comments count0
Post likes count1

How do I get the path to the default user's profile?

Raymond Chen

A customer wanted to know how to get the path to the default user's profile. On older versions of Windows, the default location of the default user's profile was . Then it moved to . Now it's in . And the location may have been customized, so in principle it could be anywhere. The function to get the default user profile's directory is is the dev...

Code
Nov 18, 2013
Post comments count0
Post likes count1

How can I launch an unelevated process from my elevated process and vice versa?

Raymond Chen

Going from an unelevated process to an elevated process is easy. You can run a process with elevation by passing the verb to or . Going the other way is trickier. For one thing, it's really hard to munge your token to remove the elevation nature properly. And for another thing, even if you could do it, it's not the right thing to do, because ...

Code
Nov 14, 2013
Post comments count0
Post likes count1

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

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 count1

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

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 count1

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

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 count1

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

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 count1

What is the point of FreeLibraryAndExitThread?

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 count1

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

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