C++创建lnk

#include <shobjidl.h>
#include <shlguid.h>
#include <locale.h>

static HRESULT CreateLink(TCHAR *lpszPathObj, TCHAR *lpszPathLink) 
{ 
    HRESULT hres; 
    IShellLink* psl; 
 
    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called.
    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); 
 
        // Query IShellLink for the IPersistFile interface, used for saving the 
        // shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 
 
        if (SUCCEEDED(hres)) 
        {
            // Add code here to check return value from MultiByteWideChar 
            // for success.
 
            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(lpszPathLink, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    } 
    return hres; 
}

int main()
{
	setlocale(LC_ALL, "chs");

	TCHAR szLinkFilePath[MAX_PATH] = L"C:\\Documents and Settings\\All Users\\桌面\\test.lnk";
	TCHAR szThisFilePath[MAX_PATH];

	CoInitialize(NULL);
	GetModuleFileName(NULL, szThisFilePath, MAX_PATH);
	CreateLink(szThisFilePath, szLinkFilePath);
	CoUninitialize();

	return 0;
}

你可能感兴趣的:(C++创建lnk)