How to add autostart feature for portable program?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: How to add autostart feature for portable program?

Post by Joakim Christiansen »

idle
I just looked at your code again and saw that I forgot to execute the procedure :lol:
It does work! :D
I like logic, hence I dislike humans but love computers.
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to add autostart feature for portable program?

Post by idle »

Joakim Christiansen wrote:idle
I just looked at your code again and saw that I forgot to execute the procedure :lol:
It does work! :D
:lol:
Windows 11, Manjaro, Raspberry Pi OS
Image
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to add autostart feature for portable program?

Post by c4s »

I just tried creating a shortcut in #CSIDL_STARTUP (using http://www.purebasic.fr/english/viewtop ... 31#p349331) on Vista and it miserably fails. Actually the shortcut was created on the desktop and I really don't know why... however it works just fine on XP.
After a rather quick search through the www I found out that it could be a problem with the access rights, restrictions, UAC or whatever. Since I'm still developing on a XP system I don't know what exactly blocks it from working properly.

Do you have any idea on this?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to add autostart feature for portable program?

Post by ts-soft »

No problem on win 7:

Code: Select all

; German forum: http://robsite.de/php/pureboard/viewtopic.php?t=3078&highlight=
; Author: Danilo
; Date: 09. December 2003
;
; create shell links/shortcuts
; translated from my old example that used CallCOM()
;
; by Danilo, 09.12.2003

; changed for easy use in PB 4.0 >
; by ts-soft

; changed for PB 4.50 and Unicode

EnableExplicit

