🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6949

Uninstalling shortcuts

#6949
From: Jeff Kish
Date: Mon, 28 Mar 2005 21:34
152 lines
5464 bytes
Greetings.

I am using some code to create shortcuts.
The shortcuts will install on w2000 or nt4 most likely.

I don't write anything explicitly to the registry to keep track of
things though.

I would like to, when the user uninstalls the application, 
to remove the shortcuts.

The only caveat I guess is that they need to supply the original
installation information (install location and whether startmenu or
desktop shortcuts were installed).. I know the files accessed by the
shortcuts then.

So can someone explain the proper way to make my cbuilder installation
program remove shortcuts?


I'm not sure if I need to use some sort of ipersistfile.delete call or
what.

The code I am using to create them in the first place is below...

Thanks
Jeff

bool __fastcall TFormIPCInstall::CreateShortcutLink(vAnsiString &
inVErrors,
    AnsiString LinkDesc,//"MyDemo"
    AnsiString LinkPath, //this is sprintf(path,
//"%s\\%s.lnk",
//DesktopPath.c_str()
//where DesktopPath is GetSpecialFolder(CSIDL_DESKTOP);
    AnsiString TargetPath, //target file to activate via shortcut
    AnsiString TargetArgs,//null
    AnsiString WorkingDirectory) //null
{     
// Must be done before any other calls to make it work.
    CoInitialize(NULL);

bool retval = false;
HRESULT hres;
IShellLink* psl = NULL;
    SetLastError(0);

LPCLASSFACTORY pcf = NULL;
    hres = CoGetClassObject(  CLSID_ShellLink,
                              CLSCTX_INPROC_SERVER,
                              NULL,
                              IID_IClassFactory,
                              (LPVOID*)&pcf);

    if(SUCCEEDED(hres))
    {
        hres = pcf->CreateInstance(
                              NULL,
                              IID_IShellLink,
                              (LPVOID*)&psl);

    pcf->Release();
    }
    if(SUCCEEDED(hres))
    {
    IPersistFile* ppf = NULL;
    // Set the path to the shortcut target, and add the description.
        psl->SetPath(TargetPath.c_str()); /* typically:
"d:\\ipc2.2p6\\ini\\ipc.bat"*/
        psl->SetArguments(TargetArgs.c_str()); /* typically: NULL */
        psl->SetDescription(LinkDesc.c_str()); /*  typically:
d:\\program files\\msx411\\maxcust.dll */
        if (WorkingDirectory.Length() < 1)
        {

psl->SetWorkingDirectory(ExtractFilePath(TargetPath).c_str());
        }
        else
        {
            psl->SetWorkingDirectory(WorkingDirectory.c_str());
        }
        // Query IShellLink for the IPersistFile interface for saving
the
        // shortcut in persistent storage.
        hres = psl->QueryInterface(IID_IPersistFile, &(void*)ppf);

        if(SUCCEEDED(hres))
        {
        wchar_t wsz[MAX_PATH];
        // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, LinkPath.c_str(), -1, wsz,
                              MAX_PATH);
        // Save the link by calling IPersistFile::Save.
        hres = ppf->Save(wsz, TRUE);
        ppf->Release();
        }
        psl->Release();
        retval = SUCCEEDED(hres);
    }
AnsiString AsErrorPrefix = "Error Creating Shortcut " + LinkDesc;
AnsiString asReturnedErrorInfo = "";
    switch(hres)
    {
    case S_OK:
    //ShowMessage("Link was successfully created");
    break;
    case REGDB_E_CLASSNOTREG:
    asReturnedErrorInfo = AsErrorPrefix + "A specified class is not
registered in the registration database";
    ShowMessage(asReturnedErrorInfo);
    break;
    case E_OUTOFMEMORY:
    asReturnedErrorInfo = AsErrorPrefix + "Out of memory";
    ShowMessage(asReturnedErrorInfo);
    break;
    case E_INVALIDARG:
    asReturnedErrorInfo = AsErrorPrefix + "One or more arguments are
invalid";
    ShowMessage(asReturnedErrorInfo);
    break;
    case E_UNEXPECTED:
    asReturnedErrorInfo = AsErrorPrefix + "An unexpected error
occurred";
    ShowMessage(asReturnedErrorInfo);
    break;
    case CLASS_E_NOAGGREGATION:
    asReturnedErrorInfo = AsErrorPrefix + "Class cannot be created as
part";
    ShowMessage(asReturnedErrorInfo);
    break;
    case E_FAIL:
    asReturnedErrorInfo = AsErrorPrefix + "Could not create link";
    ShowMessage(asReturnedErrorInfo);
    break;
    case E_NOTIMPL:
    asReturnedErrorInfo = AsErrorPrefix + "Interface not implemented";
    ShowMessage(asReturnedErrorInfo);
    break;
    default:
    asReturnedErrorInfo = AsErrorPrefix + "Unknown error";
    ShowMessage(asReturnedErrorInfo);
    }

    CoUninitialize();
    if (asReturnedErrorInfo.Length() > 0)
    {
        inVErrors.push_back(asReturnedErrorInfo);
    }
    return retval;
}

Jeff Kish
Technical Details
Message-ID: <4gtg41hmhnj38nqcel689ans5t7mqnucpu@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cb471b520f9dc8bd,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Jeff Kish <jeff.kish@mro.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Uninstalling shortcuts
Date: Mon, 28 Mar 2005 16:34:53 -0500
Message-ID: <4gtg41hmhnj38nqcel689ans5t7mqnucpu@4ax.com>
X-Newsreader: Forte Agent 2.0/32.652
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 12.106.157.130
X-Trace: newsgroups.borland.com 1112045304 12.106.157.130 (28 Mar 2005 13:28:24 -0800)
Lines: 152
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail