Tengo Windows 7.
Copio mi exe en la carpeta de inicio, reinicio, pero no se ejecuta.
También he probado a añadir una clave en el registro y nada...
Puede ser porque mi exe requiere permisos de administrador pues si creo un programa que tan sólo te muestre un MsgBox funciona con cualquiera de los métodos anteriores... :P
¿Alguna solución? ???
DoEvents! :P
el agregado al registro o a la carpeta inicio, debes hacerlo cuando estes en modo administrador, una vez hecho eso ya no necesitas el permiso, y se ejecuta estando en modo.... "protegido"
HKCU + %APPDATA% no debería darte problemas :-\
Si quiere hacerlo mas silencioso podes usar el svrany y ejecutarlo como un servicio ;). No hace falta que el usuario se loggee, el programa ya va a haber iniciado.
sino fijate no estar errando de carpeta, me ha pasado que tenia una carpeta "inicio" pero en realidad la carpeta era "start up" y por eso no me funcionaba. Esas cosas raras que pasaron en vista y seven de "program files" y "archivos de programas"..
PD: fijate que a tu programa no lo este bloqueando el antivirus.
espero que te sirva.
Mirad, hice unas pruebas con esta función:
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 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 As Long = &H80000001
Private Const KEY_WRITE As Long = &H20006
Private Const REG_SZ As Long = &H1
Public Function PutOnStartUp(ByVal sPath As String) As Boolean
Dim hRegkey As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, hRegkey) = 0 Then
sPath = sPath & vbNullChar
PutOnStartUp = RegSetValueEx(hRegkey, "My App", 0, REG_SZ, ByVal sPath, Len(sPath)) = 0
RegCloseKey hRegkey
End If
End Function
PRUEBA UNO:Private Sub Form_Load()
Dim sPath As String
sPath = App.Path & "\" & App.EXEName & ".exe"
If PutOnStartUp(sPath) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
Me.AutoRedraw = True
Me.Print sPath
End Sub
- Compilo en el escritorio.
- Lo ejecuto.
- Reinicio y efectivamente, se ha ejecutado correctamente desde mi escritorio ;-)
(http://oi45.tinypic.com/34sle06.jpg)
PRUEBA DOS:Private Sub Form_Load()
Dim sPath As String
Dim sDest As String
sPath = App.Path & "\" & App.EXEName & ".exe"
sDest = Environ("tmp") & "\Test.exe"
FileCopy sPath, sDest
If PutOnStartUp(sDest) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
Me.AutoRedraw = True
Me.Print sPath
End Sub
- Compilo en el escritorio.
- Lo ejecuto.
- Compruebo que se ha copiado en la carpeta temporal
- Borro el del escritorio por si las moscas.
- Reinicio y... ¡CRASH!
CitarError 70: Permiso denegado
¿Qué pasa aquí? :o
DoEvents! :P
¿Ese error no será porque se está reescribiendo a sí mismo? :rolleyes:
PD: Prueba con %APPDATA% :-*
He cambiado lo que dices, ahora hago esto:
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 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 As Long = &H80000001
Private Const KEY_WRITE As Long = &H20006
Private Const REG_SZ As Long = &H1
Public Function PutOnStartUp(ByVal sPath As String) As Boolean
Dim hRegkey As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, hRegkey) = 0 Then
sPath = sPath & vbNullChar
PutOnStartUp = RegSetValueEx(hRegkey, "Karcry", 0, REG_SZ, ByVal sPath, Len(sPath)) = 0
RegCloseKey hRegkey
End If
End Function
Private Sub Form_Load()
Dim sPath As String
Dim sDest As String
sPath = App.Path & "\" & App.EXEName & ".exe"
sDest = Environ("APPDATA") & "\Test.exe"
FileCopy sPath, sDest
If PutOnStartUp(sDest) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
Me.AutoRedraw = True
Me.Print sPath
Me.Print sDest
End Sub
- Compilo.
- Ejecuto desde el escritorio.
- Me muestra esto:
(http://oi50.tinypic.com/23rqql0.jpg)
- Compruebo que se ha copiado en la carpeta de destino.
- Arranco el PC y el mismo error 70. :huh:
(http://www.solosubtitulos.com/uploads/imagenes/the_x_files_season1.jpg.pagespeed.ce.5sWj8fpXAe.jpg)
DoEvents! :P[/list]
Hola, mira yo entiendo en ese codigo que la segunda vez que se ejecuta, queres autoescrivir la aplicacion en ejecución, deberias hacer una comprovación de donde se encuentra el ejecutable, es decir si se ejecuta en una carpeta que no sea Environ("APPDATA") , lo copias en Environ("APPDATA") y lo pones en el registro, de lo contrario si estan en Environ("APPDATA"), saltas todos estos paso.
PD: cuando escribas en el registro el path, ponelo entre comillas porque si el path donde esta el ejecutable tiene espacios no va a andar, o bien usa shortpath.
Ejecuta esto:
Private Sub Form_Load()
Dim sPath As String
Dim sDest As String
sPath = App.Path & "\" & App.EXEName & ".exe"
sDest = Environ("APPDATA") & "\Test.exe"
MsgBox "Surprise"
FileCopy sPath, sDest
MsgBox "Modafoca"
If PutOnStartUp(sDest) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
Me.AutoRedraw = True
Me.Print sPath
Me.Print sDest
End Sub
Ok, ok... ya está solucionado. ¡Gracias a todos por vuestro tiempo! :)
Lo arreglé así:
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 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 As Long = &H80000001
Private Const KEY_WRITE As Long = &H20006
Private Const REG_SZ As Long = &H1
Public Function PutOnStartUp(ByVal sPath As String) As Boolean
Dim hRegkey As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, hRegkey) = 0 Then
PutOnStartUp = RegSetValueEx(hRegkey, "HolaLeandro", 0, REG_SZ, ByVal sPath, Len(sPath)) = 0
RegCloseKey hRegkey
End If
End Function
Private Sub Form_Load()
Dim sPath As String
Dim sDest As String
sPath = App.Path & "\" & App.EXEName & ".exe"
sDest = Environ("APPDATA") & "\Test.exe"
If sDest <> sPath Then
FileCopy sPath, sDest
If PutOnStartUp(sDest) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
End If
Me.AutoRedraw = True
Me.Print sPath
Me.Print sDest
End Sub
Tiene lógica:
Sí se ejecutaba al inicio, pero como intentaba sobrescribir la entrada del registro daba error. :¬¬
DoEvents! :P