Shh! Be Very, Very Quiet; We're Hunting New Functions

Windows 95 offers some new functions and structures that you can use to retrieve information about files. These functions all have an SH prefix, so, as Elmer Fudd would say, “Be vewy, vewy quiet.…”

In the SHELLFUN sample, the SHGetFileInfo function retrieves information about a file specified by the user. Not one to give advice that I don't take myself (“Don't do anything that the system will do for you”), I use the Open common dialog box to get the name of the file to query. When the user chooses the file, its display name and type name are displayed in the multiline edit control in SHELLFUN's main window:

void OnFileInfo (HWND hWnd, HWND hwndEdit)
{
OPENFILENAME OpenFileName;
char szDirName [MAX_PATH] = "";
char szFile [MAX_PATH] = "\0";
char szFileTitle [MAX_PATH] = "\0";

// The filter specification for the OPENFILENAME structure
char szFilter [] = {"All Files\0*.*\0"};

OpenFileName.lStructSize = sizeof (OPENFILENAME);
OpenFileName.hwndOwner = hWnd;
OpenFileName.hInstance = (HANDLE)g_hInst;
OpenFileName.lpstrFilter = szFilter;
OpenFileName.lpstrCustomFilter = (LPTSTR)NULL;
OpenFileName.nMaxCustFilter = 0L;
OpenFileName.nFilterIndex = 1L;
OpenFileName.lpstrFile = szFile;
OpenFileName.nMaxFile = sizeof (szFile);
OpenFileName.lpstrFileTitle = szFileTitle;
OpenFileName.nMaxFileTitle = sizeof (szFileTitle);
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.lpstrTitle = "Pick a file for information.";
OpenFileName.nFileOffset = 0;
OpenFileName.nFileExtension = 0;
OpenFileName.lpstrDefExt = "*.*";
OpenFileName.lCustData = 0;
OpenFileName.Flags = OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

if (GetOpenFileName (&OpenFileName))
{
SHFILEINFO sfi;
char buff [2056];

// The user chose to get the file information: display name,
// type name, file attributes, icon location, and executable type.
if (SHGetFileInfo (OpenFileName.lpstrFile, 0, &sfi,
sizeof (SHFILEINFO), SHGFI_DISPLAYNAME | SHGFI_TYPENAME))
{
memset (buff, '\0', sizeof (buff));

// Display the information.
wsprintf (buff, "Display name: %s Type Name: %s",
sfi.szDisplayName, sfi.szTypeName);

// Update the multiline edit control with the file description.
SendMessage (hwndEdit, WM_SETTEXT, 0, (LPARAM)buff);
}
}
}

The end result, shown in Figure 11-2, isn't the fanciest example of user interface design, but it does illustrate that the display name can be a long filename. The type name is the name registered for that type of file in the Registry.

Figure 11-2.

Retrieving file information in SHELLFUN.