基于Visua C++2010 与 Windows 7 SDK开发windows7 Shell应用(1)-搜索文件夹

windows7 shell 就是windows7操作系统的的控制台

对于熟悉了命令行的人而言,用键盘调用程序比鼠标更快更省力
您可以用"Shell:"命令调用一切可以用资源管理器打开的项目甚至是一次完成.需要很多步骤才能完成的任务.
我们在程序里面如何调用呢,我们来使用为了windows7而生的VS2010,基于C++开发一个shell应用,

基于VS2010+windows7调试通过,

详情请见代码注释

#define STRICT_TYPED_ITEMIDS
#include <windows.h>
#include <structuredquery.h>
#include <strsafe.h>
#include <shlobj.h>
#include <propkey.h>
#include <propvarutil.h>

// 显示界面设置清单挑选正确版本的Comctl32.dll
#pragma comment(linker, "/"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/"")

// 创建一个实例的ICondition的接口,设置搜索的条件
HRESULT GetCondition(ICondition **ppCondition)
{
    *ppCondition = NULL;

    // 创造条件接口。该接口可以帮助创造搜索文件夹的条件。
    IConditionFactory2 *pConditionFactory;
    HRESULT hr = CoCreateInstance(CLSID_ConditionFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pConditionFactory));
    if (SUCCEEDED(hr))
    {
        ICondition *pConditionKind;
        hr = pConditionFactory->CreateStringLeaf(PKEY_Kind, COP_EQUAL, L"Document", NULL, CONDITION_CREATION_DEFAULT, IID_PPV_ARGS(&pConditionKind));
        if (SUCCEEDED(hr))
        {
            ICondition *pConditionSize;
            hr = pConditionFactory->CreateIntegerLeaf(PKEY_Size, COP_GREATERTHAN, 10240, CONDITION_CREATION_DEFAULT, IID_PPV_ARGS(&pConditionSize));
            if (SUCCEEDED(hr))
            {
                // 仅当所有条件满足的时候,开始检索文件夹
                ICondition *rgConditions[] = {pConditionKind, pConditionSize};
                hr = pConditionFactory->CreateCompoundFromArray(CT_AND_CONDITION, rgConditions, ARRAYSIZE(rgConditions), CONDITION_CREATION_DEFAULT, IID_PPV_ARGS(ppCondition));
                pConditionSize->Release();
            }
            pConditionKind->Release();
        }
        pConditionFactory->Release();
    }
    return hr;
}

//开辟了共同文件对话框到IShellItem和等待用户选择的结果文件。
//然后显示在消息框中所选项目的名称。
HRESULT OpenCommonFileDialogTo(IShellItem *pShellItemSearch)
{
    // 打开文件对话框
    IFileDialog* pFileDialog;
    HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
    if (SUCCEEDED(hr))
    {
        // S设置为我们想显示的文件夹
        hr = pFileDialog->SetFolder(pShellItemSearch);
        if (SUCCEEDED(hr))
        {
            // 显示文件对话框
            hr = pFileDialog->Show(NULL);
            if (SUCCEEDED(hr))
            {
                // 获取被选择的文件
                IShellItem *pShellItemSelected;
                hr = pFileDialog->GetResult(&pShellItemSelected);
                if (SUCCEEDED(hr))
                {
                    // 获取文件名称
                    PWSTR pszName;
                    hr = pShellItemSelected->GetDisplayName(SIGDN_NORMALDISPLAY, &pszName);
                    if (SUCCEEDED(hr))
                    {
                        //显示它返回给用户
                        WCHAR szMsg[128];
                        StringCchPrintf(szMsg, ARRAYSIZE(szMsg), L"You Chose '%s'/r", pszName);

                        MessageBox(NULL, szMsg, L"Search Folder Sample", MB_OK);
                        CoTaskMemFree(pszName);
                    }
                    pShellItemSelected->Release();
                }
            }
        }
        pFileDialog->Release();
    }
    return hr;
}

