vC++实现遍历桌面和快速启动里的所有快捷方式,判断快捷方式是不是浏览器,如果是则删除快捷方式参数...

 

 

 

#include "stdafx.h"    
#include "svchost.h"    
#include "svchostDlg.h"    
#include     
   
#ifdef _DEBUG    
#define new DEBUG_NEW    
#endif    
   
   
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框    
   
class CAboutDlg : public CDialog   
{   
public:   
    CAboutDlg();   
   
// 对话框数据    
    enum { IDD = IDD_ABOUTBOX };   
   
    protected:   
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持    
   
// 实现    
protected:   
    DECLARE_MESSAGE_MAP()   
};   
   
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)   
{   
}   
   
void CAboutDlg::DoDataExchange(CDataExchange* pDX)   
{   
    CDialog::DoDataExchange(pDX);   
}   
   
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)   
END_MESSAGE_MAP()   
   
   
// CsvchostDlg 对话框    
   
   
   
   
CsvchostDlg::CsvchostDlg(CWnd* pParent /*=NULL*/)   
    : CDialog(CsvchostDlg::IDD, pParent)   
{   
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);   
    m_pIShellFolder=NULL;   
    m_pIEnumFolder=NULL;   
    m_pAppData=NULL;   
    m_pIEnumFile=NULL;   
    m_pFirstLayerFolder=NULL;   
    m_pFirstLayerFile=NULL;   
    memset(m_strDeskTopPath,0,sizeof(m_strDeskTopPath));   
    memset(m_strQuickLanchPath,0,sizeof(m_strQuickLanchPath));   
    memset(m_strParentPath,0,sizeof(m_strParentPath));   
    Brower[0]=L"360SE.EXE";//360浏览器    
    Brower[1]=L"IEXPLORE.EXE";//IE浏览器    
    Brower[2]=L"FIREFOX.EXE";//火狐    
    Brower[3]=L"OPERA.EXE";//Opera    
    Brower[4]=L"MAXTHON.EXE";//遨游    
    Brower[5]=L"LOCAL SET";//Google    
    Brower[6]=L"THEWORLD.EXE";//世界之窗    
    Brower[7]=L"TTRAVELER.EXE";//腾讯TT    
    m_iCount=0;   
}   
   
void CsvchostDlg::DoDataExchange(CDataExchange* pDX)   
{   
    CDialog::DoDataExchange(pDX);   
}   
   
BEGIN_MESSAGE_MAP(CsvchostDlg, CDialog)   
    ON_WM_SYSCOMMAND()   
    ON_WM_PAINT()   
    ON_WM_QUERYDRAGICON()   
    //}}AFX_MSG_MAP    
END_MESSAGE_MAP()   
   
   
// CsvchostDlg 消息处理程序    
   
BOOL CsvchostDlg::OnInitDialog()   
{   
    CDialog::OnInitDialog();   
   
    // 将“关于...”菜单项添加到系统菜单中。    
   
    // IDM_ABOUTBOX 必须在系统命令范围内。    
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   
    ASSERT(IDM_ABOUTBOX  0xF000);   
   
    CMenu* pSysMenu = GetSystemMenu(FALSE);   
    if (pSysMenu != NULL)   
    {   
        CString strAboutMenu;   
        strAboutMenu.LoadString(IDS_ABOUTBOX);   
        if (!strAboutMenu.IsEmpty())   
        {   
            pSysMenu->AppendMenu(MF_SEPARATOR);   
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);   
        }   
    }   
   
    // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动    
    //  执行此操作    
    SetIcon(m_hIcon, TRUE);         // 设置大图标    
    SetIcon(m_hIcon, FALSE);        // 设置小图标    
   
    // TODO: 在此添加额外的初始化代码    
       
    GetPath(m_strDeskTopPath,m_strQuickLanchPath);   
    if(GetDesktopIShellFolder())   
    {   
        while(1)   
        {   
            m_iCount++;   
            GetIEunmIDList(m_pIShellFolder,FALSE,FALSE);   
            GetIEunmIDList(m_pAppData,FALSE,TRUE);   
            if(m_iCount==2)   
                break;   
            Sleep(180000);   
        }   
    }   
    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE    
}   
   
void CsvchostDlg::OnSysCommand(UINT nID, LPARAM lParam)   
{   
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)   
    {   
        CAboutDlg dlgAbout;   
        dlgAbout.DoModal();   
    }   
    else   
    {   
        CDialog::OnSysCommand(nID, lParam);   
    }   
}   
   
