Solved

How do I create a shortcut in VC++

Posted on 1998-10-01
3
1,255 Views
Last Modified: 2008-03-17
I'd like to create a shortcut to an .exe file using VC++.
How?
0
Comment
Question by:labatti
3 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1174173
I've written a OCX myself which does this. If you like, i can post it to you.
But i think you'll find a generic wrapper class for shell links at:

www.codeguru.com
0
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 1174174
You can do this using the IShellLink interface:

Here is an example command line program the demonstrates creating a link with the
     IShellLink interface:


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

     //
     // Demonstrates making a link with the IShellLink Interface
     //
     void main(int argc, char *argv[])
     {
     HRESULT hres;
     IShellLink *psl;


     if(argc < 4){
     cerr << "Usage: MakeLink <file to link to> <link file> <description>\n";
     return;
     }
     else{
     cerr << "Making link with: " << argv[1] << " : " << argv[2] << " : " << argv[3] << "\n";

     }

     // Create an IShellLink object and get a pointer to the IShellLink
     // interface (returned from CoCreateInstance).
     hres = CoInitialize(NULL);
     if(!SUCCEEDED(hres)){
     cerr << "CoInitialize failed\n";
     return;
     }

     hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
     IID_IShellLink, (LPVOID *)&psl);
     if (SUCCEEDED (hres)){
     IPersistFile *ppf;

     // Query IShellLink for the IPersistFile interface for
     // saving the shortcut in persistent storage.
     hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
     if (SUCCEEDED (hres)){
     WORD wsz [MAX_PATH]; // buffer for Unicode string

     // Set the path to the shortcut target.
     hres = psl->SetPath (argv[1]);

     if (! SUCCEEDED (hres))
     cerr << "SetPath failed!\n";

     // Set the description of the shortcut.
     hres = psl->SetDescription (argv[3]);

     if (! SUCCEEDED (hres))
     cerr << "SetDescription failed!\n";

     // Ensure that the string consists of ANSI characters.
     MultiByteToWideChar (CP_ACP, 0, argv[2], -1, wsz, MAX_PATH);

     // Save the shortcut via the IPersistFile::Save member function.
     hres = ppf->Save (wsz, TRUE);

     if (! SUCCEEDED (hres))
     cerr << "Save failed: " << hex << hres << "\n";

     // Release the pointer to IPersistFile.
     ppf->Release ();
     }
     else{
     cerr << "QueryInterface failed\n";
     }

     // Release the pointer to IShellLink.
     psl->Release ();
     }
     else{
     cerr << "CoCreateInstance failed:" << hex << hres << "\n";
     }
     return;

     }
0
 

Author Comment

by:labatti
ID: 1174175
Works fine! Thanx!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question