Results 1 to 23 of 23

Thread: [RESOLVED] store settings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [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.

  2. #2

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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.

  4. #4

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  7. #7
    Addicted Member
    Join Date
    Jul 2006
    Posts
    147

    Re: store settings

    Timeshifter may have been talking about CVMichael and this encryption
    http://www.vbforums.com/showthread.p...hreadid=231798

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: store settings

    Quote Originally Posted by chris1990
    also how many registry subkeys can be made
    As many as you want.
    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4.  
    5. 'Put 3 textboxes and 2 command buttons on a form.
    6.  
    7. Private Sub Command1_Click()
    8.    If RegWriteSZ(HKEY_LOCAL_MACHINE, Text1.Text, Text2.Text, Encrypt(Text3.Text)) <> ERROR_SUCCESS Then
    9.       MsgBox "Registry WRITE failed!"
    10.    End If
    11. End Sub
    12.  
    13. Private Sub Command2_Click()
    14.    Dim strRet As String
    15.  
    16.    If RegReadSZ(HKEY_LOCAL_MACHINE, Text1.Text, Text2.Text, strRet) <> ERROR_SUCCESS Then
    17.       MsgBox "Registry READ failed!"
    18.    Else
    19. 'Clear the textbox.
    20.       Text3.Text = ""
    21. 'Retrieve the registry entries.
    22.       Text3.Text = Decrypt(strRet)
    23.    End If
    24. End Sub
    25.  
    26. Private Sub Form_Load()
    27.    Caption = "Write and Read UserName and Password @ Local-Machine"
    28.    Command1.Caption = "Write"
    29.    Command2.Caption = "Read"
    30.    Text1.Text = "Software\MY APPS\" & App.EXEName 'Subkey.
    31.    Text2.Text = "MyUserName"
    32.    Text3.Text = "MyPassword"
    33. End Sub

    VB Code:
    1. Option Explicit
    2.  
    3. 'Module level code.
    4.  
    5. 'RegCreateKeyEx
    6. 'Creates the specified registry key.
    7. Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    8.    (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
    9.    ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    10.    lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
    11.  
    12. 'RegOpenKeyEx
    13. 'Opens the specified registry key.
    14. Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    15.    (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    16.    ByVal samDesired As Long, phkResult As Long) As Long
    17.  
    18. 'RegQueryValueEx
    19. 'Retrieves the type and data for a specified value name associated with an open registry key.
    20. Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    21.    (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    22.    lpType As Long, lpData As Any, lpcbData As Long) As Long
    23.  
    24. 'RegSetValueEx
    25. 'Sets the data and type of a specified value under a registry key.
    26. Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    27.    (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    28.    ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    29. 'Note that if you declare the lpData parameter as String, you must pass it By Value.
    30.  
    31. 'RegCloseKey.
    32. 'Closes a handle to the specified registry key.
    33. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
    34.  
    35. 'Custom error handling.
    36. Public Const ERROR_FAILED = -999&
    37.  
    38. 'Registry type constants.
    39. Public Const REG_SZ = 1 'Unicode nul terminated string.
    40.  
    41. 'Registry return values.
    42. Public Const ERROR_SUCCESS = 0&
    43. Public Const ERROR_MORE_DATA = 234
    44.  
    45. 'Registry HKEY constants.
    46. Public Const HKEY_LOCAL_MACHINE = &H80000002
    47.  
    48. 'Registry manipulation
    49. Public Const REG_OPTION_NON_VOLATILE = 0
    50. Public Const KEY_QUERY_VALUE = &H1
    51. Public Const KEY_SET_VALUE = &H2
    52. Public Const KEY_CREATE_SUB_KEY = &H4
    53.  
    54. 'REG_SZ - 1 off each write/read functions.
    55.  
    56. Public Function RegWriteSZ(lnghKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
    57. 'Create the key and respective REG_SZ value.
    58.    Dim lngRetval     As Long
    59.    Dim lngKeyHandle  As Long
    60.  
    61. 'Set the default return value.
    62.    RegWriteSZ = ERROR_FAILED
    63. 'Create the key.
    64.    If RegCreateKeyEx(lnghKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
    65.       ByVal 0&, lngKeyHandle, lngRetval) = ERROR_SUCCESS Then
    66. 'Open the new key.
    67.       If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
    68. 'Write it to the registry, and return a code.
    69.          RegWriteSZ = RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, _
    70.             ByVal strValue, Len(strValue))
    71.       End If
    72.    End If
    73. 'Close any key opened with RegCreateKeyEx.
    74.    Call RegCloseKey(lngKeyHandle)
    75. End Function
    76.  
    77. Public Function RegReadSZ(lnghKey As Long, strSubKey As String, strValueName As String, strRetVal As String) As Long
    78. 'Read the REG_SZ value.
    79.    Dim lngDataType As Long
    80.    Dim lngDataSize As Long
    81.    Dim lngKeyHandle As Long
    82.  
    83. 'Reset the return variable.
    84.    strRetVal = ""
    85. 'Set the default return value.
    86.    RegReadSZ = ERROR_FAILED
    87. 'Open the key.
    88.    If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_QUERY_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
    89. 'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
    90.       If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) = ERROR_MORE_DATA Then
    91. 'Test for string value.
    92.          If lngDataType = REG_SZ Then
    93. 'Size the string.
    94.             strRetVal = Space(lngDataSize)
    95. 'Get the current value, and return a code.
    96.             RegReadSZ = RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, _
    97.                ByVal strRetVal, lngDataSize)
    98.          End If
    99.       End If
    100.    End If
    101. 'Close any key opened with RegOpenKeyEx.
    102.    Call RegCloseKey(lngKeyHandle)
    103. End Function
    104.  
    105. Public Function Encrypt(Password As String) As String
    106.  
    107. 'Your encryption code here...
    108.  
    109.    Encrypt = Password 'Temporary...
    110. End Function
    111.  
    112. Public Function Decrypt(Password As String) As String
    113.  
    114. 'Your encryption code here...
    115.  
    116.    Decrypt = Password 'Temporary...
    117. End Function

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  11. #11
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: store settings

    Quote 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 ??

  12. #12
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    419

    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!

  13. #13
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: store settings

    Quote 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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  15. #15
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  17. #17
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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:
    1. "Public Const ERROR_SUCCESS = 0&"
    4) In Form_Load, change
    VB Code:
    1. Text1.Text = "Software\MY APPS\" & App.EXEName"
    to
    VB Code:
    1. 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:
    1. KeyName = "SOFTWARE\MY_APPS\SECURITY"
    2.     Accounts(0).AccountName = ""
    3.     Accounts(1).AccountName = "User1"
    4.     Accounts(2).AccountName = "User1"

    9) In the sub "CreateObjectSecurityDescriptor", change the relevant lines to:
    VB Code:
    1. Accounts(0).AccountName = ""
    2.     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:
    1. Accounts(1).AccountName = "User2"
    2.     Accounts(2).AccountName = "User2"

    17) In the sub "CreateObjectSecurityDescriptor", change the relevant lines to:
    VB Code:
    1. 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 ?

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  20. #20
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  22. #22
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: store settings

    Comment out - Public Const HKEY_LOCAL_MACHINE = &H80000002 - from my code. It's duplicated in SetPerm.bas (missed that one)

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: store settings

    Thanks
    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.

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