Solved

How do I create a shortcut in VC++

Posted on 1998-10-01
3
1,245 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
Comment Utility
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
Comment Utility
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
Comment Utility
Works fine! Thanx!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

16 Experts available now in Live!

Get 1:1 Help Now