.NET

How do I find out which process has a file open?

Posted by Ryan on March 23, 2012 at 10:27 am

Classically, there was no way to find out which process has a file open. A file object has a reference count, and when the reference count drops to zero, the file is closed. But there’s nobody keeping track of which processes own how many references. (And that’s ignoring the case that the reference is not coming from a process in the first place; maybe it’s coming from a kernel driver, or maybe it came from a process that no longer exists but whose reference is being kept alive by a kernel driver that captured the object reference .) This falls into the category of not keeping track of information you don’t need . The file system doesn’t care who has the reference to the file object. Its job is to close the file when the last reference goes away. You do the same thing with your COM object reference counts. All you care about is whether your reference count has reached zero (at which point it’s time to destroy the object). If you later discover an object leak in your process, you don’t have a magic query “Show me all the people who called AddRef on my object” because you never kept track of all the people who called AddRef on your object. Or even, “Here’s an object I want to destroy. Show me all the people who called AddRef on it so I can destroy them and get them to call Release .” At least that was the story under the classical model. Enter the Restart Manager . The official goal of the Restart Manager is to help make it possible to shut down and restart applications which are using a file you want to update. In order to do that, it needs to keep track of which processes are holding references to which files. And it’s that database that is of use here. (Why is the kernel keeping track of which processes have a file open? Because it’s the converse of the principle of not keeping track of information you don’t need: Now it needs the information!) Here’s a simple program which takes a file name on the command line and shows which processes have the file open. #include #include #include int __cdecl wmain(int argc, WCHAR **argv) { DWORD dwSession; WCHAR szSessionKey[CCH_RM_SESSION_KEY+1] = { 0 }; DWORD dwError = RmStartSession(&dwSession, 0, szSessionKey); wprintf(L”RmStartSession returned %dn”, dwError); if (dwError == ERROR_SUCCESS) { PCWSTR pszFile = argv[1]; dwError = RmRegisterResources(dwSession, 1, &pszFile, 0, NULL, 0, NULL); wprintf(L”RmRegisterResources(%ls) returned %dn”, pszFile, dwError); if (dwError == ERROR_SUCCESS) { DWORD dwReason; UINT i; UINT nProcInfoNeeded; UINT nProcInfo = 10; RM_PROCESS_INFO rgpi[10]; dwError = RmGetList(dwSession, &nProcInfoNeeded, &nProcInfo, rgpi, &dwReason); wprintf(L”RmGetList returned %dn”, dwError); if (dwError == ERROR_SUCCESS) { wprintf(L”RmGetList returned %d infos (%d needed)n”, nProcInfo, nProcInfoNeeded); for (i = 0; i < nProcInfo; i++) { wprintf(L”%d.ApplicationType = %dn”, i, rgpi[i].ApplicationType); wprintf(L”%d.strAppName = %lsn”, i, rgpi[i].strAppName); wprintf(L”%d.Process.dwProcessId = %dn”, i, rgpi[i].Process.dwProcessId); HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, rgpi[i].Process.dwProcessId); if (hProcess) { FILETIME ftCreate, ftExit, ftKernel, ftUser; if (GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser) && CompareFileTime(&rgpi[i].Process.ProcessStartTime, &ftCreate) == 0) { WCHAR sz[MAX_PATH]; DWORD cch = MAX_PATH; if (QueryFullProcessImageNameW(hProcess, 0, sz, &cch) && cch

Live Coding Implemented

Posted by Ryan on March 22, 2012 at 9:28 am

Remember Bret Victor’s live coding talk from last month? He presented an example where he would edit code on one side, and the corresponding visual would automatically update on the other side. It was instant feedback that could help in learning code.
Gabriel Florit implemented the idea with D3 , and it’s called water. [...]

Installing Visual Studio 11 on Windows 7

Posted by Ryan on March 2, 2012 at 10:42 am
image

With the launch of Visual Studio 11 beta and that it is fully supported in production I wanted to update my laptop with the bits. I have a presentation on Friday of what’s new in Visual Studio 11 and I will show some of the new features there. Figure: The new installer is clean There has been some comment on the colours, or lack there of, in the new UI, but to be honest it works. It may be a little drab for the first wee while, but once you get used to it you begin to love it. It a user interface for every day use, not occasional use. Figure: Installation is painless with no configuration lets face it, you don’t need to configure the install and now that the team has paired it down to only 1.6GB we can have it all and not worry about space. Figure: All components get on there but it may take a while It took a little while, perhaps 30 minutes. Figure: Shucks, another re-boot! I don’t know why, but it did not have any extra bits to do so hopefully they will be able to install without a reboot if you already have .NET 4.5 as I did. Figure: Pick your layout You get a bunch of first run layouts with Visual Studio 11 and you even get a “code only” minimal one for web development. But I like the “Visual Basic” layout. Note that this is not picking a language it is just picking a layout of Visual Studio IDE. You can customise everything later. Figure: Sigh, and another progress bar While Visual Studio configures itself you need to wait, but only for a few minutes…and then… Figure: The new UI I really like the clean new UI. Colour is now for accent and not just for splashing around the place (although that is a running joke at the MVP Summit) and almost all things that have colour you can click! Figure: Connecting to Team Foundation Server I LOVE the new team Explorer, but it will take a little getting used to… Remember that there is Go-Live for Visual Studio 11 ! Go on… be a kid again! Related Posts: Announcing Visual Studio 11 Beta will launch on February 29th Using Corporate ID’s with Visual Studio 11 Team Foundation Service Introduction to Visual Studio 11 Upgrading from TFS2010 to Visual Studio 11 Team Foundation Server in production Upgrade to Visual Studio 11 Team Foundation Service – Done -Do you want to move to Visual Studio 11 Team Foundation Service NOW? Microsoft is providing a Go-Live licence (that means that it is supported in production) and you can use it today! For help moving forward contact info@nwcadence.com …

Tetris In 140 Bytes

Posted by Ryan on February 21, 2012 at 9:44 am

Is it possible to write a JavaScript program in no more than a tweet's length?
A website called 140byt.es says it is and has an implementation of Tetris to prove it. Ok, it only has two types of block — hence its title “Binary Tetris” — and there's no rotate, but it works. The blocks [...]

Chrome Team Releases Field Guide to Web Applications

Posted by Ryan on February 16, 2012 at 9:11 am

The Chrome folks have put together a Field Guide for Web Applications that is almost as notable for its design as the content itself. The field guide is a short resource with four chapters on Web apps, and one devoted to “Bert Appward” – the fictitious author of the guide. 
The guide is laid out [...]