# Sunday, November 26, 2006

I've recently been asked (again) about pinning pointers so I decided to blog it, so that next time I can just email the link and save everyone a little time :-)

When you're working in C++/CLI, there are two kinds of indirect access to objects: handles and pointers. A pointer is that good old friend (fiend?) we've known for so long and it holds the address of a place on the native heap or the stack. You get a pointer by using the new operator with a type, or the address-of operator, &, with an instance:

Customer c("Microsoft");
Customer* pc1 = &c;
Customer* pc2 = new Customer("CTV");

Pointers stay still. You can cast them to ints, stick those ints into collections, do whatever you like to them, and later cast them back to pointers and dereference them and if they were pointing to something on the heap, like pc2 does, they will still be pointing to the same place on the native heap the whole time. What's more, the instance they point to will not have gone anywhere unless you explicitly delete it yourself.

Handles are the new kid on the block and they sorta-point-to a place on the managed heap. You get a handle by using the gcnew operator with a type, or the make-me-a-handle operator, %, with an instance:

Employee e("Kate", "Gregory");
Employee^ he1 = %e;
Employee^ he2 = gcnew Employee("Brian","Gregory");

Handles don't stay still. If you found a way to get the value out of them and then stuck it somewhere and let the handle itself go out of scope, the garbage collector might clear away the item on the heap, making the number meaningless. Even if you kept the handle in scope, the garbage collector might move the item to somewhere else, making the number meaningless.

Now normally this is nothing more than an interesting fact, one of the differences between handles and pointers. But what if you have some information contained inside a managed instance and you need to pass it to some native code that doesn't know about handles? Now you not only need to convert somehow, you also need to ask the garbage collector not to move that heap item while you are holding a pointer to it. This is called pinning. Let's switch to simpler types so you don't have to worry about what's in an Employee object. I need this most often with strings - I have some managed code that gives me a String^ and I need to pass it to some native code that needs a LPCWSTR or the like.

String^ title=HandyLibraryFunction();
   pin_ptr<const WCHAR> str = PtrToStringChars(title);

PtrToStringChars is a handy function you will find in <vcclr.h>. Those brace brackets are important -- when the pinned pointer goes out of scope, the garbage collector is allowed to move the item again. You really need to minimize pinning time or you will ruin your app's performance, so I've artificially shortened its lifetime to release it as soon as possible. And whatever NativeFunction does, it better not take a copy of the pointer it's given and save it away for future use, because it will be a dangling pointer once str has gone out of scope -- even if title is still in scope.

The concept is much the same when you want a member variable from a managed instance. Grab hold of it and use the pin_ptr template to pin it and give you a pointer, which you can then work with following all the rules of pointers. Let go of it as soon as you can.


Sunday, November 26, 2006 5:51:25 PM (Eastern Standard Time, UTC-05:00)  #    
# Saturday, November 25, 2006

Here's a little oddity I happened across. It's a patent application:

The present invention provides a system and/or method that facilitates expanding keywords within an existing computer programming language by employing a whitespace keyword containing embedded whitespace. A whitespace component can receive code, and create a whitespace keyword based at least upon a successive comparison of adjacent tokens. The whitespace component creates a whitespace keyword by replacing more than one token with a single token containing embedded whitespace. Moreover, the whitespace component can utilize a lexical analyzer to group code into tokens and a parser component to parse the code.

This actually makes sense to me (assuming you accept that the idea of software patents in general makes sense.) It was a huge leap to take a language like C++, where keywords are separated by spaces, and extend it to become a language where some of the keywords actually contain spaces. After all, so many of us already have words like ref and value as variable names: the only way for C++/CLI to work is the conceptual leap that says "ref isn't the keyword, ref class is the keyword." If you wonder how that's actually done... read the patent application.


Saturday, November 25, 2006 2:54:42 PM (Eastern Standard Time, UTC-05:00)  #    
# Friday, November 24, 2006

There are quite a few products, generally older products, that aren't going to be supported on Vista and probably aren't going to work on Vista. One of the not supported announcements that seems to be catching people by surprise is about SQL Server 2000 and it's free sibling, MSDE. They seem to have run afoul of UAC, which really does require you to change your application.

