Author Topic: IWshShell.CreateShortcut Method  (Read 12393 times)

0 Members and 1 Guest are viewing this topic.

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
IWshShell.CreateShortcut Method
« on: July 14, 2008, 07:57:20 AM »

The following example creates a WshShell object and uses the CreateShortcut method to create two shortcuts.

JScript

Code: [Select]
var WshShell = WScript.CreateObject("WScript.Shell");
strDesktop = WshShell.SpecialFolders("Desktop");
var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
oShellLink.TargetPath = WScript.ScriptFullName;
oShellLink.WindowStyle = 1;
oShellLink.Hotkey = "CTRL+SHIFT+F";
oShellLink.IconLocation = "notepad.exe, 0";
oShellLink.Description = "Shortcut Script";
oShellLink.WorkingDirectory = strDesktop;
oShellLink.Save();
var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
oUrlLink.TargetPath = "http://www.microsoft.com";
oUrlLink.Save();

VBScript

Code: [Select]
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
oShellLink.TargetPath = WScript.ScriptFullName
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = "notepad.exe, 0"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.TargetPath = "http://www.microsoft.com"
oUrlLink.Save

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
Re: IWshShell.CreateShortcut Method
« Reply #1 on: July 14, 2008, 07:58:37 AM »
 
The following code creates a shortcut programatically.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   ' =====================================================================================
   ' Creates an instance of the Windows Script Host
   ' =====================================================================================
   LOCAL pWsh AS IWshShell
   pWsh = NEWCOM "WScript.Shell"
   IF ISNOTHING(pWsh) THEN
      MSGBOX "Error creating an instance of Windows Scripting Host"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
   ' =====================================================================================
   LOCAL pLnk AS IWshShortcut
   pLnk = pWsh.CreateShortcut(UCODE$(EXE.Path$ & "Test.lnk"))
   IF ISNOTHING(pLnk) THEN
      MSGBOX "Error creating the shortcut"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Sets variuos properties and saves them to disk
   ' =====================================================================================
   pLnk.Description = UCODE$("Hello world")
   pLnk.WorkingDirectory = UCODE$(EXE.Path$)
   pLnk.Arguments = UCODE$("/c")
   pLnk.HotKey = UCODE$("Ctrl+Alt+e")
   pLnk.IconLocation = UCODE$(EXE.Path$ & "PROGRAM.ICO,0")
   pLnk.RelativePath = UCODE$(EXE.Path$)
   pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
   pLnk.WindowStyle = %WshNormalFocus
   pLnk.Save
   ' =====================================================================================

   MSGBOX "Shortcut created"

END FUNCTION
' ========================================================================================
« Last Edit: March 14, 2009, 06:11:09 PM by Josť Roca »

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
Re: IWshShell.CreateShortcut Method
« Reply #2 on: July 14, 2008, 08:01:31 AM »
 
The following code creates an url shortcut programatically.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   ' =====================================================================================
   ' Creates an instance of the Windows Script Host
   ' =====================================================================================
   LOCAL pWsh AS IWshShell
   pWsh = NEWCOM "WScript.Shell"
   IF ISNOTHING(pWsh) THEN
      MSGBOX "Error creating an instance of Windows Scripting Host"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
   ' =====================================================================================
   LOCAL pLnk AS IWshURLShortcut
   pLnk = pWsh.CreateShortcut(UCODE$(EXE.Path$ & "Test.lnk"))
   IF ISNOTHING(pLnk) THEN
      MSGBOX "Error creating the shortcut"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Sets the TargetPath property and saves it to disk
   ' =====================================================================================
   pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
   pLnk.Save
   ' =====================================================================================

   MSGBOX "Url Shortcut created"

END FUNCTION
' ========================================================================================

Offline Paul Breen

  • Jr. Member
  • **
  • Posts: 66
Re: IWshShell.CreateShortcut Method
« Reply #3 on: November 21, 2010, 03:09:14 AM »
Mr Roca:
I have tried both of the powerbasic versions and the second one always end with an error  ???
"Instruction referenced memory that could not be read" and no link is created. The second example that sets several properties seems  to work fine.
PB

