Adding desktop shortcut programmtically in Windows

I was tinkering of how I can put a desktop shortcut of my program and I thought this is one of those things I still have to learn. So, I wander into the internet to find a most simple example and read lots of articles and discussion and somehow I find it daunting to read all those codes where all I want is to create a simple desktop shortcut. After a lot of reading and experimentation I finally have the code below.

#include<windows.h>
#include<shlobj.h>

 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
    createShortcut();
    return 0;
}

void createShortcut()
{
    HRESULT hr;
    ::CoInitialize(NULL);

IShellLink *pShellLink;
hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,IID_IShellLink, (void**)&pShellLink);
hr = pShellLink->SetPath("C:\\MyProjects\\OCXTest\\Debug\\OCXTest.exe");  // Path to the object we are referring to
hr = pShellLink->SetDescription("This is a test!!");
hr = pShellLink->SetIconLocation("C:\\MyProjects\\OCXTest\\Debug\\ocxtest.ico",0);

IPersistFile *pPersistFile;
hr = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);

WCHAR wszLinkPath[MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, "C:\\Documents and Settings\\Earl\\Desktop\\OcxTest.lnk", -1, wszLinkPath, MAX_PATH);

hr = pPersistFile->Save(wszLinkPath, TRUE);
hr = pPersistFile->Release();
hr = pShellLink->Release();

::CoUninitialize();
}


Comments

Popular posts from this blog

Creating tray application and popup context menu in Windows

How to statically link applications to wxWidgets via Visual Studio 2008 and in Linux

How to put an image to wxPanel using wxWidgets