首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

从快捷方式读取文件路径,以及钩子?解决思路

2012-03-19 
从快捷方式读取文件路径,以及钩子?? - C++ Builder / Windows SDK/API1.做一东西,需要从Recent文件夹读取

从快捷方式读取文件路径,以及钩子?? - C++ Builder / Windows SDK/API
1.做一东西,需要从Recent文件夹读取最近打开的文档,但不知道怎么从快捷方式中读取真实的文件路径?
或根据一文件名读出文件的真实路径。
比如:1.txt;路径为C:\123\1.txt
试图使用GetModuleFileName来读取,但发现此函数只能读取exe或dll的路径;
GetFullPathName读出的也只是当前程序的路径;

2.用钩子能否监视最新打开的进程,应用程序?
可以的话,该怎样去作?

使用API!


[解决办法]
http://download.csdn.net/source/727585
[解决办法]
想down又没分……呜呜……

不过自己有写过,不是很完美,LZ上msdn找一下吧,关键字:IShellLinkW,IPersistFile,CoCreateInstance
[解决办法]
1.做一东西,需要从Recent文件夹读取最近打开的文档,但不知道怎么从快捷方式中读取真实的文件路径? 

从注册表下手, 应该有完整的路径.
[解决办法]
以下是一个简单例子:

C/C++ code
#define NO_WIN32_LEAN_AND_MEAN#include <shlobj.hpp>#include <vcl.h>// 以上三行放在.cpp文件最顶部//---------------------------------------struct TShortcutCfg{    TShortcutCfg()    {        nShowCmd = SW_SHOWNORMAL;        wHotKey = 0;        nIconIndex = 0;    }    // 结构成员:    AnsiString strShortcutName; //    AnsiString strLnkDir; //    AnsiString strDestFile; //    AnsiString strArguments; //    AnsiString strIconFile; //    int nIconIndex; //    AnsiString strWorkingDir; //    AnsiString strDescription; //    WORD wHotKey; //    int nShowCmd; //};//---------------------------------------// 读取快捷方式的信息// Example:// TShortcutCfg scShortcut;// if (ReadShortcut(&scShortcut, "C:\\WINDOWS\\SENDTO\\Proton.lnk"))// ShowMessage("快捷方式 " + scShortcut.strShortcutName +// " 的目标文件是 " + scShortcut.strDestFile);//---------------------------------------bool ReadShortcut(TShortcutCfg *scConfig, AnsiString strFileName){    bool bReturn = true;    wchar_t wszBuf [MAX_PATH];    char szBuf[MAX_PATH];    IShellLink *pShellLink;    strFileName.WideChar(wszBuf, MAX_PATH);    if (bReturn)    {        bReturn = (CoInitialize(NULL) == S_OK);        if (bReturn)        {            bReturn = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,                    IID_IShellLink, (void **)&pShellLink) >= 0;            if (bReturn)            {                IPersistFile *ppf;                bReturn = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;                if (bReturn)                {                    bReturn = ppf->Load(wszBuf, TRUE) >= 0;                    if (bReturn)                    {                        pShellLink->GetPath(szBuf, MAX_PATH, NULL, 0);                        scConfig->strDestFile = AnsiString(szBuf);                        pShellLink->GetArguments(szBuf, MAX_PATH);                        scConfig->strArguments = AnsiString(szBuf);                        // 图标                        int nIconIndex = 0;                        if (pShellLink->GetIconLocation(szBuf, MAX_PATH, &nIconIndex) == NOERROR)                        {                            scConfig->strIconFile = AnsiString(szBuf);                            scConfig->nIconIndex = nIconIndex >= 0 ? nIconIndex : 0;                        }                        // 起始位置                        pShellLink->GetWorkingDirectory(szBuf, MAX_PATH);                        scConfig->strWorkingDir = AnsiString(szBuf);                        // 备注                        pShellLink->GetDescription(szBuf, MAX_PATH);                        scConfig->strDescription = AnsiString(szBuf);                        // 快捷键                        pShellLink->GetHotkey(&scConfig->wHotKey);                        // 显示方式                        pShellLink->GetShowCmd(&scConfig->nShowCmd);                    }                    ppf->Release ();                }                pShellLink->Release ();            }            CoUninitialize();        }        if (bReturn)        {            scConfig->strLnkDir = IncludeTrailingBackslash(ExtractFilePath(strFileName));            strFileName = ExtractFileName(strFileName);            scConfig->strShortcutName = strFileName.Length() ?                    strFileName.SubString(1, strFileName.Length() -                    ExtractFileExt(strFileName).Length()) : EmptyStr;        }    }    return bReturn;}//---------------------------------------// 获取特殊目录的真实路径AnsiString __fastcall GetSpecialFolder(int nFolder){    LPITEMIDLIST pidl;    LPMALLOC pShellMalloc;    char szDir[MAX_PATH];    AnsiString strSpacialFolder;    if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))    {        if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, nFolder, &pidl)))        {            if (SHGetPathFromIDList(pidl, szDir))            {                strSpacialFolder = AnsiString(szDir);                if (!strSpacialFolder.IsPathDelimiter(strSpacialFolder.Length()))                    strSpacialFolder = AnsiString(szDir) + "\\";            }            pShellMalloc->Free(pidl);        }        pShellMalloc->Release();    }    return strSpacialFolder;}//---------------------------------------// 列举特殊目录下所有的项目int __fastcall GetAllItemFromDir(int nFolder, TStrings *pList){    String strPath = GetSpecialFolder(nFolder);    WIN32_FIND_DATA wfd;    int nCount = 0;    HANDLE hFile = FindFirstFile((strPath + "*.*").c_str(), &wfd);    if (hFile)    {        if (strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName, ".."))        {            pList->Add(wfd.cFileName);            nCount++;        }        while (FindNextFile(hFile, &wfd))        {            if (strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName, ".."))            {                pList->Add(wfd.cFileName);                nCount++;            }        }    }    FindClose(hFile);    return nCount;}//---------------------------------------// 使用举例,列举RECENT目录所有项目到ListBox中并将RECENT目录的真实路径显示在Edit中void __fastcall TForm1::Button1Click(TObject *Sender){    Edit1->Text = GetSpecialFolder(CSIDL_RECENT);    GetAllItemFromDir(CSIDL_RECENT, ListBox1->Items);}//---------------------------------------// 双击ListBox的某一项,显示该快捷方式的目标文件信息void __fastcall TForm1::ListBox1DblClick(TObject *Sender){    if (ListBox1->ItemIndex > -1)    {        TShortcutCfg scShortcut;        if (ReadShortcut(&scShortcut, Edit1->Text                + ListBox1->Items->Strings[ListBox1->ItemIndex]))            ShowMessage(String().sprintf(                    "快捷方式:%s\r\n目标文件:%s",                    scShortcut.strShortcutName,                    scShortcut.strDestFile));    }} 

热点排行