Offline John Montenigro

  • Newbie
  • *
  • Posts: 8
Re: IWshShell.CreateShortcut Method
« Reply #4 on: November 21, 2010, 09:19:32 PM »
Hey Paul,

Try this:

First:
Code: [Select]
   ' =====================================================================================
   ' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
   ' =====================================================================================
   Local pLnk As IWshURLShortcut
   pLnk = pWsh.CreateShortcut(UCode$(EXE.Path$ & "Test.url"))    ' <<< change to .URL
   If IsNothing(pLnk) Then
      MsgBox "Error creating the shortcut"
      Exit Function
   End If


Second:
Code: [Select]
' =====================================================================================
   ' Sets the TargetPath property and saves it to disk
   ' =====================================================================================
   'pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")   ' <<< change this
   pLnk.TargetPath = UCode$("http://www.spirituality.com")       ' <<< to a real URL string like this
   pLnk.Save
   ' =====================================================================================

Third:
enjoy!


-John
« Last Edit: November 21, 2010, 09:29:07 PM by John Montenigro »

Offline Paul Breen

  • Jr. Member
  • **
  • Posts: 66
Re: IWshShell.CreateShortcut Method
« Reply #5 on: November 22, 2010, 01:37:10 AM »
Thanks, I thought "sure why did'nt I see that?" but it still does the same thing. The url ex. just does something wrong here. I can use the other one.

PB

Offline Dubravko Tuckoric

  • Newbie
  • *
  • Posts: 8
Re: IWshShell.CreateShortcut Method
« Reply #6 on: November 22, 2010, 05:37:01 PM »
Hi Paul,
try this code - it work on my PC:

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"
#Include "shlobj.inc"  

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   ' =====================================================================================
   ' Creates an instance of the Windows Script Host
   ' =====================================================================================
   LOCAL pWsh AS IWshShell
   pWsh = NEWCOM "WScript.Shell"
   IF ISNOTHING(pWsh) THEN
      MSGBOX "Error creating an instance of Windows Scripting Host"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
   ' =====================================================================================
   LOCAL pLnk AS IWshURLShortcut
   Local strDesktop as string
   Local sPath As Asciiz * %MAX_PATH
  
    SHGetFolderPath(0, %CSIDL_DeskTop, 0, 0, sPath)
    strDesktop =  sPath  


   pLnk = pWsh.CreateShortcut(UCODE$(strDesktop & "\TestURL.url"))


   IF ISNOTHING(pLnk) THEN
      MSGBOX "Error creating the shortcut"
      EXIT FUNCTION
   END IF
   ' =====================================================================================

   ' =====================================================================================
   ' Sets the TargetPath property and saves it to disk
   ' =====================================================================================
   ' pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
pLnk.TargetPath = UCODE$("http://www.microsoft.com")

   pLnk.Save
   ' =====================================================================================

   MSGBOX "Url Shortcut created  ????"

END FUNCTION
' ========================================================================================
« Last Edit: November 22, 2010, 05:42:58 PM by Dubravko Tuckoric »

Offline Paul Breen

  • Jr. Member
  • **
  • Posts: 66
Re: IWshShell.CreateShortcut Method
« Reply #7 on: November 23, 2010, 11:09:22 PM »
OK, that last one did it for the url object. I don't know what is wrong with the others for me but this one works fine. Setting the shortcuts in code is already in my latest project. Not much of hint when the call does not work, though.

Did you notice that it takes 217,000 lines of code to make a shortcut to a url? Seventy-six files linked to make this one little shortcut (from log file). 48K program size so there must be a lot going on that I can't see.

Thanks everybody, I'm making shortcuts as if they were free . . .

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
Re: IWshShell.CreateShortcut Method
« Reply #8 on: November 23, 2010, 11:44:26 PM »
Quote
Did you notice that it takes 217,000 lines of code to make a shortcut to a url? Seventy-six files linked to make this one little shortcut (from log file). 48K program size so there must be a lot going on that I can't see.

No, it does not:

