i need able create shortcut in winforms using vs2017. have found many solutions, reason iwshruntimelibrary library doesn't seem work in vs 2017.
i can add reference, there warning symbol in references list reference. consequently imports iwshruntimelibrary statement doesn't work , neither code found...
thoughts?
tia,
miles (win7 pro x 64)
full solution creating shortcuts via win32 api:
public notinheritable class win32interop private sub new() end sub ' http://msdn.microsoft.com/en-us/library/windows/desktop/bb774944%28v=vs.85%29.aspx <flags> public enum ishelllinkgetpathflags shortpath = &h1 uncpriority = &h2 rawpath = &h4 end enum ' http://msdn.microsoft.com/en-us/library/windows/desktop/bb774952%28v=vs.85%29.aspx <flags> public enum ishelllinkresolveflags noui = &h1 update = &h4 noupdate = &h8 nosearch = &h10 notrack = &h20 nolinkinfo = &h40 invokemsi = &h80 end enum ' http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx public enum windowstate integer hide = 0 normal = 1 showminimized = 2 maximize = 3 showmaximized = windowstate.maximize shownoactivate = 4 show = 5 minimize = 6 showminnoactive = 7 showna = 8 restore = 9 showdefault = 10 forceminimize = 11 end enum ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740%28v=vs.85%29.aspx <structlayout(layoutkind.sequential, charset:=charset.auto)> public structure win32finddataw public dwfileattributes uinteger public ftcreationtime long public ftlastaccesstime long public ftlastwritetime long public nfilesizehigh uinteger public nfilesizelow uinteger public dwreserved0 uinteger public dwreserved1 uinteger <marshalas(unmanagedtype.byvaltstr, sizeconst:=260)> public cfilename string <marshalas(unmanagedtype.byvaltstr, sizeconst:=14)> public calternatefilename string end structure ' clsid_shelllink shlguid.h header file. <comimport> <classinterface(classinterfacetype.none)> <guid("00021401-0000-0000-c000-000000000046")> public class cshelllink end class ' http://msdn.microsoft.com/es-es/library/windows/desktop/bb774950%28v=vs.85%29.aspx <comimport> <interfacetype(cominterfacetype.interfaceisiunknown)> <guid("000214f9-0000-0000-c000-000000000046")> public interface ishelllinkw sub getpath(<out(), marshalas(unmanagedtype.lpwstr)> byval pszfile stringbuilder, byval cchmaxpath integer, byref refwin32finddata win32finddataw, byval fflags ishelllinkgetpathflags) sub getidlist(byref refpidl intptr) sub setidlist(byval pidl intptr) sub getdescription(<out(), marshalas(unmanagedtype.lpwstr)> byval pszname stringbuilder, byval cchmaxname integer) sub setdescription(<marshalas(unmanagedtype.lpwstr)> byval pszname string) sub getworkingdirectory(<out(), marshalas(unmanagedtype.lpwstr)> byval pszdir stringbuilder, byval cchmaxpath integer) sub setworkingdirectory(<marshalas(unmanagedtype.lpwstr)> byval pszdir string) sub getarguments(<out(), marshalas(unmanagedtype.lpwstr)> byval pszargs stringbuilder, byval cchmaxpath integer) sub setarguments(<marshalas(unmanagedtype.lpwstr)> byval pszargs string) sub gethotkey(byref refhotkey ushort) sub sethotkey(byval whotkey ushort) sub getshowcmd(byref refshowcmd windowstate) sub setshowcmd(byval ishowcmd windowstate) sub geticonlocation(<out(), marshalas(unmanagedtype.lpwstr)> byval psziconpath stringbuilder, byval cchiconpath integer, byref reficonindex integer) sub seticonlocation(<marshalas(unmanagedtype.lpwstr)> byval psziconpath string, byval iicon integer) sub setrelativepath(<marshalas(unmanagedtype.lpwstr)> byval pszpathrel string, byval dwreserved integer) sub resolve(byval hwnd intptr, byval fflags ishelllinkresolveflags) sub setpath(byval pszfile string) end interface ' http://msdn.microsoft.com/en-us/library/windows/desktop/ms688695%28v=vs.85%29.aspx <comimport> <interfacetype(cominterfacetype.interfaceisiunknown)> <guid("0000010c-0000-0000-c000-000000000046")> public interface ipersist <preservesig()> sub getclassid(byref refclassid guid) end interface ' http://msdn.microsoft.com/en-us/library/windows/desktop/ms687223%28v=vs.85%29.aspx <comimport> <interfacetype(cominterfacetype.interfaceisiunknown)> <guid("0000010b-0000-0000-c000-000000000046")> public interface ipersistfile : inherits ipersist shadows sub getclassid(byref refclassid guid) <preservesig()> function isdirty() integer <preservesig()> sub load(<[in](), marshalas(unmanagedtype.lpwstr)> byval pszfilename string, byval dwmode uinteger) <preservesig()> sub save(<[in](), marshalas(unmanagedtype.lpwstr)> byval pszfilename string, <[in](), marshalas(unmanagedtype.bool)> byval fremember boolean) <preservesig()> sub savecompleted(<[in](), marshalas(unmanagedtype.lpwstr)> byval pszfilename string) <preservesig()> sub getcurfile(<[in](), marshalas(unmanagedtype.lpwstr)> byval ppszfilename string) end interface end class
then:
imports win32interop namespace shortcuts ' http://msdn.microsoft.com/en-us/library/windows/desktop/bb774926%28v=vs.85%29.aspx ' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646278%28v=vs.85%29.aspx <flags> public enum shortcuthotkeymodifier short none = 0s shift = 1s control = 2s alt = 4s win = 8s end enum ' http://msdn.microsoft.com/es-es/library/windows/desktop/bb761056%28v=vs.85%29.aspx public enum shortcutwindowstate integer none = 0 normal = 1 maximized = 3 minimized = 7 end enum <serializable> <xmlroot("shortcutinfo")> public notinheritable class shortcutinfo public property shortcutfile string public property description string public property arguments string public property target string public property workingdir string public property icon string public property iconindex integer public property hotkey string public property hotkeymodifier shortcuthotkeymodifier public property hotkeyaccesor keys public property windowstate shortcutwindowstate public property isfile boolean public property isdirectory boolean public property driveletter string public property directoryname string public property filename string public property fileextension string public sub new() end sub <debuggerstepthrough> public sub create() me.createshortcut(me.shortcutfile, me.target, me.workingdir, me.description, me.arguments, me.icon, me.iconindex, me.hotkeymodifier, me.hotkeyaccesor, me.windowstate) end sub private sub createshortcut(byval filepath string, byval target string, optional byval workingdirectory string = "", optional byval description string = "", optional byval arguments string = "", optional byval icon string = "", optional byval iconindex integer = 0, optional byval hotkeymodifier shortcuthotkeymodifier = shortcuthotkeymodifier.none, optional byval hotkeyaccesor keys = keys.none, optional byval windowstate shortcutwindowstate = shortcutwindowstate.normal) dim lnk new cshelllink dim lnkw ishelllinkw directcast(lnk, ipersistfile).load(filepath, 0) lnkw = directcast(lnk, ishelllinkw) lnkw .setpath(target) .setdescription(description) .setarguments(arguments) .seticonlocation(icon, iconindex) .setshowcmd(directcast(windowstate, windowstate)) .setworkingdirectory(if(not string.isnullorempty(workingdirectory), workingdirectory, path.getdirectoryname(target))) .sethotkey(if(hotkeymodifier + hotkeyaccesor <> 0, me.createword(convert.tobyte(hotkeyaccesor), convert.tobyte(hotkeymodifier)), nothing)) end directcast(lnkw, ipersistfile).save(filepath, true) directcast(lnkw, ipersistfile).savecompleted(filepath) marshal.finalreleasecomobject(lnkw) marshal.finalreleasecomobject(lnk) end sub ' creates word value (16-bit unsigned integer) lobyte , hibyte. private function createword(byval lobyte byte, byval hibyte byte) ushort return bitconverter.touint16({lobyte, hibyte}, 0) end function end class end namespace
note: removed xml documentation reduce code size answer.
usage example:
imports shortcuts dim shortcut new shortcutinfo shortcut .shortcutfile = "c:\my shortcut.lnk" .target = "c:\target file.txt" .arguments = "" .description = "my shortcut description" end shortcut.create()
Comments
Post a Comment