Archive: Creating a Shortcut using an Environment Variable in the Target field - possible bug?


Creating a Shortcut using an Environment Variable in the Target field - possible bug?
Hi all,

I'm using an environment variable in the CreateShortCut function's second argument (target.file) as the path for the target file. The function succeeds; however, when I examine the shortcut it appends "C:\" to the beginning of the target field string.

For example, define the environment variable FOO to be "C:\some_path"

Create a script:
CreateShortCut "$DESKTOP\foo.lnk" "%FOO%\some_exe.exe"

This will create a shortcut on the desktop with a target field defined as
"C:\%FOO%\some_exe.exe"

which resolves to "C:\C:\some_path\some_exe.exe"

If I were to remove the extra "C:\", the shortcut will work fine.

To create the environment variables, I'm using the sample code found on the NSIS website. This code is working fine. The environment variables are properly being created and deleted on uninstall. I've also tried this with manually created environment variables.

I've tried to work around this problem by setting the "Start In" folder (ie. the working directory) to the directory defined in %FOO%.

SetOutPath "%FOO%"
CreateShortCut "$DESKTOP\foo.lnk" "some_exe.exe"

However, the shortcut fails to create at run time, presumably because it is not a fully qualified path name (even though the working directory is defined to the directory where the program resides).

Any idea why the extra "C:\" is being appended? I looked quickly through the NSIS source code and I can't see where NSIS is possibly appending this. I assume it must be Windows doing this when the ShellLink object is passed parameters in exec.c line 990 (NSIS v2.01). Maybe the code should be setting the working directory before it is setting the path (then maybe the workaround would work)?

Thanks for any help,
Eric


It seems to do it when it the environment variable can't be found. Make sure you set the variable for the current process too. There is an Archive page with such an example.


Thanks kichik. That did the trick. Strange behavior for the failure case though.