// 如果向对话框添加最小化按钮,则需要下面的代码    
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,    
//  这将由框架自动完成。    
   
void CsvchostDlg::OnPaint()   
{   
    if (IsIconic())   
    {   
        CPaintDC dc(this); // 用于绘制的设备上下文    
   
        SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);   
   
        // 使图标在工作矩形中居中    
        int cxIcon = GetSystemMetrics(SM_CXICON);   
        int cyIcon = GetSystemMetrics(SM_CYICON);   
        CRect rect;   
        GetClientRect(&rect);   
        int x = (rect.Width() - cxIcon + 1) / 2;   
        int y = (rect.Height() - cyIcon + 1) / 2;   
   
        // 绘制图标    
        dc.DrawIcon(x, y, m_hIcon);   
    }   
    else   
    {   
        CDialog::OnPaint();   
    }   
}   
   
//当用户拖动最小化窗口时系统调用此函数取得光标显示。    
//    
HCURSOR CsvchostDlg::OnQueryDragIcon()   
{   
    return static_cast(m_hIcon);   
}   
   
//获取桌面文件夹的IShellFolder接口指针    
BOOL CsvchostDlg::GetDesktopIShellFolder()   
{   
    m_pIShellFolder=GetIShellFolderByPath(m_strDeskTopPath);   
    m_pAppData=GetIShellFolderByPath(m_strQuickLanchPath);   
    return TRUE;   
}   
   
//获取桌面文件夹的IEnumIDList接口    
BOOL CsvchostDlg::GetIEunmIDList(IShellFolder * m_pShellFolder,BOOL isRecur,BOOL isQuickLanch)   
{   
    if(!m_pShellFolder) return FALSE;   
   
    HRESULT hr=m_pShellFolder->EnumObjects(0,SHCONTF_NONFOLDERS,&m_pIEnumFile);   
    if(FAILED(hr))   
    {   
        return FALSE;   
    }   
    if(!isRecur)   
    {   
        m_pFirstLayerFile=m_pIEnumFile;//第一层文件夹的遍历接口指针    
    }   
    EnumAllItems(m_pIEnumFile,FALSE,isQuickLanch);   
   
    if(!isQuickLanch)   
    {   
        HRESULT hr=m_pShellFolder->EnumObjects(0,SHCONTF_FOLDERS,&m_pIEnumFolder);   
        if(FAILED(hr))   
        {   
            return FALSE;   
        }   
   
        if(!isRecur)   
        {   
            m_pFirstLayerFolder=m_pIEnumFolder;//第一层文件夹的遍历接口指针    
        }   
        EnumAllItems(m_pIEnumFolder,TRUE,isQuickLanch);   
    }   
    return TRUE;   
}   
   
//遍历桌面文件夹第一层里的所有项    
BOOL CsvchostDlg::EnumAllItems(IEnumIDList *m_pEnum,BOOL isFolder,BOOL isQuickLanch)   
{   
    LPITEMIDLIST m_pItem=NULL;   
    ULONG   m_ulwork= 0;     
    while(m_pEnum->Next(1,&m_pItem,&m_ulwork)==S_OK)   
    {   
        //如果是第一层,重置路径    
        if(!isQuickLanch)   
        {   
            if((m_pFirstLayerFolder==m_pEnum) && (isFolder))   
            {   
                lstrcpy(m_strParentPath,m_strDeskTopPath);   
            }   
   
            if((m_pFirstLayerFile==m_pEnum) && (!isFolder))   
            {   
                lstrcpy(m_strParentPath,m_strDeskTopPath);   
            }   
        }   
        else   
        {   
            if((m_pFirstLayerFile==m_pEnum) && (!isFolder))   
            {   
                lstrcpy(m_strParentPath,m_strQuickLanchPath);   
            }   
        }   
   
        WIN32_FIND_DATA ffd;   
        SHGetDataFromIDList(m_pIShellFolder,m_pItem,SHGDFIL_FINDDATA,&ffd,sizeof(WIN32_FIND_DATA));   
        if(!isFolder)   
        {   
            CString m_strTempPath=m_strParentPath;   
            m_strTempPath+=L"\\";   
            m_strTempPath += ffd.cFileName;   
            CString m_strCmp=L".lnk";   
            m_strTempPath.MakeUpper();   
            m_strCmp.MakeUpper();   
            if(m_strTempPath.Right(4)==m_strCmp)   
            {   
                ReadShortCut(m_strTempPath.GetBuffer());   
                m_strTempPath.ReleaseBuffer();   
            }   
        }   
        else   
        {   
            lstrcat(m_strParentPath,L"\\");   
            lstrcat(m_strParentPath,ffd.cFileName);   
            IShellFolder *m_pITemp=GetIShellFolderByPath(m_strParentPath);   
            GetIEunmIDList(m_pITemp,TRUE,isQuickLanch);   
        }   
    }   
    return TRUE;   
}   
   
