windows创建快捷方式和快捷网站链接

#include "shobjidl.h"
#include "shlobj.h"

HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc) 

    HRESULT hres; 
    IShellLink* psl; 

    // Get a pointer to the IShellLink interface. 
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, 
                            IID_IShellLink, (LPVOID*)&psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 

        // Set the path to the shortcut target and add the description. 
        psl->SetPath(lpszPathObj); 
        psl->SetDescription(lpszDesc); 

        // Query IShellLink for the IPersistFile interface for saving the 
        // shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 

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

            // Ensure that the string is Unicode. 
            MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); 

            // TODO: Check return value from MultiByteWideChar to ensure 
                     success.

            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(wsz, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    } 
    return hres; 
}

网址简单,就一句话:


WritePrivateProfileString("InternetShortcut","URL",lpszLink,lpszPath);

使用该函数需要注意的是lpszPath的后缀是url,最终生成的链接文件的格式如下

[InternetShortcut]

URL=http://www.baidu.com

你可能感兴趣的:(C++,windows快捷方式创建)