会员: 密码:  免费注册 | 忘记密码 | 会员登录 网页功能: 加入收藏 设为首页 网站搜索  
游戏开发 > 程序设计 > 系统操作
Windows“快捷方式”文件的数据读取
发表日期:2007-01-16 17:28:54作者: 出处:  


此代码可以对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;
}
 
返回顶部】 【打印本页】 【关闭窗口

关于我们 / 给我留言 / 版权举报 / 意见建议 / 网站编程QQ群   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net 加载时间 0.00269