|
-
Feb 13th, 2007, 05:41 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] store settings
I would like to have a database which stores settings from my programs. I have 3 programs which i would like to interact with one main program (e.g security centre) so the database would need to be opened by them all and needs a password.
I dont have a clue on how to start this, help needed.
thanks in advance
Chris1990
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 13th, 2007, 07:02 PM
#2
Re: store settings
It might be easier, and cheaper in resources, to use the registry. See the SaveSetting/GetSetting example in my signature if you need help.
-
Feb 13th, 2007, 08:13 PM
#3
Re: store settings
For the password, registry would be a good way to go since it's more discreet. For the rest of the settings, though, you might consider using a normal text file. Lots of big apps use that method, and it's fairly simple to read and write the settings as needed.
-
Feb 13th, 2007, 08:15 PM
#4
Re: store settings
Passwords stored in the Registry should be encrypted.
-
Feb 13th, 2007, 08:18 PM
#5
Re: store settings
Agreed. Finding a good encryption shouldn't be too difficult, either. I can't remember who has it, but there's a sig link around here somewhere to a 31-bit encryption. He says it's very difficult to crack, so if it's going to be a high-scale project, then it might be worth your while to check it out. If I come across it, I'll add a link.
-
Feb 13th, 2007, 10:23 PM
#6
Thread Starter
Hyperactive Member
Re: store settings
If i write it to the registry i need to be able to create keys in HKEY_LOCAL_MACHINE not HKEY_CURRENT_USER as the passwords have to be universal
please could you tell me how to write keys like
HKEY_LOCAL_MACHINE\Software\MY APPS\app.name\
also how many registry subkeys can be made
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 13th, 2007, 10:44 PM
#7
Addicted Member
Re: store settings
Timeshifter may have been talking about CVMichael and this encryption
http://www.vbforums.com/showthread.p...hreadid=231798
-
Feb 14th, 2007, 05:55 AM
#8
Re: store settings
 Originally Posted by chris1990
also how many registry subkeys can be made
As many as you want.
VB Code:
Option Explicit
'Form level code.
'Put 3 textboxes and 2 command buttons on a form.
Private Sub Command1_Click()
If RegWriteSZ(HKEY_LOCAL_MACHINE, Text1.Text, Text2.Text, Encrypt(Text3.Text)) <> ERROR_SUCCESS Then
MsgBox "Registry WRITE failed!"
End If
End Sub
Private Sub Command2_Click()
Dim strRet As String
If RegReadSZ(HKEY_LOCAL_MACHINE, Text1.Text, Text2.Text, strRet) <> ERROR_SUCCESS Then
MsgBox "Registry READ failed!"
Else
'Clear the textbox.
Text3.Text = ""
'Retrieve the registry entries.
Text3.Text = Decrypt(strRet)
End If
End Sub
Private Sub Form_Load()
Caption = "Write and Read UserName and Password @ Local-Machine"
Command1.Caption = "Write"
Command2.Caption = "Read"
Text1.Text = "Software\MY APPS\" & App.EXEName 'Subkey.
Text2.Text = "MyUserName"
Text3.Text = "MyPassword"
End Sub
VB Code:
Option Explicit
'Module level code.
'RegCreateKeyEx
'Creates the specified registry key.
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
'RegOpenKeyEx
'Opens the specified registry key.
Public 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
'RegQueryValueEx
'Retrieves the type and data for a specified value name associated with an open registry key.
Public 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
'RegSetValueEx
'Sets the data and type of a specified value under a registry key.
Public 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
'Note that if you declare the lpData parameter as String, you must pass it By Value.
'RegCloseKey.
'Closes a handle to the specified registry key.
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
'Custom error handling.
Public Const ERROR_FAILED = -999&
'Registry type constants.
Public Const REG_SZ = 1 'Unicode nul terminated string.
'Registry return values.
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_MORE_DATA = 234
'Registry HKEY constants.
Public Const HKEY_LOCAL_MACHINE = &H80000002
'Registry manipulation
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
'REG_SZ - 1 off each write/read functions.
Public Function RegWriteSZ(lnghKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
'Create the key and respective REG_SZ value.
Dim lngRetval As Long
Dim lngKeyHandle As Long
'Set the default return value.
RegWriteSZ = ERROR_FAILED
'Create the key.
If RegCreateKeyEx(lnghKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
ByVal 0&, lngKeyHandle, lngRetval) = ERROR_SUCCESS Then
'Open the new key.
If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
'Write it to the registry, and return a code.
RegWriteSZ = RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, _
ByVal strValue, Len(strValue))
End If
End If
'Close any key opened with RegCreateKeyEx.
Call RegCloseKey(lngKeyHandle)
End Function
Public Function RegReadSZ(lnghKey As Long, strSubKey As String, strValueName As String, strRetVal As String) As Long
'Read the REG_SZ value.
Dim lngDataType As Long
Dim lngDataSize As Long
Dim lngKeyHandle As Long
'Reset the return variable.
strRetVal = ""
'Set the default return value.
RegReadSZ = ERROR_FAILED
'Open the key.
If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_QUERY_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) = ERROR_MORE_DATA Then
'Test for string value.
If lngDataType = REG_SZ Then
'Size the string.
strRetVal = Space(lngDataSize)
'Get the current value, and return a code.
RegReadSZ = RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, _
ByVal strRetVal, lngDataSize)
End If
End If
End If
'Close any key opened with RegOpenKeyEx.
Call RegCloseKey(lngKeyHandle)
End Function
Public Function Encrypt(Password As String) As String
'Your encryption code here...
Encrypt = Password 'Temporary...
End Function
Public Function Decrypt(Password As String) As String
'Your encryption code here...
Decrypt = Password 'Temporary...
End Function
-
Feb 15th, 2007, 08:38 AM
#9
Re: store settings
Also, see this:
VB6 - Save settings into a file compressed and encrypted
You can change the code to save in the registry or database, if you don't want to save in a file.
-
Feb 15th, 2007, 12:30 PM
#10
Thread Starter
Hyperactive Member
Re: store settings
how di i make my own registry key under HKLM/software
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 15th, 2007, 02:56 PM
#11
Re: store settings
 Originally Posted by chris1990
