Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Program Starts every time windows does

Posted on 1999-09-23
27
Medium Priority
?
229 Views
Last Modified: 2010-05-02
What is the best way to make a program load every time windows loads? Other than putting a shortcut in the startup folder...  I want to be able to have a setting in the program so that the user can make it not load every time windows does if they choose to.
0
Comment
Question by:TAF2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
  • 2
  • +3
27 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 2069077
use the job scheduler and set the scheduled task to run 'at start-up'

save the setting in a .ini file,a text file, or a database then

if setting = false then end   'in the form initialize event.
0
 
LVL 4

Expert Comment

by:Juilette
ID: 2069100

1. The app exe sits in the startup tray.
On load of windows it is executed.
Have a check box item in your menu. Set it so that on unload it is marked to run pgm next instance or not.  
If checked then terminate the app (close it) or unchecked then let it run. The app will always start but if it is checked to close it will just open/close

0
 

Author Comment

by:TAF2000
ID: 2069202
Okay, well could you tell me how to do that?  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:deighton
ID: 2069790
here's the solution - make your start up menu option point to 'yourprog.exe AUTO'

then if the checkbox is set on the program wil not run anymore.  If you want to run it from a desktop shortcut simply run 'yourprog.exe' without the AUTO flag

your still using the windows start-up route - but why not?  It'll work

Private Sub Form_Load()

    Dim i As Integer
    On Error GoTo trap
   
   ''is this run from the startup menu  
    If UCase(Command$) = "AUTO" Then
        Open "option" For Input As #1
        Input #1, i
        If i Then End
        Close #1
    End If
   
trap:

End Sub

Private Sub Form_Unload(Cancel As Integer)

    Open "option" For Output As #1

    If Check1.Value = vbUnchecked Then
   'checked to disable at startup
        Write #1, 0
       
    Else
   
        Write #1, 1
       
    End If
   
    Close #1
       

End Sub

0
 

Author Comment

by:TAF2000
ID: 2070150
I was thinking along the lines of a registry Key.  I can do it using regedit, but I don't know how to write it from VB, or check to see if it is already there.  I dont really understand your solution.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2070191
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long        

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        

Public Const HKEY_CURRENT_USER = &H80000001


Here are all the API's and constants you need to modify the run section of the registry properly. If you would like some code, you'll have to reject the other answer so that I can try to post as an answer.
0
 

Author Comment

by:TAF2000
ID: 2072216
OKay, go ahead.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 2072276
The registry key is HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run.

You need to create an entry like:

AppName              C:\Program Files\AppName\AppName.exe
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2072768
TheLearnedOne apparently didn't read the thread too closely, but I'll post anyway.

First, you need your functions as mentioned in previous quote.

Then, some code:

Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", hReg)
RetVal = RegSetValueEx(hReg, Any_String_Of_ASCII, 0, REG_SZ, Path_To_Your_App, Len(Path_To_Your_App))
RetVal = RegCloseKey(hReg)

RegQueryValueEx can be used to see what is in the entry.

A simple method of preventing the app from loading would be the same as the above code BUT use an invalid or non-existing path/file name. The app won't be loaded and Windows won't display an error.

Replace the RegSetValueEx API I gave you earlier with this one:

Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

While both declares are valid, VB handles strings differently (oddly?) so the alternate declare needs to be used.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2072777
Oops. Forgot one:

Public Const REG_SZ = 1
0
 

Author Comment

by:TAF2000
ID: 2073125
okay..I said that I knew how to do it and that I could do it manually.  
0
 
LVL 5

Accepted Solution

by:
KDivad earned 200 total points
ID: 2074658
What about mine?
0
 

Author Comment

by:TAF2000
ID: 2079794
Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run\test", hReg)
RetVal = RegSetValueEx(hReg, Any_String_Of_ASCII, 0, REG_SZ, "c:\windows\calc.exe", Len("c:\windows\calc.exe"))
RetVal = RegCloseKey(hReg)



whats wrong with this?  I am not getting any errors, but it isn't working.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2080560
Sorry, my fault.

Any_String_Of_ASCII wasn't meant to be literally pasted. What I meant is that you can use any ascii string for the value name.

So...
#1. Remove "\test" from the end of the key's path.
#2. Replace "Any_String_Of_ASCII" with a valid string. It can be anything you want: it's only a "friendly" name for the entry.

Your code, adjusted:

Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", hReg)
RetVal = RegSetValueEx(hReg, "Calculator", 0, REG_SZ, "c:\windows\calc.exe", Len("c:\windows\calc.exe"))
RetVal = RegCloseKey(hReg)

