• C

Create a Desktop Shortcut for an program in C

Hello All. Is it possible to write a C program to create a desktop shortcut for a specific static executable? I am trying to package some software for end users and want to have a program automatically create a shortcut on the desktop for the program that just installed in the package.

Thanks a lot.
waugerAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
BTW, here's a complete program that does it for you:

#include <windows.h>
#include <windowsx.h>
#include <objbase.h>
#include <shlobj.h>
#include <stdio.h>
#include <initguid.h>
#include <string.h>

main(int ac, char *av[])
    {
    IShellLink *psl;
    HRESULT hres;
    WIN32_FIND_DATA wfd;
    char szGotPath[MAX_PATH];
    IPersistFile *ppf;

    if (ac != 2)
        {
        printf("Syntax: ln <pathname>\n");
        return 0;
        }

    hres = CoInitialize(NULL);
    if (!SUCCEEDED(hres))
        printf("Could not open the COM library\n");

    hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                                &IID_IShellLink, (LPVOID *)&psl);
    if (SUCCEEDED(hres))
        {
        hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);

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

            MultiByteToWideChar(CP_ACP, 0, av[1], -1, wsz, MAX_PATH);

            hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
            if (SUCCEEDED(hres))
                {
                hres = psl->lpVtbl->Resolve(psl, 0, SLR_ANY_MATCH);

                if (SUCCEEDED(hres))
                   {
                   strcpy(szGotPath, av[1]);

                   hres = psl->lpVtbl->GetPath(psl, szGotPath, MAX_PATH,
                               (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH );
                   if (!SUCCEEDED(hres))
                       printf("GetPath failed!\n");

                   printf("This points to %s\n", wfd.cFileName);
                   if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                       printf("This is a directory\n");
                   }
               }
           else
               printf("IPersistFile Load Error\n");
           ppf->lpVtbl->Release(ppf);
           }
       else
           printf("QueryInterface Error\n");
       psl->lpVtbl->Release(psl);
       }
   else
       printf("CoCreateInstance Error - hres = %08x\n", hres);
   return 0;
   }


==================================
C++ version
==================================

#include <windowsx.h>
#include <objbase.h>
#include <shlobj.h>
#include <stdio.h>
#include <initguid.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>


// This program should print out whether the file is a link and where it
// points to and whether it is a directory or not.
//

main(int ac, char *av[])
    {
    if (ac != 2)
        {
        printf("Syntax: ln <pathname>\n");
        return 0;
        }

    IShellLink *psl;                            // pointer to IShellLink i/f
    HRESULT hres;
    WIN32_FIND_DATA wfd;
    char szGotPath[MAX_PATH];

    // Get pointer to the IShellLink interface.

    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                                IID_IShellLink, (LPVOID *)&psl);

    if (SUCCEEDED(hres))
        {
        // Get pointer to the IPersistFile interface.

        IPersistFile *ppf;
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);

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

            // Ensure string is Unicode.
            MultiByteToWideChar(CP_ACP, 0, av[1], -1, wsz, MAX_PATH);

            // Load the shell link
            hres = ppf->Load(wsz, STGM_READ);
            if (SUCCEEDED(hres))
                {
                // Resolve the link.

                hres = psl->Resolve(0, SLR_ANY_MATCH);
                //                  ^
                // Using 0 instead -| of hWnd, as hWnd is only used if
                // interface needs to prompt for more information. Should use
                // hWnd from current console in the long run.

                if (SUCCEEDED(hres))
                    {
                    strcpy(szGotPath, av[1]);

                    hres = psl->GetPath(szGotPath, MAX_PATH,
                                (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH );
                    if (!SUCCEEDED(hres))
                        printf("GetPath failed!\n");

                    printf("This points to %s\n", wfd.cFileName);
                    if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                        printf("This is a directory\n");
                    }
                }
            else
                printf("IPersistFile Load Error\n");
            ppf->Release();
            }
        else
            printf("QueryInterface Error\n");
        psl->Release();
        }
    else
        printf("CoCreateInstance Error - hres = %08x\n", hres);
    return 0;
    }

You can find the original archive at http://www.wotsit.org/download.asp?f=lnk


0
 
jkrCommented:
BTW, there's more sample code available - check http://www.microsoft.com/msj/defaulttop.asp?page=/msj/archive/s2bd.htm ("Fusing Your Applications to the System Through the Windows95 Shell")
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
waugerAuthor Commented:
Thanks. But I really dont want to use the Windows Shell environment if possible. Is there another way? Cant I just write a plan C program and compile a small executable to create a desktop shortcut? Thanks agian.
0
 
jkrCommented:
>>Cant I just write a plan C program and compile a small executable to create a desktop shortcut?

That is what I am suggesting. However, this C program will have to use the shell's API do achieve that.
0
 
waugerAuthor Commented:
That is perfect. Thanks a lot. But I tried compiling the C code and got multiple errors. Any ideas? Thanks
0
 
jkrCommented:
>>Any ideas?

Yes. Post the errors - I forgot to clean my crystal ball :o)
0
 
waugerAuthor Commented:
Oh, yea. That would help. Here you go....

shortcut.c: In function `main':
shortcut.c:23: parse error before `*'
shortcut.c:30: `hres' undeclared (first use in this function)
shortcut.c:30: (Each undeclared identifier is reported only once
shortcut.c:30: for each function it appears in.)
shortcut.c:31: `psl' undeclared (first use in this function)
shortcut.c:31: incompatible type for argument 1 of `CoCreateInstance'
shortcut.c:31: incompatible type for argument 4 of `CoCreateInstance'
shortcut.c:48: structure has no member named `Load'
shortcut.c:61: `szGotPath' undeclared (first use in this function)
shortcut.c:64: `wfd' undeclared (first use in this function)
shortcut.c:75: structure has no member named `Release'
0
 
jkrCommented:
That is weird - did you use the code from the link or copy the code from my above comment?
0
 
waugerAuthor Commented:
oh, i just copied it from your comment. I should try the link first.
0
 
jonninCommented:
er, could you not just make a shortuct to it to distribute, then open that with your program and change where it points to??  Its a binary file but the path part is in text, it should be an easy, quick way to do this.

not a great way to do things, but that way you don't need os specific headers or anything, if its still giving you trouble and you need a fix now it may help...





0
 
peterchen092700Commented:
No comment has been added lately.
To cleanup this TA, I will recommend to moderators that this question is:

*** Answered by: jkr ***

Please leave any comments here within the next seven days, or finalize the question if you own it
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
peterchen
EE Cleanup Volunteer
0
 
Kent OlsenData Warehouse Architect / DBACommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept jkr's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.