网页功能: 加入收藏 设为首页 网站搜索  
Windows“快捷方式”文件的数据读取
发表日期:2007-01-16作者:[转贴] 出处:  


此代码可以对Windows“快捷方式”的读取。

#include <windowsx.h>
#include <objbase.h>
#include <shlobj.h>
#include <stdio.h>
#include <initguid.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>

main(int ac, char *av[])
{
    if (ac != 2)
    {
        printf("Syntax: ln <pathname>\n");
        return 0;
    }

    IShellLink *psl; // pointer to IShellLink i/f
    HRESULT hres;
    WIN32_FIND_DATA wfd;
    char szGotPath[MAX_PATH];

    // Get pointer to the IShellLink interface.
    hres = CoInitialize(NULL);
    if (!SUCCEEDED(hres))
    printf("Could not open the COM library\n");

    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
    IID_IShellLink, (LPVOID *)&psl);

    if (SUCCEEDED(hres))
    {
        // Get pointer to the IPersistFile interface.

        IPersistFile *ppf;
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);

        if (SUCCEEDED(hres))
        {
            WORD wsz[MAX_PATH];

            // Ensure string is Unicode.
            MultiByteToWideChar(CP_ACP, 0, av[1], -1, wsz, MAX_PATH);

            // Load the shell link
            hres = ppf->Load(wsz, STGM_READ);
            if (SUCCEEDED(hres))
            {
                // Resolve the link.

                hres = psl->Resolve(0, SLR_ANY_MATCH);
                // ^
                // Using 0 instead -| of hWnd, as hWnd is only used if
                // interface needs to prompt for more information. Should use
                // hWnd from current console in the long run.

                if (SUCCEEDED(hres))
                {
                    strcpy(szGotPath, av[1]);

                    hres = psl->GetPath(szGotPath, MAX_PATH,
                    (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH );
                    if (!SUCCEEDED(hres))
                    printf("GetPath failed!\n");

                    printf("This points to %s\n", wfd.cFileName);
                    char buffer[256];
                    LPSTR buf = &buffer[0];
                    hres = psl->GetWorkingDirectory(buf, 256);
                    if (!SUCCEEDED(hres))
                    printf("GetWoringPath failed!\n");
                    printf("This points directory to %s\n", buf);
                    if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                    printf("This is a directory\n");
                }
            }
            else
                printf("IPersistFile Load Error\n");
            ppf->Release();
        }
        else
            printf("QueryInterface Error\n");
        psl->Release();
    }
    else
        printf("CoCreateInstance Error - hres = %08x\n", hres);
    return 0;
}
 
我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 Windows“快捷方式”文件的数据读取
本类热点文章
  如何用D3D/OpenGL在制作2D游戏中高效地..
  自己绘制True type font字体
  使用CPU时间戳进行高精度计时
  阵汉字显示
  程序员的超强外挂——Visual Assist .N..
  用VC++实现console程序显示彩色文本 wx..
  制做自己的点阵字库的方法和程序
  “变速齿轮”再研究
  一个简单的线程管理机制
  VC编程中常用快捷键
  Delpih中的Windows API编程初步
  BIG5到GB的转换技术
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00367