Create Windows "shortcut" (.lnk) with D?

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 5 21:00:55 PST 2016


On Sunday, 6 March 2016 at 03:13:23 UTC, 岩倉 澪 wrote:
> I'm creating a small installation script in D, but I've been 
> having trouble getting shortcut creation to work! I'm a linux 
> guy, so I don't know much about Windows programming...
>
> [...]
>
> Any help would be highly appreciated as I'm new to Windows 
> programming in D and have no idea what I'm doing wrong!

If you don't want to mess with the Windows API then you can 
dynamically create a script (I do this in CE installer):

void createLnk(string exeName, string displayName)
{
     import std.process: environment, executeShell;
     import std.file: remove, exists;
     import std.random: uniform;
     import std.conv: to;

     string vbsName;
     do vbsName = environment.get("TEMP") ~ r"\shcsh" ~ 
uniform(0,int.max).to!string ~ ".vbs";
     while (vbsName.exists);

     string vbsCode = "
         set WshShell = CreateObject(\"WScript.shell\")
         strDesktop = WshShell.SpecialFolders(\"Desktop\")
         set lnk = WshShell.CreateShortcut(strDesktop + 
\"\\%s.lnk\")
         lnk.TargetPath = \"%s\"
         lnk.Save
     ";
     File vbs = File(vbsName, "w");
     vbs.writefln(vbsCode, displayName, exeName);
     vbs.close;
     executeShell(vbsName);

     vbsName.remove;
}



More information about the Digitalmars-d-learn mailing list