Macro DEFINE_GUID(Name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
  CompilerIf Defined(Name, #PB_Variable)
    If SizeOf(Name) = SizeOf(GUID)
      Name\Data1    = l
      Name\Data2    = w1
      Name\Data3    = w2
      Name\Data4[0] = b1
      Name\Data4[1] = b2
      Name\Data4[2] = b3
      Name\Data4[3] = b4
      Name\Data4[4] = b5
      Name\Data4[5] = b6
      Name\Data4[6] = b7
      Name\Data4[7] = b8
    Else
      Debug "Error - variable not declared as guid"
    EndIf
  CompilerEndIf
EndMacro


Procedure CreateShortcut(Path.s, Link.s, WorkingDir.s = "", Argument.s = "", ShowCommand.l = #SW_SHOWNORMAL, Description.s =  "", HotKey.l = #Null, IconFile.s = "|", IconIndex.l = 0)
  Protected psl.IShellLinkW, ppf.IPersistFile, Result
  Protected.GUID CLSID_ShellLink, IID_IShellLink, IID_IPersistFile
 
  DEFINE_GUID(CLSID_ShellLink, $00021401, $0000,$0000, $C0, $00, $00, $00, $00, $00, $00, $46) ; {00021401-0000-0000-C000-000000000046}
  DEFINE_GUID(IID_IShellLink, $000214F9, $0000,$0000, $C0, $00, $00, $00, $00, $00, $00, $46)  ; {000214F9-0000-0000-C000-000000000046}
  DEFINE_GUID(IID_IPersistFile, $0000010B, $0000,$0000, $C0, $00, $00, $00, $00, $00, $00, $46); {0000010b-0000-0000-C000-000000000046}
 
  If IconFile = "|" : IconFile = Path : EndIf
  If Not WorkingDir : WorkingDir = GetPathPart(Path) : EndIf

  CoInitialize_(0)
  If CoCreateInstance_(@CLSID_ShellLink, 0, 1, @IID_IShellLink, @psl) =  #S_OK

    Set_ShellLink_preferences:
    psl\SetPath(Path)
    psl\SetArguments(Argument)
    psl\SetWorkingDirectory(WorkingDir)
    psl\SetDescription(DESCRIPTION)
    psl\SetShowCmd(ShowCommand)
    psl\SetHotkey(HotKey)
    psl\SetIconLocation(IconFile, IconIndex)
    ShellLink_SAVE:
    If psl\QueryInterface(@IID_IPersistFile, @ppf) = #S_OK
      ppf\Save(Link, #True)
      result = 1
      ppf\Release()
    EndIf
    psl\Release()
  EndIf
  CoUninitialize_()
  ProcedureReturn result
EndProcedure


Procedure.s GetSpecialFolder(CSIDL)
  Protected *itemid.ITEMIDLIST
  Protected location.s = Space(#MAX_PATH)
  
  If SHGetSpecialFolderLocation_ (0, CSIDL, @*itemid) = #NOERROR
    If SHGetPathFromIDList_(*itemid, @location) = #True
      If Right(location, 1) <> "\" : location + "\" : EndIf
    Else
      location = ""
    EndIf
    CoTaskMemFree_(*itemid)
  EndIf
  
  ProcedureReturn location
EndProcedure

Define.s Folder = GetSpecialFolder(#CSIDL_STARTUP)

Debug CreateShortcut("notepad.exe", Folder + "editor.lnk")
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to add autostart feature for portable program?

Post by c4s »

@ts-soft
I already told you that some time ago but putting location.s=Space(#MAX_PATH) at the top isn't good, because when SHGetSpecialFolderLocation_() fails, Space(#MAX_PATH) will be returned by the GetSpecialFolder() function...

c4s wrote:I just tried creating a shortcut in #CSIDL_STARTUP (using viewtopic.php?p=349331#p349331) on Vista and it miserably fails. Actually the shortcut was created on the desktop and I really don't know why... however it works just fine on XP.
Ok, I just checked it again and GetSpecialFolder(#CSIDL_STARTUP) returns nothing on Vista!
That's why the shortcut was created on the desktop: The testing executable was on there.

So the problem isn't creating the shortcut - which obviously works - but getting the correct folder!

Edit:
The returned error by SHGetSpecialFolderLocation_() is $80070002 (instead of #S_OK=$00000000) but I can't find a description for it in this context.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: How to add autostart feature for portable program?

Post by LuCiFeR[SD] »

have you tried #CSIDL_ALTSTARTUP ?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to add autostart feature for portable program?

Post by c4s »

LuCiFeR[SD] wrote:have you tried #CSIDL_ALTSTARTUP ?
Yep, just did.
Same error on Vista and doesn't work on XP as well - error $80004005...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: How to add autostart feature for portable program?

Post by a_carignan »

netmaestro wrote: Sun Mar 20, 2011 3:25 pm This is the method I use, code is by Joakim Christiansen. With this procedure all you need to do is implement a menu item for the user to check or clear "Start with Windows" and run this procedure with the state parameter set to 1 or 0. I use it in several of my programs and it's never given me any problems:

Code: Select all

Procedure StartWithWindows(State.b) ; by Joakim Christiansen
  Protected Key.l = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine 
  Protected Path.s = "Software\Microsoft\Windows\CurrentVersion\Run" 
  Protected Value.s = "GmailEnhancer" ;Change into the name of your program 
  Protected String.s = Chr(34)+ProgramFilename()+Chr(34) ;Path of your program 
  Protected CurKey.l 
  If State 
    RegCreateKey_(Key,@Path,@CurKey) 
    RegSetValueEx_(CurKey,@Value,0,#REG_SZ,@String,Len(String)) 
  Else 
    RegOpenKey_(Key,@Path,@CurKey) 
    RegDeleteValue_(CurKey,@Value) 
  EndIf 
  RegCloseKey_(CurKey) 
EndProcedure 
Hello, I have a problem with this code, it only writes the first 36 out of 74 characters of my software and as a result the software does not start. Is there a solution to make it write all the characters?
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: How to add autostart feature for portable program?

Post by a_carignan »

a_carignan wrote: Sat Mar 16, 2024 8:43 pm
netmaestro wrote: Sun Mar 20, 2011 3:25 pm This is the method I use, code is by Joakim Christiansen. With this procedure all you need to do is implement a menu item for the user to check or clear "Start with Windows" and run this procedure with the state parameter set to 1 or 0. I use it in several of my programs and it's never given me any problems:

Code: Select all

Procedure StartWithWindows(State.b) ; by Joakim Christiansen
  Protected Key.l = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine 
  Protected Path.s = "Software\Microsoft\Windows\CurrentVersion\Run" 
  Protected Value.s = "GmailEnhancer" ;Change into the name of your program 
  Protected String.s = Chr(34)+ProgramFilename()+Chr(34) ;Path of your program 
  Protected CurKey.l 
  If State 
    RegCreateKey_(Key,@Path,@CurKey) 
    RegSetValueEx_(CurKey,@Value,0,#REG_SZ,@String,Len(String)) 
  Else 
    RegOpenKey_(Key,@Path,@CurKey) 
    RegDeleteValue_(CurKey,@Value) 
  EndIf 
  RegCloseKey_(CurKey) 
EndProcedure 
Hello, I have a problem with this code, it only writes the first 36 out of 74 characters of my software and as a result the software does not start. Is there a solution to make it write all the characters?
In fact, you should use StringByteLength(String) and not len(string).
Post Reply