Harry
Harry

Reputation: 179

Whether to show Common Item Dialog or GetOpenFileName? (Win32 API)

I am wondering if I am reinventing the wheel here.

In essence, the new Common Item Dialog file browser is much better than the old GetOpenFileName() version. If a user is on Vista+ operating system I want to use the new dialog, yet still have a function for the old dialog on XP, etc..

So I am wondering if I should create a function like this.

BOOL ShowOpenFileDialog(_Out_ LPTSTR szBuffer, _In_ UINT iBufferSize)
{
    static DWORD dwMajorVersion = 0;

    if (!dwMajorVersion)
        dwMajorVersion = (DWORD)(LOBYTE(LOWORD(GetVersion())));

    if (dwMajorVersion >= 6)    // Vista+
        return ShowNewOpenFileDialog(szBuffer, iBufferSize); // show common item

    return ShowOldOpenFileDialog(szBuffer, iBufferSize);  // fall back to old dialog
}

Also a followup is the common item C++ only?

Upvotes: 5

Views: 2508

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598134

As Raymond Chen said, you should not be relying on version numbers, rely on the availability of functionality instead, eg:

BOOL ShowOpenFileDialog(_Out_ LPTSTR szBuffer, _In_ UINT iBufferSize)
{
    IFileDialog *pfd = NULL;
    HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_IFileDialog, (void**)&pfd);
    if (SUCCEEDED(hr))
    {
        // use IFileDialog as needed...
        pfd->Release();
    }
    else
    {
        // use GetOpenFileName() as needed...
    }
}

And no, IFileDialog is not specific to C++ only. It can be used by any language that supports COM, including C, C++, Delphi, VisualBasic, etc.

Upvotes: 9

Related Questions