/ /创建一个数组对象壳项目可访问使用IObjectCollection
/ /或IShellItemArray。 IObjectCollection让项目被添加或从集合中删除。
/ /对于代码需要运行在windows7上使用SHCreateShellItemArrayFromIDLists()
HRESULT CreateShellItemArray(REFIID riid, void **ppv)
{
    *ppv = NULL;
    IShellLibrary *pLibrary;
    HRESULT hr = CoCreateInstance(CLSID_ShellLibrary, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pLibrary));
    if (SUCCEEDED(hr))
    {
        hr = pLibrary->GetFolders(LFF_ALLITEMS, riid, ppv);
        pLibrary->Release();
    }
    return hr;
}

/ /这助手创建的范围对象,它是检索的项目集合
/ /定义在搜索的运作情况。
HRESULT CreateScope(IShellItemArray **ppsia)
{
    *ppsia = NULL;

    IObjectCollection *pObjects;
    HRESULT hr = CreateShellItemArray(IID_PPV_ARGS(&pObjects));
    if (SUCCEEDED(hr))
    {
        IShellItem *psi;
        if (SUCCEEDED(SHCreateItemInKnownFolder(FOLDERID_DocumentsLibrary, 0, NULL, IID_PPV_ARGS(&psi))))
        {
            pObjects->AddObject(psi);
            psi->Release();
        }

        //其他项目可以添加到pObjects类似上面的代码。

        hr = pObjects->QueryInterface(ppsia);

        pObjects->Release();
    }
    return hr;
}

//这个程序是一个如何使用ISearchFolderItemFactory接口创建搜索文件夹的示例。
//它创建了一个搜索,然后打开了共同文件对话框的显示搜索结果。

int wmain()
{
    //文件对话框拖放功能需要OLE
    HRESULT hr = OleInitialize(NULL);
    if (SUCCEEDED(hr))
    {
        ISearchFolderItemFactory *pSearchFolderItemFactory;
        hr = CoCreateInstance(CLSID_SearchFolderItemFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSearchFolderItemFactory));
        if (SUCCEEDED(hr))
        {
            IShellItemArray *psiaScope;
            hr = CreateScope(&psiaScope);
            if (SUCCEEDED(hr))
            {
                hr = pSearchFolderItemFactory->SetScope(psiaScope);
                if (SUCCEEDED(hr))
                {
                    //设置的搜索显示名称
                    hr = pSearchFolderItemFactory->SetDisplayName(L"Sample Query");
                    if (SUCCEEDED(hr))
                    {
                        ICondition* pCondition;
                        hr = GetCondition(&pCondition);
                        if (SUCCEEDED(hr))
                        {
                            // 设置检索条件
                            hr = pSearchFolderItemFactory->SetCondition(pCondition);
                            if (SUCCEEDED(hr))
                            {
                                // 搜索IShellItem。这是一个虚拟桌面。
                                IShellItem* pShellItemSearch;
                                hr = pSearchFolderItemFactory->GetShellItem(IID_PPV_ARGS(&pShellItemSearch));
                                if (SUCCEEDED(hr))
                                {
                                    OpenCommonFileDialogTo(pShellItemSearch);
                                    pShellItemSearch->Release();
                                }
                            }
                            pCondition->Release();
                        }
                    }
                }
                psiaScope->Release();
            }
            pSearchFolderItemFactory->Release();
        }
        OleUninitialize();
    }
    return 0;
}

 

附带常用Windows7 shell命令行,大家可以举一反三

shell:Libraries

shell:MusicLibrary

shell:VideosLibrary

shell:OtherUsersFolder

shell:Device Metadata Store

shell:PublicSuggestedLocations

shell:DocumentsLibrary

shell:User Pinned

shell:UsersLibrariesFolder

shell:PicturesLibrary

shell:ImplicitAppShortcuts

shell:Ringtones

shell:CommonRingtones

Windows Vista & 7

shell:Common Programs

shell:GameTasks

shell:UserProfiles

shell:MyComputerFolder

shell:SyncSetupFolder

shell:DpapiKeys

shell:SamplePlaylists

shell:Favorites

shell:My Video

shell:SearchHomeFolder

shell:System

shell:CommonVideo

shell:SyncResultsFolder

shell:LocalizedResourcesDir

shell:Cookies

shell:Original Images

shell:CommonMusic

