Windows shell programming 3 (function explanation)

Source: Internet
Author: User

Explain in detail some of the most basic functions, struct, and enumeration in Shell programming.

Shgetasktopfolder
Get the IShellFolder interface of the desktop

 [DllImport("shell32.dll")]
public static extern Int32 SHGetDesktopFolder(out IntPtr ppshf);

To use this function, you must first define an IntPtr pointer. Then, use the pointer to use GetObjectForIUnknown to return the pointer instance through the IShellFolder interface pointing to the COM object. Therefore, you need to write the following functions:

Code

       public static IShellFolder GetDesktopFolder(out IntPtr ppshf) {
SHGetDesktopFolder(out ppshf);
Object obj = Marshal.GetObjectForIUnknown(ppshf);
return (IShellFolder)obj;
}


ParseDisplayName
Obtain the PIDL of an object, even if the object is in the bottom or more layers of the current directory in the directory tree. For example, for a file object, its resolution name is its path. We use the full path name of the file system object to call the ParseDisplayName method of the IshellFolder interface on the desktop, it returns the complete PIDL of this object. Definition:

 

Code

 void ParseDisplayName(
IntPtr hwnd,
IntPtr pbc,
[MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName,
out uint pchEaten,
out IntPtr ppidl,
ref uint pdwAttributes);

The most important parameter is out IntPtr ppidl, which returns PIDL corresponding to the specified path of pszDisplayName. However, PIDL alone does not allow you to do more things. At this time, you also need to call BindToObject to return the IShellFolder interface.

 

BindToObject
Create and initialize an IShellFolder object based on PIDL. Definition:

 void BindToObject(
IntPtr pidl,
IntPtr pbc,
[In()] ref Guid riid,
out IShellFolder ppv);

There is a [In ()] ref Guid riid parameter, indicating the interface identifier (IID ). GUID is actually a unique identifier. No two computers in the world will generate duplicate GUID values. GUID is used to assign a unique identifier to a network or system with multiple nodes and computers. Here we use IID_IShellFolder to indicate that it obtains an IShellFolder interface.

 public static Guid IID_IShellFolder = new Guid("{000214E6-0000-0000-C000-000000000046}");

In addition, the IEnumIDList interface is introduced. The IEnumIDList interface allows the resource manager to obtain the PIDL of all objects contained in the folder. PIDL can then be used to obtain information about these objects.

Therefore, we use the EnumObjects function to return a pointer to the IEnumIDList:

 int EnumObjects(IntPtr hWnd, SHCONTF flags, out IntPtr enumIDList);

Among them, flags is the SHCONTF Enumeration type, which determines the enumerated content:

Code

 public enum SHCONTF {
FOLDERS = 0x20,
NONFOLDERS = 0x40,
INCLUDEHIDDEN = 0x80,
INIT_ON_FIRST_NEXT = 0x100,
NETPRINTERSRCH = 0x200,
SHAREABLE = 0x400,
STORAGE = 0x800
}

Therefore, we can use different flags to list sub-files and sub-directories. Here we will encounter a problem. How can we get the PIDL Object Name. Two functions are written here. You can use PIDL or IShellFolder to return the object name.

 

Related Article

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.