Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9512
  • Last Modified:

Creating shortcut to a .exe file

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
sambha03
Asked:
sambha03
  • 12
  • 11
  • 3
  • +1
1 Solution
 
Jaime OlivaresCommented:
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
 
adg080898Commented:
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
 
sambha03Author Commented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
sambha03Author Commented:
jaime_olivares: Your solution requires the file to be always placed in C:\ . The user can unzip the folder anywhere.
0
 
Jaime OlivaresCommented:
That was an example, you have to put the correct path at installing time.
0
 
adg080898Commented:
What is the purpose of the shortcut if it is in the same directory as the executable?
0
 
sambha03Author Commented:
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
 
Jaime OlivaresCommented:
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
 
sambha03Author Commented:
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
 
adg080898Commented:
There is no need for a shortcut if the executable is right there. Just doubleclick the executable.
0
 
adg080898Commented:
You were going to extract the icon to some other folder???
0
 
sambha03Author Commented:
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
 
adg080898Commented:
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
 
sambha03Author Commented:
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
 
sambha03Author Commented:
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
 
sambha03Author Commented:
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
 
adg080898Commented:
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
 
sambha03Author Commented:
thanks adg. I got it working using VC. However still wondering if there is an easier way to create a relative shortcut?
0
 
adg080898Commented:
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
 
adg080898Commented:
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
 
sambha03Author Commented:
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
 
adg080898Commented:
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
 
adg080898Commented:
clarification:

... click the + beside resource files and doubleclick resource.rc.
0
 
sambha03Author Commented:
Thanks
0
 
adg080898Commented:
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
 
adg080898Commented:
Glad I could help :)
0
 
feddasCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 11
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now