If you are using SQL Server 2000, you should upgrade to 2005 anyway: it has a lot of improvements for you. But MSDE users don't generally even realize they are using a database: they're using an application that uses a database. Whoever made that application needs to tweak their install so it uses SQL Express (which is just as free as MSDE ever was, and doesn't have a governor) instead. And if that's you, learn about Vista development while you're at it in case your application ends up needing changes too.


Friday, November 24, 2006 2:24:51 PM (Eastern Standard Time, UTC-05:00)  #    
# Thursday, November 23, 2006

Has someone sent you a .docx file yet? That's the Word 2007 file format.  (The new Excel format is .xlsx, the new Powerpoint is .pptx, and so on.) The first time I tried to open one with Word 2003, I got a helpful dialog offering to get and install the converter for me, and I did, and now I can move the files back and forth around my network without concern. But a few people have mentioned to me that they didn't get this helpful dialog. You can hand-download and install the converter from the Office preview site. Make sure you are up to date on Office Service Packs... there are links from the download page. And though the converter says it's for the Technical Refresh 2, it has worked fine for me on files created with earlier betas.


Thursday, November 23, 2006 2:02:09 PM (Eastern Standard Time, UTC-05:00)  #    
# Wednesday, November 22, 2006

I don't have a tag cloud on this blog yet because I haven't figured out how to do it with dasblog. But I think they're cool when I see them on other people's blogs. I came across a site that uses the cloud approach (larger fonts mean more occurrences) to report on word frequency in US presidential speeches. It's really neat to see Constitution fade over time, while economy or economic stays always there, and new words rise up. Play with the slider a little. I wonder what other bodies of text you could apply tag clouds to? I wonder what all the powerpoints on this laptop would produce?


Wednesday, November 22, 2006 1:43:56 PM (Eastern Standard Time, UTC-05:00)  #    
# Tuesday, November 21, 2006

When Microsoft discovers a bug in a product, whether it's Windows, Visual Studio, or some other product, they typically fix it. And once that fix is tested and working, the question becomes how to deploy it. Sure, you can put the corrected code into the next version of the product, but that might be years away. There are service packs, but they are pretty far apart, and for good reason. So the first deployment vehicle is the hotfix or QFE (Quick Fix Engineering.) These are typically available only from support and only after you've convinced support you're facing the problem that the hotfix takes care of. (At least, that's the theory. Since a hotfix is a single file, people can and do share them although I suspect you're not supposed to.)

Now there's a new pilot program underway where some hotfixes are being made public, so that you don't have to wait for a service pack. Of course you should approach this with caution, but it can make investigating those hard problems a little smoother. At the moment I see seven hotfixes there: three are for Visual C++.


Tuesday, November 21, 2006 12:56:15 PM (Eastern Standard Time, UTC-05:00)  #    
# Monday, November 20, 2006

Ed Bott has published some advanced tips for Vista. My favourite? Master the Quick Launch bar, including the Add to Quick Launch context menu item and (I didn't know this) keyboard shortcuts for the items in the Quick Launch. More reasons piling up why I want to move this laptop to Vista ... not much longer!


Monday, November 20, 2006 12:24:17 PM (Eastern Standard Time, UTC-05:00)  #    
# Sunday, November 19, 2006

XPS is a new document format. Office 2007 uses it, and your applications can use it too. Under the hood, an XPS document is just a zip file of many XML files and some binary resources (such as images.) The Microsoft XPS page says:

Microsoft has integrated XPS-based technologies into the 2007 Microsoft Office system and the Microsoft Windows Vista operating system, but XPS itself is platform independent, openly published, and available royalty-free. Microsoft is using XPS to bring additional document value to its customers, its partners, and the computing industry.

Since XPS documents describe layout and rendering as well as just content, you can think of them a lot like PDF files. As a result they're likely to show up on web sites or to be emailed to you. If you haven't yet moved up to Vista/Office 2007/IE7 you may not know what to do with them.

The solution is a free XPS viewer. It's up to you whether you want it integrated into IE6 -- I got the standalone viewer from the XPS Essentials. Took only a minute or so to download and install, and I didn't even have to reboot. Get it and be one of the cool kids again.


Sunday, November 19, 2006 7:58:15 AM (Eastern Standard Time, UTC-05:00)  #