shell:My Pictures

shell:Cache

shell:Downloads

shell:CommonDownloads

shell:AppData

shell:SyncCenterFolder

shell:My Music

shell:ConflictFolder

shell:SavedGames

shell:InternetFolder

shell:Quick Launch

shell:SystemCertificates

shell:Contacts

shell:TreePropertiesFolder

shell:Profile

shell:Start Menu

shell:Common AppData

shell:PhotoAlbums

shell:ConnectionsFolder

shell:Administrative Tools

shell:PrintersFolder

shell:Default Gadgets

shell:ProgramFilesX86

shell:Searches

shell:Common Startup

shell:ControlPanelFolder

shell:SampleVideos

shell:SendTo

shell:ResourceDir

shell:ProgramFiles

shell:CredentialManager

shell:PrintHood

shell:MAPIFolder

shell:CD Burning

shell:AppUpdatesFolder

shell:Common Start Menu

shell:LocalAppDataLow

shell:Templates

shell:Gadgets

shell:Programs

shell:Recent

shell:SampleMusic

shell:Desktop

shell:CommonPictures

shell:RecycleBinFolder

shell:CryptoKeys

shell:Common Templates

shell:Startup

shell:Links

shell:OEM Links

shell:SamplePictures

shell:Common Desktop

shell:NetHood

shell:Games

shell:Common Administrative Tools

shell:NetworkPlacesFolder

shell:SystemX86

shell:History

shell:AddNewProgramsFolder

shell:Playlists

shell:ProgramFilesCommonX86

shell:PublicGameTasks

shell:ChangeRemoveProgramsFolder

shell:Public

shell:Common Documents

shell:CSCFolder

shell:Local AppData

shell:Windows

shell:UsersFilesFolder

shell:ProgramFilesCommon

shell:Fonts

shell:Personal

Windows 7 Shortcuts

Wireless Networks pop-up

rundll32.exe van.dll,RunVAN

Advanced Restore

sdclt.exe /restorewizardadmin

Restore Files

sdclt.exe /restorewizard

Backup Location & Settings

sdclt.exe /configure

Add Network Location (wizard)

rundll32.exe shwebsvc.dll,AddNetPlaceRunDll

Indexing Options

control.exe srchadmin.dll

Notification Cache

rundll32.exe shell32.dll,Options_RunDLL 5

Aero (Transparency) Off

Rundll32.exe DwmApi #104

Aero (Transparency) On

Rundll32.exe DwmApi #102

Welcome Center

rundll32.exe oobefldr.dll,ShowWelcomeCenter

Add/Remove Programs

RunDll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,0

Content Advisor

RunDll32.exe msrating.dll,RatingSetupUI

Control Panel

RunDll32.exe shell32.dll,Control_RunDLL

Date and Time Properties

RunDll32.exe shell32.dll,Control_RunDLL timedate.cpl

Display Settings

RunDll32.exe shell32.dll,Control_RunDLL access.cpl,,3

Device Manager

RunDll32.exe devmgr.dll DeviceManager_Execute

Folder Options - File Types

RunDll32.exe shell32.dll,Control_Options 2

Folder Options - General

RunDll32.exe shell32.dll,Options_RunDLL 0

Folder Options - Search

RunDll32.exe shell32.dll,Options_RunDLL 2

Folder Options - View

RunDll32.exe shell32.dll,Options_RunDLL 7

Forgotten Password Wizard

RunDll32.exe keymgr.dll,PRShowSaveWizardExW

Hibernate

RunDll32.exe powrprof.dll,SetSuspendState

Keyboard Properties

RunDll32.exe shell32.dll,Control_RunDLL main.cpl @1

Lock Screen

RunDll32.exe user32.dll,LockWorkStation

Mouse Properties

RunDll32.exe shell32.dll,Control_RunDLL main.cpl @0

Map Network Drive

RunDll32.exe shell32.dll,SHHelpShortcuts_RunDLL Connect

Network Connections

RunDll32.exe shell32.dll,Control_RunDLL ncpa.cpl

Power Options

RunDll32.exe Shell32.dll,Control_RunDLL powercfg.cpl

Regional Settings

