Page 1 of 1

[windows] The preferred way to create shortcuts.

Posted: Mon Aug 26, 2002 5:05 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by PB.

Here is my procedure for creating shortcuts on Windows. I have called
it the "preferred way" because it suffers none of the limitations of
all other offerings, such as:

(1) The offering at Paul's resource site fails if file extensions are
not shown in folders, and doesn't give you the option of creating the
shortcut in a specific folder (it only does it on the Desktop).

(2) Doesn't require the Visual Basic VB5STKIT.DLL file to be installed.

(3) Doesn't require WSH (Windows Scripting Host) to be installed or
enabled. Some people have it installed but disabled for security
reasons (such as to avoid scripting worms like Melissa).

(4) It's a fully self-contained procedure that works on all versions
of Windows (NT, 95, 98, 2K, and XP). I haven't tested it on ME as I
don't have access to that version of Windows, but it should work.

There is only one problem with it that involves Windows XP: you can't
create shortcuts to EXE files, as mentioned in another post. Having
thought about it, I am 99% sure I have a workaround for this but not
at this time (I need to wait for PureBasic's new "OSVersion()" command
to be added first, otherwise this procedure will get too damn long).

Note also that you can't create shortcuts to "non-registered" files,
that is, files that aren't associated with an application.

To use this procedure, call it in one of two ways:

r=CreateShortcut(SourceFile$,TargetLink$)
r=CreateShortcut(SourceFile$,"desktop")

As you can see, the first way creates a TargetLink$ anywhere on a hard
drive, and the second way creates the link directly on the Desktop.
The procedure returns 0 if the shortcut failed to create, or non-zero
on success. The procedure is heavily commented to explain what is going
on in each step, so it looks a bit messy (sorry!). Enjoy! :)

Code: Select all

; CreateShortcut procedure by PB -- do whatever you want with it.  :)
; Doesn't require any libraries, DLLs or Windows Scripting Host.
; Tested successfully on Windows NT, 95, 98, 2K, and XP, but note that
; XP can't create shortcuts to Exe files due to technical limitations.
; You also can't create shortcuts to "non-registered" files, that is,
; files that aren't associated with an application.
;
; Usage: r=CreateShortcut(SourceFile$,TargetLink$|"desktop")
; Returns 0 for failure to create or non-zero for success.
; Example 1: CreateShortcut("C:\MyApp.exe","desktop")
; Example 2: CreateShortcut("C:\MyApp.exe","D:\Folder\LinkThatRunsMyApp")
;
Procedure CreateShortcut(SourceFile$,TargetLink$)
  If LCase(TargetLink$)="desktop"
    ; Get location of Desktop in Desktop$.
    Desktop$=Space(255) : r=0 : SHGetSpecialFolderLocation_(0,16,@r) : SHGetPathFromIDList_(r,@Desktop$)
    ; Create new TargetLink$ based on Desktop$+SourceFile$.
    TargetLink$=Desktop$+"\"+GetFilePart(SourceFile$)
  EndIf
  ; Make sure TargetLink$ ends with ".lnk" (which is the extension for shortcuts).
  If LCase(Right(TargetLink$,4)) = ".lnk" 
    TargetLink$=TargetLink$+".lnk" 
  EndIf
  ; Get location of Documents menu (which is actually a folder called "Recent").
  Recent$=Space(255) 
  r=0 
  SHGetSpecialFolderLocation_(0,8,@r)
  SHGetPathFromIDList_(r,@Recent$)
  ; Create temp shortcut in Documents menu of SourceFile$, and give it 1/8 of a second to "register".
  SHAddToRecentDocs_(2,SourceFile$) 
  Sleep_(125) ; If we don't wait, then FileSize (below) can fail.
  ; Get temp filename of shortcut (file1$) if "show file extensions" is enabled in Windows.
  file1$=Recent$+"\"+GetFilePart(SourceFile$)+".lnk"
  ; Get temp filename of shortcut (file2$) if "show file extensions" is disabled in Windows.
  a=Len(GetExtensionPart(SourceFile$)) : SourceFile$=Left(SourceFile$,Len(SourceFile$)-a)
  file2$=Recent$+"\"+GetFilePart(SourceFile$)+"lnk" ; The dot is not needed in this case.
  ; Now copy shortcut to TargetLink$.  We test with FileSize to see which temp filename for
  ; the shortcut was actually created, due to the "show file extensions" setting in Windows.
  If FileSize(file1$)-1 ; FileSize -1 if "show file extensions" is enabled.
    r=CopyFile_(file1$,TargetLink$,#False) ; Copy temp shortcut to TargetLink$.
    DeleteFile_(file1$) ; Delete temp shortcut from Documents menu.
  Else ; FileSize = -1 if "show file extensions" is disabled.
    r=CopyFile_(file2$,TargetLink$,#False) ; Copy temp shortcut to TargetLink$.
    DeleteFile_(file2$) ; Delete temp shortcut from Documents menu.
  EndIf
  ProcedureReturn r ; Return status of created shortcut (will be 0 if failed).
EndProcedure
;
original$=OpenFileRequester("Select file to create shortcut from:","","*.*",0)
shortcut$=SaveFileRequester("Specify filename for shortcut (or type 'desktop'):","","*.*",0)
If LCase(Right(shortcut$,7))="desktop" : shortcut$="desktop" : EndIf
;
If CreateShortcut(original$,shortcut$)=0
  Debug "Failed to create shortcut..."
Else
  Debug "Shortcut created successfully!"
EndIf

PB - Registered PureBasic Coder

Posted: Mon Aug 26, 2002 10:08 am
by BackupUser
Restored from previous forum. Originally posted by freak.

> I need to wait for PureBasic's new "OSVersion()" command

So you want to detect, wether it's WinXP, the code is running on?
That's not a lot of Code...

Code: Select all

GetVersionEx_(@os.OSVERSIONINFO)
If os\dwMajorVersion = 5 And os\dwMinorVersion = 1 And os\dwPlatformId = #VER_PLATFORM_WIN32_NT
  ; got XP or .NET Server detected...
Else
  ; all other OS'es...
EndIf
Maybe this helps...

Timo

--------------------------------
Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs and the universe trying to produce bigger and better idiots.

...So far, the universe is winning.