RetVal should be zero after the RegOpenKey statement, indicating that there was no error openeing the key. The others are probably the same.

After running this code, you should be able to open regedit, open the key named "Software\Microsoft\Windows\CurrentVersion\Run" under HKEY_CURRENT_USER, and find a value called "Calculator" with the data "c:\windows\calc.exe".

Let me know!
0
 

Author Comment

by:TAF2000
ID: 2082103
okay..that added the value "Calculator", but the value data was "ô
"


Any ideas?
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2082329
I went through and tried it and I think I know exactly what you did. In a previous quote, I mentioned to exchange one of the API's for a different version of it.

Replace the RegSetValueEx declare with this one:

Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

I tried it with both the "bad" one and this new one. I got the same results you did with the "bad" one and correct results with the "good" one.
0
 

Author Comment

by:TAF2000
ID: 2082443
ahh, yes!  Thanks.  Now all I need is to know how to do it with DWORD values for the other question..  Help me with that part and I'll give you the pts for the other question as well.  Header told me kinda what to do...But you are the one that is showing me how to do it!
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2082477
Haven't tried it in a while (with dword) but this should be what you do:

#1. Add the following constant to your declarations:
Public Const REG_DWORD = 4                      

#2. Use the "bad" declare that I mentioned earlier.

#3. In the RegSetValueEx line (in the code NOT in the declarations section), use REG_DWORD instead of REG_SZ.

To use both "bad" and "good" declares in the same app, replace the one you have with these:

Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Declare Function RegSetValueExAny 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

This way, when you want to add a string to the registry you can call "RegSetValueExString" and use "RegSetValueExAny" for everything else.

Let me know if this is wrong or gives you any problems!
0
 

Author Comment

by:TAF2000
ID: 2082526
well that gave me what apears to be a binary value, but it says {Invalid DWORD value} as the value data.  I say that it apears to be binary, because when I go to edit it, the title is "Edit Binary Value".

This is the code I used:

Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", hReg)
RetVal = RegSetValueExString(hReg, "test", 0, REG_DWORD, "1", Len("1"))
RetVal = RegCloseKey(hReg)

do I specify Hexidecimal or decimal somewhere?  Or does it just default to Hexidecimal?

Thanks
0
 

Author Comment

by:TAF2000
ID: 2083213
This gave me the same response:

Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", hReg)
RetVal = RegSetValueExAny(hReg, "test", 0, REG_DWORD, "0", Len("0"))
RetVal = RegCloseKey(hReg)
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2083670
Hmm...Perhaps VB does not recognize "Any" at all. The declare may need to be altered for each type you need to use. I think DWord and double are the same, so...:

Try this one:
Declare Function RegSetValueExDWord Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Double, ByVal cbData As Long) As Long

If that doesn't work, then try this one:
Declare Function RegSetValueExDWord Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As Double, ByVal cbData As Long) As Long
0
 

Author Comment

by:TAF2000
ID: 2085544
Declare Function RegSetValueExDWord Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Double, ByVal cbData As Long) As Long

creates a binary value with no data...  the other does not work at all.  
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2085828
Whoa! I think I see what happened. In a previous post you put:

RetVal = RegSetValueExString(hReg, "test", 0, REG_DWORD, "1", Len("1"))

You are passing a STRING to this call. Use the original declare (the "bad" one) and this:

RetVal = RegSetValueEx(hReg, "test", 0, REG_DWORD, 1, 4)

I *thought* everything but string could use the "bad" one, but I didn't catch this. When passing a DWord value, you can just use 4 for the final parameter. A DWord has a length of 4 (always!) and using any other function to get that value will slow the call.
0
 

Author Comment

by:TAF2000
ID: 2085949
Okay!  That does work.  But its value is 31.  Why? and how do I change it?
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2086661
31? Odd.
Hang on a minute.

OK, I tried it and I got a value of 1. I don't think it differs from the code you used but here is everything I used to set this value:

API's and constants:
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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_CURRENT_USER = &H80000001
Private Const REG_DWORD = 4

Code:
Dim hReg As Long
RetVal = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", hReg)
RetVal = RegSetValueEx(hReg, "test", 0, REG_DWORD, 1, 4)
RetVal = RegCloseKey(hReg)


Maybe there is a difference here that I am missing that you can catch.
0
 

Author Comment

by:TAF2000
ID: 2089250
I dont know what the problem was...but I have it fixed now.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6862812
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp

In efforts to help all Members maintain their open questions, this is a reminder to click your Member Profile, expand your question history and find/update all your Open and Locked questions.
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question