how di i make my own registry key under HKLM/software
If you look at the example in my post 8, that's exactly what it does. You can edit the text in the 3 textboxes to decide the location:- HKEY_LOCAL_MACHINE and Text1 ("Software\MY APPS\" & App.EXEName), the name of the values (Text2 - "MyUserName"), and the value itself (Text3 - "MyPassword"). Have you even tried it ??
-
Feb 15th, 2007, 04:08 PM
#12
Hyperactive Member
Re: store settings
hey
if i use what schoolbusdriver said in post 8, would that mean than when i save settings it will save to somewhere that could be read on any account, e.g. my admin account can read any were as the guest cant, by using this can the guest read these settings aswell?
(when i save settings i want them to be shared by the users if you know what i mean)
thanks
lee
If a post has been usefull then Rate it! 
-
Feb 15th, 2007, 06:08 PM
#13
Re: store settings
 Originally Posted by VBlee
hey
if i use what schoolbusdriver said in post 8, would that mean than when i save settings it will save to somewhere that could be read on any account, e.g. my admin account can read any were as the guest cant, by using this can the guest read these settings aswell?
(when i save settings i want them to be shared by the users if you know what i mean)
thanks
lee
As long as you are logged in as admin, you'll be able to write to HKLM - but your users logged in on their own accounts can't. ie WRT the above code, your users would have to get you to login as admin, then you'll be able to set the "username/password" (registry value name / registry value) combination. Your users will then be able to login on their own accounts, enter "MyUserName" (registry value name) and retrieve "MyPassword" (the registry value). It's only to demo how to do the registry HKLM read/write, hence "username/password" - of course it could be anything. In practice you'd retrieve "MyPassword" but not display it, and compare it to whatever the user typed in. So yes, it's effectively a shared READ ONLY value on non-admin accounts. As admin you can do whatever you want, of course.
As an aside, as admin, once you have created the sybkey ("Software\MY APPS\" & App.EXEName - or "Software\MY APPS\Project1" in this case), you should be able to alter its permissions, if you want, by starting regedit, navigating to the subkey, right-clicking on it and choosing "permissions", where you can set the read/write properties for different groups of users, allowing them to do their own setup. But then you'll lose control over what should be restricted/supervised, and they could end up trying to put invalid characters or words you don't want in there - not recommended.
Last edited by schoolbusdriver; Feb 15th, 2007 at 06:13 PM.
Reason: Clarification
-
Feb 15th, 2007, 06:20 PM
#14
Thread Starter
Hyperactive Member
Re: store settings
Can i change the permissions using vb6 code this will also awnser my other thread "Services"
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 16th, 2007, 04:33 AM
#15
Re: store settings
Pnish has put a little app in the "services" thread which probably has the code to do what you want (I haven't had time to look at it yet). I've never tried doing it myself, so have no ready c0d. MS has a lot of info on this:- How to use low-level access control APIs from Visual Basic.
In particular:
The UpdatePermissionsOfRegistryKey function in the sample code uses the RegGetKeySecurity() and RegSetKeySecurity() functions to modify an existing DACL of a registry key. UpdatePermissionsOfHKLM is used to call this helper function to modify permissions on a SOFTWARE\TEST registry key under HKEY_LOCAL_MACHINE.
-
Feb 16th, 2007, 05:26 AM
#16
Thread Starter
Hyperactive Member
Re: store settings
ive downloaded the module now please could you tell me how to use the module to set the permissions on the following reg key.
HKEY_LOCAL_MACHINE\SOFTWARE\MY_APPS\SECURITY\
Thanks in Advance
Chris1990
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 16th, 2007, 01:24 PM
#17
Re: store settings
I think I've got this right . I haven't the time ATM to create a prog from the MS code, so this will have to do for now. To demo this you'll need a couple of accounts as well as the admin account. If you haven't got them already, create them. Lets say they're "User1" and "User2".
1) Create a project called "Project1" with the above code.
2) Rename the module to "Module2". (The mod from MS is also called "Module1"
3) Comment out the following, as this is duplicated in the mod from MS.
VB Code:
"Public Const ERROR_SUCCESS = 0&"
4) In Form_Load, change
VB Code:
Text1.Text = "Software\MY APPS\" & App.EXEName"
to
VB Code:
Text1.Text = "SOFTWARE\MY_APPS\SECURITY"
5) Add a command button with the caption "Set Permission"
6) In this new command button's Click event, add the code "UpdatePermissionsOfHKLM"
7) Now add the MS mod "SetPerm.bas" to the project.
8) In the sub "UpdatePermissionsOfHKLM", change the relevant lines to:
VB Code:
KeyName = "SOFTWARE\MY_APPS\SECURITY"
Accounts(0).AccountName = ""
Accounts(1).AccountName = "User1"
Accounts(2).AccountName = "User1"
9) In the sub "CreateObjectSecurityDescriptor", change the relevant lines to:
VB Code:
Accounts(0).AccountName = ""
Accounts(1).AccountName = "User1"
10) Create the executable and put it somewhere where you can get at it even if you're not logged in as admin.
Make a cup of coffee. It's testing time.
11) Make sure you are logged in as admin then start up the app.
12) Click on "Write", then on "Set Permission", to set permissions for "User1".
13) Assuming there's no error messages, log out and login as "User1". You should find that you can write/read the "username" and "password". (Clicking on "Set Permission" will raise an error message)
14) Log out and login as "User2". You'll find that you CAN'T write/read the "username" and "password". Only the admin and "User1" can do this at the moment.
15) Log in as admin, load the project and do:
16) In the sub "UpdatePermissionsOfHKLM", change the relevant lines to:
VB Code:
Accounts(1).AccountName = "User2"
Accounts(2).AccountName = "User2"
17) In the sub "CreateObjectSecurityDescriptor", change the relevant lines to:
VB Code:
Accounts(1).AccountName = "User2"
18) Rebuild the executable and, as above, put it somewhere where you can get at it even if you're not logged in as admin.
19) Make sure you are logged in as admin then start up the app.
20) Click on "Write", then on "Set Permission", to set permissions for "User2".
21) Assuming there's no error messages, log out and login as "User2". You should find that you can write/read the "username" and "password". (Clicking on "Set Permission" will raise an error message)
22) Log out and login as "User1". You'll find that you can still write/read the "username" and "password".
Both "User1" and "User2" can now write/read the "username" and "password". In practice the "username" would be different for each user, so they could only retrieve their own "password" (or program settings). As admin, YOU would be responsible setting the "read/write" attribute for the individual users. They can't decide their own permissions.
Is that easy or is that easy ?
-
Feb 16th, 2007, 01:40 PM
#18
Thread Starter
Hyperactive Member
Re: store settings
thank you for this code, i will try it later on
chris1990
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 17th, 2007, 10:58 AM
#19
Thread Starter
Hyperactive Member
Re: store settings
is it possible to change the permissions for every user without typing all the usernames into the code. could i put a star (*) instead. I am now using everyone as the user1.
i get an error: ambiguos name detected.
Last edited by chris1990; Feb 17th, 2007 at 11:08 AM.
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 17th, 2007, 04:10 PM
#20
Re: store settings
No, you can't do it like that. WRT the current (posted) code and the MS module, there's nothing to stop you changing the subs "UpdatePermissionsOfHKLM" and "CreateObjectSecurityDescriptor" to functions and passing the (Windows Login) user names to them in a loop. If you can programatically obtain a list of user names, you could do it with a single click on a command button, without resorting to pen and paper. You must remember though, that if you add another user to the PC, you'd have to update the permissions yourself - as only an admin can do this.
(You may want to search for a method of getting a list of user names )
Last edited by schoolbusdriver; Feb 17th, 2007 at 04:13 PM.
Reason: minor clarification
-
Feb 20th, 2007, 04:17 PM
#21
Thread Starter
Hyperactive Member
Re: store settings
i get an error:
HKEY_LOCAL_MACHINE : Ambiguos Name Detected
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
Feb 20th, 2007, 04:26 PM
#22
Re: store settings
Comment out - Public Const HKEY_LOCAL_MACHINE = &H80000002 - from my code. It's duplicated in SetPerm.bas (missed that one)
-
Feb 20th, 2007, 05:03 PM
#23
Thread Starter
Hyperactive Member
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|