INICIAR JUNTO COM PC

PAULOHSV 13/03/2004 15:06:51
#15264
Gostaria de saber como inciar a minha apliação junto com o pc, ist é a hora que ele for ligado o meu programa deve começas a funcionar.
Alguem tem poderia me dizer como fazer isto?
USUARIO.EXCLUIDOS 14/03/2004 09:53:27
#15308
Resposta escolhida
No exemplo abaixo, na pasta do programa soltei Calc.exe (calculadora do Windows):

Private Sub Command1_Click()
'Salva no Regedit
Dim WS As Object, Caminho As String
Set WS = CreateObject("WScript.Shell")
Select Case Right(App.Path, 1)
Case "\": Caminho = App.Path & "CALC.EXE"
Case Else: Caminho = App.Path & "\CALC.EXE"
End Select
'Local dos q iniociam com o Windows:
'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
WS.RegWrite "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\Calculadora", Caminho
End Sub


Na verdade, como vc sabe, não é esta a única chave que faz no Regedit a inicialização de programas; além do regedit, como disse nosso colega CASPER-EARK, também tem o Win.ini, e o mais conhecido de todos: a pasta iniciar do Windows.

OBS.: Não sei se esse código funciona no XP, mas, caso negativo, fica fácil mudar a chave.
Sei que esse código grava perfeitamente a chave no XP, mas não sei se essa chave é comum a todos usuários.

Espero tê-lo ajudado.
USUARIO.EXCLUIDOS 14/03/2004 18:27:08
#15360
GEWTON JHAMES:
Se você puser na pasta Iniciar do Windows qualquer atalho, o mesmo será chamado ao término da inicialização do Windows. Por exemplo, se vc criar um atalho para Calc.exe que fica na pasta Windows e colocar esse atalho na pasta iniciar, qdo. o micro terminar a inicialização, a calculadora será exibida.

Na verdade você pode colocar até executáveis, documentos de texto, etc, diretamente nesta pasta, mas isso não é um procedimento sábio.

Uma vez estando determinado atalho na pasta iniciar do windows, não será necessário registrá-lo no Regedit.

Acho preferível a colocação de um registro numa das chaves Run do Regedit (uso sempre HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run), pois é menos provável que um usuário leigo exclua-o por acaso, ou que o mesmo aplicativo seja chamado duas vezes, uma pela máquina, ao iniciar, outra por um usuário curioso.
USUARIO.EXCLUIDOS 14/03/2004 21:04:45
#15370
O código que estava no programa deu erro ao tentar apagar:

'Apaga do Regedit
Dim WS As Object, Caminho As String
Set WS = CreateObject("WScript.Shell")
Select Case Right(App.Path, 1)
Case "\": Caminho = App.Path & "CALC.EXE"
Case Else: Caminho = App.Path & "\CALC.EXE"
End Select
WS.RegDelete "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\Calculadora", Caminho
MsgBox "Excluído com sucesso."

Mas nesse outro funcionou:
Se o valor for binário:

Option Explicit
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
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 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
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
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim RET
'Open the key
RegOpenKey hKey, strPath, RET
'Get the key's content
GetString = RegQueryStringValue(RET, strValue)
'Close the key
RegCloseKey RET
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim RET
'Create a new key
RegCreateKey hKey, strPath, RET
'Save a string to the key
RegSetValueEx RET, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey RET
End Sub
Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
Dim RET
'Create a new key
RegCreateKey hKey, strPath, RET
'Set the key's value
RegSetValueEx RET, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey RET
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim RET
'Create a new key
RegCreateKey hKey, strPath, RET
'Delete the key's value
RegDeleteValue RET, strValue
'close the key
RegCloseKey RET
End Sub
Private Sub Command3_Click()
'Delete Value (Delete the setting from the registry)
DelSetting HKEY_CURRENT_USER, "Raniere", "BinaryValue"
MsgBox "O valor foi apagado...", vbInformation + vbOKOnly, App.Title
End Sub


Se o valor for string:

Option Explicit
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_String = 1
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
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 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
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


Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_String Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim RET
'Open the key
RegOpenKey hKey, strPath, RET
'Get the key's content
GetString = RegQueryStringValue(RET, strValue)
'Close the key
RegCloseKey RET
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim RET
'Create a new key
RegCreateKey hKey, strPath, RET
'Save a string to the key
RegSetValueEx RET, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey RET
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim RET
'Create a new key
RegCreateKey hKey, strPath, RET
'Delete the key's value
RegDeleteValue RET, strValue
'close the key
RegCloseKey RET
End Sub
Private Sub Command3_Click()
'Delete Value (Delete the setting from the registry)
DelSetting HKEY_CURRENT_USER, "Raniere", "StringValue"
MsgBox "O valor foi apagado...", vbInformation + vbOKOnly, App.Title
End Sub
USUARIO.EXCLUIDOS 14/03/2004 21:09:31
#15371
Uma observação:

Nesses últimos códigos a chave a qual fiz alusão não é a do primeiro exemplo.
Mas basta que vc altere. (foi a pressa.)

Esse último exemplo retirei há meses do AllAPI.

Espero ter ajudado. Se você quiser, mando por e-mail dois ou três projetinhos onde você pode lidar bem com o Regedit, adicionar, retirar chaves, etc.

Basta enviar um e-mail para raniere@web.de

Boa noite!
USUARIO.EXCLUIDOS 14/03/2004 22:21:51
#15376
Acho que tem algo de errado com essa linha:
Set WS = CreateObject("WScript.Shell") 

que só deveria ser usada para criar a chave e na linha onde foi posto
WS.RegDelete "HKEY_LOCAL_MACHINE\Software...

o objetivo seria apagar a chave.

Por falar nisso, já enviei o e-mail, tem uma boa quantidade de códigos. Espero que ajude.
USUARIO.EXCLUIDOS 14/03/2004 22:23:24
#15377
Acho que tem algo de errado com essa linha:
Set WS = CreateObject("WScript.Shell") 

que só deveria ser usada para criar a chave e na linha onde foi posto
WS.RegDelete "HKEY_LOCAL_MACHINE\Software...

o objetivo seria apagar a chave.

Por falar nisso, já enviei o e-mail, tem uma boa quantidade de códigos. Espero que ajude.
Tópico encerrado , respostas não são mais permitidas