Showing tag results for Code

Nov 21, 2013
Post comments count0
Post likes count0

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

Raymond Chen
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 count0

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

Raymond Chen
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 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