Results 1 to 29 of 29

Thread: How to add a program to the Startup folder through code?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    How do I add an executable to the startup folder so that it runs on startup? (using VB code of course)

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Sure. Try this:
    Code:
    Private Type OSVERSIONINFO
            dwOSVersionInfoSize As Long
            dwMajorVersion As Long
            dwMinorVersion As Long
            dwBuildNumber As Long
            dwPlatformId As Long
            szCSDVersion As String * 128      '  Maintenance string for PSS usage
    End Type
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    Private Const VER_PLATFORM_WIN32_NT = 2
    Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    
    
    
    Private Sub Command1_Click()
        Dim os As OSVERSIONINFO
        Dim strWindDir As String
        Dim lngRet As Long
        Dim strDestination As String
        Dim strSource As String
        
        'Change the filename to the appropriate one
        strSource = "C:\MyFile.exe"
        
        os.dwOSVersionInfoSize = 148
        os.szCSDVersion = Space$(128)
        lngRet = GetVersionEx(os)
        
        strWindDir = Space(255)
        'Get Windows Directory
        lngRet = GetWindowsDirectory(strWindDir, Len(strWindDir))
        If lngRet Then strWindDir = Left(strWindDir, lngRet)
        
        'Check the Operating System
        Select Case os.dwPlatformId
            Case VER_PLATFORM_WIN32_NT
                strDestination = strWindDir & "\Profiles\All Users\Start Menu\Programs\Startup"
            Case VER_PLATFORM_WIN32_WINDOWS
                strDestination = strWindDir & "\Startup"
        End Select
        'Copy your file
        FileCopy strSource, strDestination
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Serge!

    How would you added it to the "Run" in Regedit.exe.

    Thanks
    Chemically Formulated As:
    Dr. Nitro

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    Looks good Serge... thanks.

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Sure. You can use this function I wrote:
    Code:
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const ERROR_SUCCESS = 0&
    Private Const REG_SZ = 1
    Private Const KEY_SET_VALUE = &H2
    
    Public Function SetProgramStartup(pProgramName As String, pProgramPath As String) As Boolean
        Dim lKeyHandle As Long
        Dim lRet As Long
        Dim strBuffer As String
        Dim strKey As String
        
        strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_SET_VALUE, lKeyHandle)
        If lRet = ERROR_SUCCESS Then
            lRet = RegSetValueEx(lKeyHandle, pProgramName, 0, REG_SZ, ByVal pProgramPath, Len(pProgramPath))
            RegCloseKey lKeyHandle
        End If
    End Function
    Then you can use it like this:


    SetProgramStartup "MyProgram", "C:\MyProgram.exe"

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Thanks Serge!

    Can always depend on you.
    Chemically Formulated As:
    Dr. Nitro

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Serge,
    Your code seems to get the job done, but it has a few problems.

    Problem one:
    In order to retrieve the StartUp folder you're basing your path on what you assume to be the subdirectory from wherever the Destop path is. This is not always the case, it can be changed in the registry. If you want to retrieve the actual path you can do it through a couple of methods.

    You can retrieve any of the standard paths through the registry in the following key:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

    Or you can use the SHGetPathFromIDList and SHGetSpecialFolderLocation API functions from Shell32.dll which also allows you to retrieve any of the standard paths by sending it a constant representing the path you wish to retrieve, such as CSIDL_STARTUP = &H7&.


    Problem two:
    The file that you're copying in to the path is an actual copy of the executable, not a shortcut. In order to create a shortcut to the file you need to implement the IShellLink class. You can find an example of using this class on the VB disk or on the MSDN site.


    Sorry, but I've run in to this same problem before, and I know that you need to do it this way in order to accomadate all user's needs.

    Later.

  8. #8
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    SonGouki!

    Can you show a working sample? I am kind of lost on what your are talking about?
    Chemically Formulated As:
    Dr. Nitro

  9. #9
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    For which part?

    Reading the path from the registry is as easy as using RegOpenKeyEx and RegQueryValueEx. All of the standard paths are kept in the Key I gave, open Regedit up and take a look.

    As for the other part, look on you VB6 CD that you bought, in there is an example called Shelllnk, it shows this whole process. If, under some circumstances, you don't have the VB6 CD, then you can search the MSDN site for the same project name. They used to have it there, I'm not sure if they still do though.

    Hope that helps, if not post again and I'll see if I have the code here.

    BTW, this is in reference to the first code posting, I haven't any problems with the second posting .

    [Edited by SonGouki on 04-12-2000 at 05:45 PM]

  10. #10
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Of course, I know that I can retrieve the path to the Startup directory, but actually, it is a lot easier that it seems:
    Code:
    Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long
    
    
    Private Sub Command1_Click()
        'Note that on Windows NT, you'll have to restart
        'your PC until it takes effect
        Call fCreateShellLink("\Startup", "My Programm", "c:\MyProgram.exe", "")
    End Sub

  11. #11
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Serge!

    Thanks for the code but want to add one more thing for the API.

    NOTE:
    In Visual Basic 5.0 or 6.0
    Private Declare Function fCreateShellLink Lib "Vb5STKIT.DLL" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long

    In Visual Basic 4.0
    Private Declare Function fCreateShellLink Lib "Stkit432.dll" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long
    Chemically Formulated As:
    Dr. Nitro

  12. #12
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    I know that, but STKIT432.DLL still ships with VB5/6, so it really makes no difference, but good catch anyway.

    Thanks Nitro,

  13. #13
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Thumbs up

    That's great Serge! I didn't know about that API! Why didn't you show that the first time? I asked about this a long time ago and all anyone would tell me about is IShellLink. This is a LOT easier to implement, thanks!

  14. #14
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    The reason I put that first example together is because Jamie_Dowd sked for it like this:


    How do I add an executable to the startup folder so that it runs on startup? (using VB code of course)

  15. #15
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    I know that, but STKIT432.DLL still ships with VB5/6, so it really makes no difference, but good catch anyway.

    Thanks Nitro,
    Hello Serge!

    When I run "Stkit432.dll", it doesn't work. Is it because I am running on a Win98 platform or is it because I am using VB6. Once change "Stkit432.dll" to "Vb5STKIT.dll", then everything works fine.

    Please do me a favor! If you are using VB6, change your API to "Stkit432.dll" and run it. If it works, please tell me why it doesn't work on my computer.

    Thanks Serge

    BTW... I don't know where you got this API but it is not in the API viewer. Did you get it from a book?
    Chemically Formulated As:
    Dr. Nitro

  16. #16
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    I did....That example that I've shown was using VB6 on Windows98........hmmmm, I don't understand why it wouldn't work on yours. Did you get any errors???

  17. #17
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Yes!

    I ran the code that you provided last night but it did not work. That is the reason why I went to research on your API and found this site

    http://www.vbcode.com/asp/showsn.asp?theID=117

    I will try it again tonight because I am on a NT platform right now.


    Thanks for all your help Serge!

    Chemically Formulated As:
    Dr. Nitro

  18. #18
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Question

    By the way at this moment I'm developing a site that would be also VB related.
    Currently I'm working on the Message Board. At this moment you cannot create your own login but I'm going to complete it this weekend. Take a look:
    VBGarage

  19. #19
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Thanks Mr. Serge!

    I admire your work very much as well as a handful of others on this site.
    Chemically Formulated As:
    Dr. Nitro

  20. #20
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Unhappy It's so unfair...

    Why is everyone (especially Serge) so much better than me at Visual Basic? I try just as hard, but all I end up with is code similar to this:

    Type Load_Of_Rubbish
    Pile as string
    Size as integer
    Street_Value as decimal
    End Type

    Public Sub Command1_Click()
    Dim Crp as Load_of_Rubbish

    Goto this

    that:
    goto ScrewAround

    this:
    goto that

    ScrewAround:
    Command1.Value = Crp.Street_Value

    End Sub

    Any suggestions as to how I could improve this?



    Oh, yeah, how do you make an area of your message into code (ie Public and Sub go blue and the font stays at Courier New)?
    Courgettes.

  21. #21
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    V(ery) Basic, what exactly are you trying to accomplish???

  22. #22
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Question Qu-est que c'est?

    Or in English: Eh?

    What do you mean by 'what am I trying to accomplish'?
    The bit about the code was just a joke, but it would be nice to know how to make and area of text look like VB Code
    with different colours and font and all that. Any help?
    Courgettes.

  23. #23
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Okay, I got to know this from Serge and/or Nitro, where did you guys find this DLL? It's not on the CDs, and I have the entire Visual Studio Enterprise edition! I've searched the net and found a couple of places where I could get the stkit432.dll as part of another app, but I couldn't find the vb5stkit anywhere.

    A location would be greatly appreciated, please.

  24. #24
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    You should give the credit to Serge or ask him about it.

    I ran the code he provided, but did not work on my computer. It gave me some dam error that it can find the entry point of "skrt.dll".

    I went back to this posting and double check if there was any typo which is not uncommon. Anyway, everything was correct, so I went to the API view - just like you - and did a search on this API (fshell), but could not find it. I then went to my second favorite VB site and did a search on shortcut like what you have mentioned in your previous posting and got the following site:

    http://www.vbcode.com/asp/showsn.asp?theID=117

    And there you have it my quest for this API Mr. SonGouki!

    Hey BTW... Are you Japanese judging from your last name. If yes, are you present in America or Japan? Just curios!
    Chemically Formulated As:
    Dr. Nitro

  25. #25
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Lightbulb

    Ok, I've also searched the CD and did't find it. So my guess would be that this file is compressed with something else and then durring installation it is being uncompressed.

    Just a guess...

  26. #26
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    OK, I just did a search again of everything and I came up with a few things...

    First of all, I searched my computer again with the following search criteria *stkit*.dll and I found two files: stkit416.dll and vb6stkit.dll, so I took a look in both files.
    The stkit416.dll I'm guessing is the 16-bit equivalent of the stkit432.dll that ships with VB4(16), but it doesn't export the fCreateShellLink function. I pretty sure this is because Windows 3.1 didn't support shortcuts, therefore it makes sense that the 16-bit DLL would not provide this functionality. Bad news for those of us who are forced to program apps in 16-bit. I guess I could always link to a 32-bit app in my SetUp package to create the Shortcut.
    The vb6stkit.dll is obviously the DLL that ships with Visual Basic 6. This one does actually export the fCreateShellLink function.


    Nitro, I'm Canadian, but I have a great interest in Japanese culture. My name actually is a combination of a couple of my favorite Japanese pop influences from my younger years, "Son" is the last name of Son Goku from the anime show Dragon Ball Z, and "Gouki" is the japanese name of Akuma a character from the Street Fighter fighting game series from Capcom. I chose the name a long time ago as my ISP ID and it just kind of stuck with me. FYI, this is my Napster ID as well, so if you ever see me in there say hello. My real name is Dan Marion, if you wanted to know.

    Serge, just to straighten this out, not so much for me but for all the other forum members who seem to get it wrong, your name is pronounced as "sir"-"gay", correct? Just wanted to clarify that with you. By the way I just wanted to send you a personal salute as one the most helpful people on this forum! You're the best!

    [Edited by SonGouki on 04-14-2000 at 02:47 PM]

  27. #27
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Dan!

    Nice meeting you and yes, I have to agree Serge is one of the most helpful person on this site. I posted that earlier somewhere on this posting too I think. On the same token, I think your talent is also really great after looking at the posting "Deleting Empty Folders"

    http://forums.vb-world.net/showthrea...threadid=13478


    Anyway, I use to love playing Street Fighter too. I kind think that is cool because you took up an Asian Handle although you are Caucasian. On the other hand, I am asian and love the handle Nitro from the American Gladiator.

    Beside programming, I love fishing. If you go to any fishing site and see the handle "Nitro", yes that is me.

    Nice meeting you again.

    [Edited by Nitro on 04-14-2000 at 02:59 PM]
    Chemically Formulated As:
    Dr. Nitro

  28. #28
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Lightbulb Serge: THIS IS YOUR LIFE!

    Dan (my name's Camillo), (we're all on first name terms
    now!). I would have thought that Serge would be pronounced
    "Sir-j", well, that's how it would be pronounced in England
    (I'm English with an Italian name). If Serge were to be
    from another country, then it would be pronounced differently.

    Where is Serge from. 'I'm-a-naturally-brilliant-programmer-
    world'?



    And just as a matter of interest, how long have you all
    been programming for (me: 2 years: probably why I'm rubbish)

    Well, that's another question I've posted in a 'reply' section.

    Do you get points off for that?

    Cheerio(s are so good for you...)

    Au revoir,
    Ciao,
    Bye,

    Camillo the rubbish VB programmer


    Courgettes.

  29. #29
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Question

    SonGouki, my name was pronounced like that when I used to live in Russia. Now it pronounces like Sir-j.

    V(ery) Basic, I'm originally from Russia. I moved to USA about 10 years ago. I have almost 6 years of VB experience.

    And thank you all for recognition, I really like helping other people who got stock with a problem(s) (even though I never liked to teach).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width