We help IT Professionals succeed at work.

Creating shortcut to a .exe file

sambha03 asked
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 ?
Watch Question

Jaime OlivaresSoftware Architect
Top Expert 2008

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"

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:



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)


jaime_olivares: Your solution requires the file to be always placed in C:\ . The user can unzip the folder anywhere.
Jaime OlivaresSoftware Architect
Top Expert 2008

That was an example, you have to put the correct path at installing time.
What is the purpose of the shortcut if it is in the same directory as the executable?


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.
Jaime OlivaresSoftware Architect
Top Expert 2008

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.


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.
There is no need for a shortcut if the executable is right there. Just doubleclick the executable.
You were going to extract the icon to some other folder???


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".
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.


Thanks adg. I think you got what I need exactly. However I tried to compile it using the compiler at
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


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


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?
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.


thanks adg. I got it working using VC. However still wondering if there is an easier way to create a relative shortcut?
Well, exe files hold icon resources. It uses the first icon in the exe file. Can you compile resources?

Which compiler are you using?
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


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 :-)
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.

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


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.
Glad I could help :)

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">
<meta http-equiv="REFRESH" content="0;url=..\..\..\..\ExampleWork\ART373">

Open in new window