Freigeben über


Coding to the Windows SDK

In the near future I'll be posting code examples that compile and run. In case you'd like to follow along, here's my personal setup. My code should compile on RC1 also, so go ahead and use RC1 if that's what you can get a hold of.

  1. Windows Vista Sept CTP (Build 5728)
  2. The matching Windows SDK Sept CTP
  3. Visual Studio 2005 C++ Express Edition

RC1 NOTE: If you have Windows Vista RC1, be sure to grab the matching Windows RC1 SDK. The versions must match or you will get runtime errors. 

The best thing is, you can get all these downloads for FREE! RC1 will expire in July 2007 but it's a good way to play with Vista before it ships.

The first thing to do is point Visual Studio at the Windows SDK. Click Tools->Options...->Projects and Solutions->VC++ Directories and insert the following directories at the top of each list. I'm a purist, so I removed the other directories too, but you probably don't have to do that.

RC1 NOTE: The RC1 SDK for some reason installs its files to a v1.0 directory. Adjust the paths below accordingly.

  • Executable files:
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Bin
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\Bin
    • $(PATH)
  • Include files:
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Include
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\Include
  • Library files:
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Lib
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\Lib
  • Source files:
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Include
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\Include
  • Exclude directories:
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Include
    • G:\Program Files\Microsoft SDKs\Windows\v6.0\Include

As a purist, I like to start with empty C++ console applications. Let's create one and create propshow.cpp. I'll fill this file out later, but for now, let's just type:

 #include <windows.h>
#include <propsys.h>
#include <propvarutil.h>
#include <propkey.h>
#include <stdio.h>

#pragma comment(lib, "propsys.lib") 
#pragma comment(lib, "ole32.lib") 

int wmain(__in int argc, __in_ecount(argc) WCHAR **argv)
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr))
    {
        PROPVARIANT propvar = {0};
        hr = InitPropVariantFromUInt32(RATING_FOUR_STARS_SET, &propvar);
        if (SUCCEEDED(hr))
        {
            WCHAR szValue[100] = L"\0";
            hr = PSFormatForDisplay(PKEY_Rating, propvar, PDFF_DEFAULT, szValue, ARRAYSIZE(szValue));
            if (SUCCEEDED(hr))
            {
                wprintf(L"The rating is %s.\n", szValue);
            }
            PropVariantClear(&propvar);
        }
        CoUninitialize();
    }
}

That's it! Compile, run, and it should print "The rating is 4 Stars."

-Ben Karas

Comments

  • Anonymous
    October 02, 2006
    It's interesting that you expliclity pass COINIT_DISABLE_OLE1DDE to CoInitializeEx; I've never really seen it used before.  Is it something that we "should" usually do?  I'm guessing it helps reduce attack surface area or something?
  • Anonymous
    October 02, 2006
    The comment has been removed
  • Anonymous
    November 05, 2006
    This coding expedition has developed a tool that can dump out all the properties on a file. If you are