CreateShortCut

CreateShortCut

Useage:

#pragma lib <shell32.lib>
#pragma lib <ole32.lib>
#pragma lib <uuid.lib>
char ShellPath[MAX_PATH];
GetShellFolderPath("Desktop", ShellPath);
strcat(ShellPath, "\\");
strcat(ShellPath, "\\Shortcut to BOOTLOG.TXT.lnk");
CreateShortCut("C:\\BOOTLOG.TXT", ShellPath, 
            "This is a short cut to the file C:\\BOOTLOG.TXT");
BOOL CreateShortCut(char * pSourceFile, char * pDestination, char * pDesc)
{
    HRESULT hResult = 0;
    IShellLink * pShellLink = NULL;
    BOOL rc = FALSE;

    hResult = CoInitialize(NULL);

    // Initialize the IShellLink interface.
    hResult = CoCreateInstance(&CLSID_ShellLink, NULL,
        CLSCTX_INPROC_SERVER, &IID_IShellLink, (void**) &pShellLink);

    // Get a pointer to the IShellLink Interface
    if (SUCCEEDED(hResult)) {
        IPersistFile* ppf = NULL;
        pShellLink->lpVtbl->SetPath(pShellLink, pSourceFile);

        // Set the path to the shortcut target
        hResult = pShellLink->lpVtbl->SetDescription(pShellLink, pDesc);

        // Add Description
        hResult = pShellLink->lpVtbl->QueryInterface(pShellLink, 
                        &IID_IPersistFile, (void**) &ppf);

        // Query IShellLink for the IPersistFile interface for saving the
        // shortcut in persistent storage.
        if (SUCCEEDED(hResult)){
            WORD wszWideString[MAX_PATH];

            // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, pDestination, -1, wszWideString, MAX_PATH);

            hResult = ppf->lpVtbl->Save(ppf, wszWideString, TRUE);

            // Save the link by calling IPersistFile::Save. ppf->Release();
            rc = TRUE;
        }
    pShellLink->lpVtbl->Release(pShellLink);
    CoUninitialize();
    // Clean up
    }
    return rc;
}

Back to main page