Solved

How do I create a shortcut in VC++

Posted on 1998-10-01
3
1,257 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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