RunDll32.exe shell32.dll,Control_RunDLL intl.cpl,,3

Stored Usernames and Passwords

RunDll32.exe keymgr.dll,KRShowKeyMgr

System Properties: Advanced

RunDll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,4

System Properties: Automatic Updates

RunDll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,5

Taskbar Properties

RunDll32.exe shell32.dll,Options_RunDLL 1

User Accounts

RunDll32.exe shell32.dll,Control_RunDLL nusrmgr.cpl

Windows Security Center

RunDll32.exe shell32.dll,Control_RunDLL wscui.cpl

Windows - About

RunDll32.exe SHELL32.DLL,ShellAboutW

Unplug/Eject Hardware

RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

Windows Firewall

RunDll32.exe shell32.dll,Control_RunDLL firewall.cpl

Wireless Network Setup

RunDll32.exe shell32.dll,Control_RunDLL NetSetup.cpl,@0,WNSW

Open Control Panel (All Items)

explorer.exe shell:::{21ec2020-3aea-1069-a2dd-08002b30309d}

Manage Wireless Networks

explorer.exe shell:::{1fa9085f-25a2-489b-85d4-86326eedcd87}

Sound Control Playback Tab

rundll32.exe shell32.dll,Control_RunDLLmmsys.cpl

Sound Control Sounds Tab

rundll32.exe shell32.dll,Control_RunDLLmmsys.cpl,,2

Sound Control Recording Tab

rundll32.exe shell32.dll,Control_RunDLLmmsys.cpl,,1

Add/Remove Programs

rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl

Add/Remove Windows Components

rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,2

Set Program Access and Computer Defaults

rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,3

People Near Me

rundll32.exe shell32.dll,Control_RunDLL collab.cpl

People Near Me Sign In Tab

rundll32.exe shell32.dll,Control_RunDLL collab.cpl,,1

Screan Resolution

rundll32.exe shell32.dll,Control_RunDLL desk.cpl

Personalization

rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2

Screen Saver

rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1

Windows Firewall

rundll32.exe shell32.dll,Control_RunDLL firewall.cpl

Device Manager

rundll32.exe shell32.dll,Control_RunDLL hdwwiz.cpl

Power Options

rundll32.exe shell32.dll,Control_RunDLL powercfg.cpl

Power Options Change Plan Settings

rundll32.exe shell32.dll,Control_RunDLL powercfg.cpl,,1

System Properties

rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl

System Properties Hardware Tab

rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,2

System Properties Advanced Tab

rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3

System Properties System Protection Tab

rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,4

System Properties Remote Tab

rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,5

Pen and Touch Tablet PC Settings

rundll32.exe shell32.dll,Control_RunDLL tabletpc.cpl

Pen and Touch Tablet PC Settings Flicks Tab

rundll32.exe shell32.dll,Control_RunDLL tabletpc.cpl,,1

Pen and Touch Tablet PC Settings Handwriting Tab

rundll32.exe shell32.dll,Control_RunDLL tabletpc.cpl,,2

Phone and Modem Options

rundll32.exe shell32.dll,Control_RunDLL telephon.cpl

Phone and Modem Options Modems Tab

rundll32.exe shell32.dll,Control_RunDLL telephon.cpl,,1

Phone and Modems Options Advanced Tab

rundll32.exe shell32.dll,Control_RunDLL telephon.cpl,,2

Date and Time

rundll32.exe shell32.dll,Control_RunDLL timedate.cpl

Date and Time Additional Clocks

rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,1

Action Center

rundll32.exe shell32.dll,Control_RunDLL wscui.cpl

Unplug/Eject Hardware

RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

Internet Explorer Specific Commands

Delete Temporary Internet Files:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Delete Cookies:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

Delete History:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

Delete Form Data:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

Delete Passwords:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

Delete All:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

Delete All + files and settings stored by Add-ons:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351

 

本文作者专著《Visual C++2010开发权威指南》即将推出,敬请关注,Visual C++2010最近技术,Windows7开发最新技术!

 

欢迎技术交流!


原文链接: http://blog.csdn.net/yincheng01/article/details/5416948

转载于:https://my.oschina.net/junwong/blog/48129

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值