Code: [Select]
PowerBASIC for Windows
PB/Win  Version 9.05.
Copyright (c) 1996-2010 PowerBasic Inc.
Englewood, Florida USA
All Rights Reserved

Primary source:  Untitled3.BAS   {1671 total lines}
Target compilation:  Untitled3.exe
Compile time:  0.1 seconds, at 2005200 lines/minute

1116 bytes compiled code, 8572 bytes RTLibrary,
188 bytes string literals, and 2060 bytes dgroup.
Executable stack size:  1048576 bytes.
Disk image: 13312 bytes   Memory image: 10820 bytes.

Component Files:
----------------
Untitled3.BAS
C:\USERS\PEPE\WINAPI\WINAPI\WSHOM.INC
C:\USERS\PEPE\WINAPI\WINAPI\SCRRUN.INC

It is the use of "shlobj.inc" in John's example. As you can imagine, the Windows Shell is very big.

BTW neither 217,000 lines of code are used nor 76 files are linked. These are the number of lines and files parsed, which is not quite the same.

Offline John Montenigro

  • Newbie
  • *
  • Posts: 8
Re: IWshShell.CreateShortcut Method
« Reply #9 on: November 24, 2010, 10:47:47 PM »
Oops! Not mine! (I only showed edits to the first posts...)
-jhm

Have a Happy Thanksgiving!  :)


[ADDED LATER]
With my edits, I've got the code in message #3 to work, but I cannot get the code provided by Dubravko to write a file...

I'm totally new to objects, so I can't track down the problem, but it has me wondering: why would this source code work fine on one machine and not at all on another? Does the OS make that much of a difference? (I'm running on XP PRO SP3)   Or the compiler? (I'm running PBwin 9.05)

-jhm

« Last Edit: November 25, 2010, 04:16:09 AM by John Montenigro »

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 1114
  • Gender: Male
    • it-berater
Re: IWshShell.CreateShortcut Method
« Reply #10 on: December 26, 2010, 12:16:23 PM »
Here it also does not work - XP SP3. I do not really trust the WSH-Stuff for reliability.

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
Re: IWshShell.CreateShortcut Method
« Reply #11 on: December 26, 2010, 04:15:57 PM »
It is realiable, as long as you have it installed, of course.

Offline John Montenigro

  • Newbie
  • *
  • Posts: 8
Re: IWshShell.CreateShortcut Method
« Reply #12 on: July 11, 2014, 06:13:24 PM »
As shown in my posts earlier in this thread, I had been able to create both File and URL shortcuts back in 2010, when I was using PB Win 9.05 and the JR includes were version 117. The code I compiled was the code from messages 2 and 3 of this thread.

Now I'm trying to recompile that same code, using PBWin 10.04 and the JR includes are III/104, and I cannot get past this point:

Code: [Select]
   Local pLnk As IWshShortcut
   pLnk = pWsh.CreateShortcut(UCode$(Exe.Path$ & "Test of CreateFileShortcut.lnk"))
   If IsNothing(pLnk) Then
      MsgBox "Error creating the shortcut"
      Exit Function
   End If   

The only difference between the two versions of WSHOM.inc is that the STRING params are now WSTRING.

So I tried to recompile using the older version of WSHOM.INC, and it worked...

Is there something I am doing wrong?

-John

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3189
  • Gender: Male
    • Josť Roca Software
Re: IWshShell.CreateShortcut Method
« Reply #13 on: July 11, 2014, 06:28:31 PM »
Of course,

with

pLnk = pWsh.CreateShortcut(UCode$(Exe.Path$ & "Test of CreateFileShortcut.lnk"))

you are converting the strint o unicode twice.

Use:

pLnk = pWsh.CreateShortcut(Exe.Path$ & "Test of CreateFileShortcut.lnk")

Offline John Montenigro

  • Newbie
  • *
  • Posts: 8
Re: IWshShell.CreateShortcut Method
« Reply #14 on: July 11, 2014, 09:10:55 PM »
...twice... doh!

I need more sleep...

Thanks, Jose,
-John