Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I create a shortcut in VC++

Posted on 1998-10-01
3
Medium Priority
?
1,267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

610 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