Object nature-shgetfileinfo ()

Source: Internet
Author: User
Tags hex code

This article describes the following methods:

  •  Shgetfileinfo Definition
  • Shgetfileinfo ()How functions work
  •  Shgetfileinfo ()Function return value
  •  Shgetfileinfo ()Simple Function example
  Shgetfileinfo Definition

Shgetfileinfo is defined on msdn as follows:

Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.

It is defined in shellapi. h. This function has five variables, which are defined as follows:

DWORD_PTR SHGetFileInfo(  __in     LPCTSTR pszPath,  DWORD dwFileAttributes,  __inout  SHFILEINFO *psfi,  UINT cbFileInfo,  UINT uFlags);

Basically, the shgetfileinfo () function provides information about file system objects. As explained above, this object can be a file, folder, directory or drive root. DWORD return refers to a large number of return states, which are related to the uflags variable settings. To put it simply, you can expect:

  1. Determine the target platform for executable files (Win32, Win16, MS-DOS)
  2. Get various distinctive file icons (small, large, associated, overlapping, selected, open)
  3. Read Other display attributes, such as file type (brief description displayed on the detector type column) and display name (displayed on the name column)
  4. Read any other attributes that are unique to a file, such as whether the file can be copied, moved, deleted, or renamed, whether it can form a shortcut, whether it has subfolders, and whether it is shared, is to drag the target, or have additional property pages, and so on.
Note: Before calling shgetfileinfo (), you must use coinitialize or oleinitialize to initialize com. Otherwise, it can be used on the surface, but may cause insecurity or loss of some functions. Shgetfileinfo ()How functions work

To correctly understand the functions of a function, it is necessary to use all possible methods to force this function to be called. First, let's take a look at the required variables:

Variable name

Description

Pszpath

A buffer containing the relative or absolute path of the file to be retrieved. It can process long or short file names. (That is, the specified file path) note [1]

Dwfileattributes

This parameter is only used when uflags contain the shgfi_usefileattributes flag (not generally used ). In this way, it should be a combination of file attributes: Archive, read-only, directory, system, etc.

Psfi

Pointer to the shfileinfo structure of the received data. Note [2]

Cbfileinfo

Give the size of the above item structure.

Uflags

The core variable of the function, through all possible signs, you can control the function's behavior and actually get information.

Note [1]: When the uflags value does not contain shgfi_pidl, you can directly specify it;

When the uflags value contains shgfi_pidl, pszpath must be calculated and cannot be specified directly;

Note [2]: The shfileinfo structure is defined as follows:

typedef struct _SHFILEINFO {  HICON hIcon;  int   iIcon;  DWORD dwAttributes;  TCHAR szDisplayName[MAX_PATH];  TCHAR szTypeName[80];} SHFILEINFO;

In addition, this structure is always used to return data to the calling program and never need to be initialized. The only one that can contain information to affect the function row is the dwattributes Member, which will be further explained later. Obviously, all interest in various behaviors using the shgetfileinfo () function is concentrated on setting the uflags variable value. In most cases, information is returned through the psfi buffer, but in some cases, it can be effectively included in the DWORD return of the function.

Return Value of shgetfileinfo () function

Returns a value whose meaning depends on the uFlags parameter. If uFlags contains the SHGFI_EXETYPE flag, the return value specifiesthe type of the executable file. It will be one of the following values:

Value Executable File Type
0 Nonexecutable file or an error condition
Loword = ne or PE and hiword = 3.0, 3.5, or 4.0 Windows Application
Loword = MZ and hiword = 0 MS-DOS. EXE,. com, or. BAT file
Loword = PE and hiword = 0 Win32 console application

If the function returns 0, an error occurs somewhere. In most cases, it is because an unreasonable file name or pidl is passed, or a combination of conflicting signs is specified. Compared with the first two, the latter is more likely. Unless the specified flag tells it to perform the specified operation, if each operation is successfully completed, this function returns 1. One exception is that when the shgfi_exetype flag is set,

Shgetfileinfo () returns the DWORD Value. At this time, the low word represents the signature of the executable file, which is explained in the following table:

File Signature

HEX Code

Meaning

PE

Zero X 4550

Win32 executable format, which is used by all 32-bit Microsoft operating systems.

Ne

0x454e

Windows 3. x new executable format, typically a 16-bit window Program

MZ

0x5a4d

DOS executable format. This value is also returned if you query. com or. bat.

The corresponding hex code is actually the signature code of the file signature column. For example, 0x50 corresponds to P and 0x45 corresponds to E. The two bytes of a high character contain the minimum operating system version number required for running.

Another case where the return code contains more meanings is that the shgfi_sysiconindex flag is set. At this time, the function returns a system image list handle, which contains the icon of the specified file or folder.

Simple Example of shgetfileinfo () function
#include <windows.h>#include <ShellAPI.h>#include <ShlObj.h>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){TCHAR str[MAX_PATH] = {0};OPENFILENAME ofn;ZeroMemory(&ofn,sizeof(OPENFILENAME));ofn.lStructSize  = sizeof(OPENFILENAME);ofn.lpstrFilter = TEXT("All File\0*.*\0Text\0*.txt\0\0");ofn.nFilterIndex  = 1;ofn.lpstrFile = str;ofn.nMaxFile = MAX_PATH;ofn.lpstrInitialDir = TEXT("C:\\");ofn.lpstrTitle = TEXT("Hello");if(GetOpenFileName(&ofn))//LPITEMIDLIST pidl = NULL;//SHGetFolderLocation(NULL, CSIDL_DRIVES, NULL, 0, &pidl);{CoInitialize(NULL);SHFILEINFO shfi;ZeroMemory(&shfi, sizeof(SHFILEINFO));HRESULT hr = SHGetFileInfo((LPCTSTR)ofn.lpstrFile,-1,&shfi,sizeof(shfi),SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES);if (SUCCEEDED(hr)){// The display name is now held in sfi.szDisplayName.DWORD dwAttr = shfi.dwAttributes;TCHAR sbuf[1024] = {0};if (dwAttr & SFGAO_CANCOPY)lstrcat(sbuf,TEXT("Copy, "));if(dwAttr & SFGAO_CANDELETE)lstrcat(sbuf,TEXT("Delete, "));if(dwAttr & SFGAO_CANMOVE)lstrcat(sbuf,TEXT("Move, "));if(dwAttr & SFGAO_CANRENAME)lstrcat(sbuf,TEXT("Rename, "));if(dwAttr & SFGAO_CANLINK)lstrcat(sbuf,TEXT("Link, "));wsprintf(str,TEXT("the display name is:%s\nthe typename is:%s\nthe attribute is %s\nthe icon index is %d\n"),shfi.szDisplayName,shfi.szTypeName,sbuf,shfi.iIcon);MessageBox(NULL,str,TEXT("MessageBox"),MB_OK);}CoUninitialize();}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.