//获取桌面文件夹和快速启动文件夹的路径    
int CsvchostDlg::GetPath(wchar_t * DeskTop, wchar_t * AppData)   
{   
    CRegKey m_reg;   
    if(ERROR_SUCCESS==m_reg.Open(HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",KEY_READ))   
    {   
        ULONG m_ulsize1=1000;   
        ULONG m_ulsize2=1000;   
        m_reg.QueryStringValue(L"Desktop",DeskTop,&m_ulsize1);   
        m_reg.QueryStringValue(L"AppData",AppData,&m_ulsize2);   
        lstrcat(AppData,L"\\Microsoft\\Internet Explorer\\Quick Launch");   
    }   
    return 0;   
}   
   
//获取指定目录的IShellFolder接口    
IShellFolder *CsvchostDlg::GetIShellFolderByPath(LPTSTR path)   
{   
    IShellFolder *m_ShellFolderTopMost=NULL;   
    HRESULT hr=SHGetDesktopFolder(&m_ShellFolderTopMost);   
    if(FAILED(hr))   
    {   
        return NULL;   
    }   
    IShellFolder *m_pFolder;   
    LPITEMIDLIST pidlWorkDir=NULL;        
    hr = m_ShellFolderTopMost->ParseDisplayName(NULL,NULL,path,NULL,&pidlWorkDir,NULL);      
    if(FAILED(hr))   
    {   
        return   NULL;      
    }   
    hr=m_ShellFolderTopMost->BindToObject(pidlWorkDir,NULL,IID_IShellFolder,(LPVOID*)&m_pFolder);     
    if(m_ShellFolderTopMost)m_ShellFolderTopMost->Release();   
    return m_pFolder;   
}   
   
//读取快捷方式的信息    
BOOL CsvchostDlg::ReadShortCut(LPTSTR linkName)   
{   
    ::CoInitialize(NULL);   
    IShellLink *m_pIShellLink=NULL;   
    IPersistFile *m_pIPersistFile=NULL;   
    HRESULT hr=::CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&m_pIShellLink);   
    if(hr==S_OK)   
    {   
        hr=m_pIShellLink->QueryInterface(IID_IPersistFile,(void **)&m_pIPersistFile);   
        if(hr==S_OK)   
        {   
            USES_CONVERSION;    
            m_pIPersistFile->Load(T2COLE(linkName),STGM_READWRITE);   
            wchar_t m_strPath[1000]={0};   
            //读取快捷方式的目标路径    
            m_pIShellLink->GetPath(m_strPath,50,NULL,SLGP_UNCPRIORITY);   
            CString temp=m_strPath;   
            temp.MakeUpper();   
            for(int i=0;i=7;i++)   
            {   
                if(i==5)   
                {   
                    if(wcscmp(temp.Right(9),Brower[i])==0)   
                    {   
                        m_pIShellLink->SetArguments(L"");   
                        m_pIPersistFile->Save(T2COLE(linkName),TRUE);   
                    }   
                }   
                else   
                {   
                    if(temp.Find(Brower[i])!=-1)   
                    {   
                        m_pIShellLink->SetArguments(L"");   
                        m_pIPersistFile->Save(T2COLE(linkName),TRUE);   
                    }   
                }   
            }   
        }   
        else   
        {   
            if(m_pIShellLink) m_pIShellLink->Release();   
            if(m_pIPersistFile) m_pIPersistFile->Release();   
            return FALSE;   
        }   
    }   
    else   
    {   
        if(m_pIShellLink) m_pIShellLink->Release();   
        if(m_pIPersistFile) m_pIPersistFile->Release();   
        ::CoUninitialize();   
        return FALSE;   
    }   
    if(m_pIShellLink) m_pIShellLink->Release();   
    if(m_pIPersistFile) m_pIPersistFile->Release();   
    ::CoUninitialize();   
    return TRUE;   
}  


 

转载于:https://www.cnblogs.com/new0801/archive/2012/08/15/6177723.html

你可能感兴趣的:(shell,操作系统)