Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating shortcut to a .exe file

Posted on 2004-08-09
27
Medium Priority
?
9,504 Views
Last Modified: 2013-12-03
Hi All,
I have a directory called "A" and a sub directory called "A.sub". I want to create a shortcut to a .exe file in A.sub and place it inside A (at same level as A.sub). What is the best way to do this such that I can give away A.zip to anyone and the shortcut works irrespective of where(dirve or folder) they unzip ?
0
Comment
Question by:sambha03
[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
  • 12
  • 11
  • 3
  • +1
27 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11758945
In Visual Basic is something like this:

    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    Set sc = WshShell.CreateShortcut("YourFileName.zip")
    sc.TargetPath = "c:\folder1\folder2\yourfilename.zip"
    sc.Save

0
 
LVL 8

Expert Comment

by:adg080898
ID: 11758946
If I remember correctly you can't quite do that directly. However, it is probably possible to open up the shortcut in a hex editor and remove the path specification.

I mean:

- Create a shortcut manually in explorer
- Open your new shortcut in a hex editor and overwrite the full path to the file with just the raw filename.

(You might think I'm nuts, but the shortcut interfaces have always left out some needed stuff. For example, almost EVERYTHING is inaccessible when creating a shortcut to a DOS program through the standard COM interfaces. To get around that I just compiled the shortcuts into my installer as resources, extract them, then open the file and patch in all the needed changes "manually". My program used a series of offsets to locate the fields that needed to be changed. Again, I had to hack the shortcuts because they were to a DOS program - I suspect that "real" shortcuts have similar annoying limitations too.)

Or, you could always try using the standard COM interfaces to create the shortcut programmatically without a path. Have you tried that?

Search for IShellLink on the net and you'll find a ton of references. Here's one to give you an idea of what is available when creating the shortcut:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/ifaces/ishelllink/ishelllink.asp
0
 

Author Comment

by:sambha03
ID: 11758989
I am not that experienced in VB. I need the shortcut to work irrespective where "A" is located. The relative path will always be the same for the shortcut "A\A.sub\MYfile.exe". However the location of "A" might be anywhere (C:, D:, E: or C:\somefolder etc)
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:sambha03
ID: 11758991
jaime_olivares: Your solution requires the file to be always placed in C:\ . The user can unzip the folder anywhere.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11759016
That was an example, you have to put the correct path at installing time.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11759203
What is the purpose of the shortcut if it is in the same directory as the executable?
0
 

Author Comment

by:sambha03
ID: 11768557
Sorry for the confusion.
Lets say I have a folder called "Tree". Inside the tree folder I have another folder called "Branch". Inside Branch folder there is a file called apple.exe. I want to create a shortcut to apple.exe and place that shortcut in Tree folder. Now while "Branch" will always be inside "Tree", "Tree" can be placed in c:, d: or in any folder inside c: or d:.

Please advise.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11768594
No matter where is located (c:, d:, etc), at installing time you will know the full-path of .exe file, at this moment create the shortcut and place it where you want.
0
 

Author Comment

by:sambha03
ID: 11768793
I want to zip up the tree folder and make it donwloadable on the web. People may unzip it anywhere in their harddisk. The shortcut should continue to work. Please advise.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11769435
There is no need for a shortcut if the executable is right there. Just doubleclick the executable.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11769438
You were going to extract the icon to some other folder???
0
 

Author Comment

by:sambha03
ID: 11770489
adg:  The shortcut is in the Tree folder. The executable is in the branch folder. So the tree folder contains: Branch folder and a shortcut to a file in the branch folder.  The branch folder contains many many many files (with similar names) and it is confusing for my user to find the right file without the shortcut. The shortcut will say something like "Launcher".
0
 
LVL 8

Accepted Solution

by:
adg080898 earned 2000 total points
ID: 11772927
You could create an extremely simple C program to launch it. Call it launcher.exe. or anything:

----

#include <windows.h>

#define FOLDER_NAME "MyFolder"
#define EXE_NAME "MyProgram.exe"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
            LPSTR pCmdLine, int nCmdShow)
{
      static char aPath[MAX_PATH];
      static char aProg[MAX_PATH];
      char *pSrch;

      if (!GetModuleFileName(hInstance, aPath, sizeof(aPath)))
            return 1;

      pSrch = strrchr(aPath, '\\');
      if (!pSrch)
            return 2;
      pSrch[0] = '\\';
      strcpy(pSrch + 1, FOLDER_NAME);

      strcpy(aProg, aPath);
      strcat(aProg, "\\");
      strcat(aProg, EXE_NAME);

      ShellExecute(NULL, "open", aProg, "", aPath, SW_SHOWNORMAL);

      return 0;
}

----

- Change the FOLDER_NAME define to the subfolder name.
- Change the EXE_NAME define to the name of the program to start.

You can change SW_SHOWNORMAL to SW_SHOWMAXIMIZED if you want it to start maximized.

This little program gets its own location, then chops off its own name, leaving the path. Then it appends the folder name and uses that for the startup directory. It then appends the exe name and starts the program.
0
 

Author Comment

by:sambha03
ID: 11777508
Thanks adg. I think you got what I need exactly. However I tried to compile it using the compiler at
http://www.c-compiler.com/
I am getting the following error. Any advice?

C:\Program Files\Miracle C\temp.c: line 6: Parse Error, expecting `SEP'
'int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow) { static char aPath[MAX_PATH]'
aborting compile
0
 

Author Comment

by:sambha03
ID: 11778883
Its not finding the windows.h . I have it in C:\cygwin\usr\include\w32api . What should I do to make it find it?
0
 

Author Comment

by:sambha03
ID: 11779337
OK, I got it working...Thanks. However now I realised that I'll need to change the icon the file.
Just curious if there is an easier way of creating relative shortcuts?
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779341
I looked through the miracle c page and found that it only builds DOS programs ( on http://www.c-compiler.com/guide/About.htm )

Is the program to be started a Windows program? I assumed it was since this question was posted in the Win Prog area. If it *is* a DOS program, I could write a DOS version of the same program, which miracle C can handle. Using a DOS program to do this will cause a black DOS window to appear momentarily so it is not the cleanest solution.

If the program to be launched is not a dos program...

try using your cygwin compiler:

- Get into a command prompt
- cd to the directory containing the program I posted
- Type:
    gcc -o temp.exe temp.c

This should compile using the cygwin compiler - which builds windows programs. Let me if this works in your setup.
0
 

Author Comment

by:sambha03
ID: 11779358
thanks adg. I got it working using VC. However still wondering if there is an easier way to create a relative shortcut?
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779359
Well, exe files hold icon resources. It uses the first icon in the exe file. Can you compile resources?

Which compiler are you using?
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779371
ok, sorry I missed that VC comment! :)

In the project, right click "Resource Files" and click Add New.

type resource.rc for the filename

say yes to the warning

Pull down the resource menu and choose insert.

Choose Icon

Click import

Find the icon I assume you already have

Ok all the way back and build the project
0
 

Author Comment

by:sambha03
ID: 11779378
I am using MS VC. However I am not sure what resrouces are. I can probably compile them thought. I guess i need more handholding :-)
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779428
Oops, sorry, here's better instructions:

In the project, where you see Source Files, Include Files, Resource Files, right click on Resource files and click "Add files to folder..."

Make sure it is sitting on the same folder as the temp.c (it should be) and type resource.rc as the filename

A warning will appear about the file not existing. Say yes.

Back to the project (where you see Source Files, Include Files, Resource Files), click the + beside resource files and doubleclick it.

Now pull down the resource menu and choose Insert

Choose Icon

Choose Import

Ok your way back and build the project.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779435
clarification:

... click the + beside resource files and doubleclick resource.rc.
0
 

Author Comment

by:sambha03
ID: 11779492
Thanks
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779519
Don't forget to pull down the build menu, choose Batch Build... check both boxes and hit build.

Then, grab the exe from the Release directory. It will be drastically smaller than the Debug one.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11779583
Glad I could help :)
0
 
LVL 5

Expert Comment

by:feddas
ID: 20624649
I had trouble finding an easy solution to this.  I liked the hex edit idea above but couldn't figure out way to open the actual *.lnk file.  It only opened what it was linked too.  I eventually realized HTML is a fitting solution.
I created the following HTML file to be a relative path shortcut
inside the directory "W:\collegeGrad\fall2005\art373\_art_stuff"
that linked to the directory "W:\ExampleWork\ART373"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="REFRESH" content="0;url=..\..\..\..\ExampleWork\ART373">
</HEAD>
<BODY></BODY>
</HTML>

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

715 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