Solved

How do I create a shortcut in VC++

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now