Results 1 to 34 of 34

Thread: [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

    I have the following code to save the forms position when it is closed, but when it runs through the Reg.bas if errors out on this line:

    Code:
    If rv <> REGISTRY_SUCCESS Then GoTo ErrorHandler
    Code:
    Dim vSubKey As String
    vSubKey = "Software\Deftech\CodeLibrary\2.0\Config"
    
    SaveRegistryValue regLong, hkeyLOCAL_MACHINE, vSubKey, "Left", Me.Left
    SaveRegistryValue regLong, hkeyLOCAL_MACHINE, vSubKey, "Top", Me.Top
    SaveRegistryValue regLong, hkeyLOCAL_MACHINE, vSubKey, "Height", Me.Height
    SaveRegistryValue regLong, hkeyLOCAL_MACHINE, vSubKey, "Width", Me.Width
    Is there something else I should be doing to make a new entry in the registry.
    The app is just for me, so I could manually enter the new keys, but I would like to know how to create them properly.
    Last edited by aikidokid; Mar 9th, 2007 at 09:13 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Save to registry, but it doesn't!

    What is the error?

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    This code was given to me ages ago by a friend, but I cannot quite remember how to use it properly

    It goes to this line and then returns the error message:

    vb Code:
    1. If rv <> REGISTRY_SUCCESS Then GoTo ErrorHandler

    vb Code:
    1. Public Function SaveRegistryValue(ValueType As enumRegistryValueType, _
    2.                             TopLevelKey As enumTopLevelKey, _
    3.                             SUBKEY As String, _
    4.                             RegistryItemName As String, _
    5.                             RegistryData As Variant) As Boolean
    6.    
    7.     'init
    8.     Dim hKey As Long
    9.     Dim hCurrKey As Long
    10.     Dim vDataType As Long
    11.     Dim vDataBufferSize As Long
    12.     Dim rv As Long
    13.    
    14.     Select Case ValueType
    15.         Case regString
    16.             'create string buffer
    17.             Dim vStrBuffer As String
    18.             vDataType = REG_SZ
    19.         Case regLong
    20.             'create long buffer
    21.             Dim vBuffer As Long
    22.             vDataType = REG_DWORD
    23.     End Select
    24.    
    25.     'init values
    26.     SaveRegistryValue = True
    27.     hKey = TopLevelKey
    28.     hCurrKey = 0
    29.     rv = 0
    30.    
    31.     'populate buffer
    32.     Select Case ValueType
    33.         Case regString
    34.             vStrBuffer = CStr(RegistryData) & vbNullChar
    35.             vDataBufferSize = Len(vStrBuffer)
    36.         Case regLong
    37.             vBuffer = CLng(RegistryData)
    38.             vDataBufferSize = 4
    39.     End Select
    40.  
    41.     'open registry key - handle returned in `hCurrKey`
    42.     rv = RegOpenKeyEx(hKey, SUBKEY, 0, KEY_ALL_ACCESS, hCurrKey)
    43.    
    44.     If rv <> REGISTRY_SUCCESS Then GoTo ErrorHandler
    45.  
    46.     'set key value
    47.     Select Case ValueType
    48.         Case regString
    49.             rv = RegSetValueEx(hCurrKey, RegistryItemName, _
    50.                             0, vDataType, ByVal vStrBuffer, _
    51.                             vDataBufferSize)
    52.         Case regLong
    53.             rv = RegSetValueEx(hCurrKey, RegistryItemName, _
    54.                             0, vDataType, vBuffer, vDataBufferSize)
    55.     End Select
    56.    
    57.     If rv <> REGISTRY_SUCCESS Then GoTo ErrorHandler
    58.  
    59.     'close key
    60.     rv = RegCloseKey(hCurrKey)
    61.    
    62.     If rv = REGISTRY_SUCCESS Then Exit Function
    63.  
    64. ErrorHandler:
    65.     On Error Resume Next
    66.     'attempt to close key
    67.     rv = RegCloseKey(hCurrKey)
    68.     SaveRegistryValue = False
    69. End Function
    Last edited by aikidokid; Mar 7th, 2007 at 11:35 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Save to registry, but it doesn't!

    I'll repeat Hack's question for emphasis:

    WHAT error message? "The" error message doesn't tell us what the error is. I'm guessing that you haven't defined REGISTRY_SUCCESS.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    I'm not quite sure how to explain this.
    I have this code in the form_unload:

    vb Code:
    1. SaveRegistryValue regLong, hkeyLOCAL_MACHINE, vSubKey, "Left", Me.Left

    When I step through the code, it then, after this line, takes me to the Registry.Bas, shown in post #3

    It goes as far as the line:

    vb Code:
    1. If rv <> REGISTRY_SUCCESS Then GoTo ErrorHandler

    It then goes to the ErrorHandler and then exits the sub.

    Registry_success is defined as

    vb Code:
    1. Public Const REGISTRY_SUCCESS = 0&

    I am only assuming that because there is not already a key for this in the registry, that it doesn't write to it.

    That's why I asked if there was something else I should be doing to write this to the registry for the first time.

    I hope this is a bit clearer
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Quote Originally Posted by aikidokid
    I am only assuming that because there is not already a key for this in the registry, that it doesn't write to it.
    That is correct. RegSetValueEx requires an pre-existing subkey if you're not writing a value directly to the hive's root. Use RegCreateKeyEx to create the subkey first.

    As an aside, it's not good practice to use KEY_ALL_ACCESS, as this is likely to fail if you're not logged-in as admin. Use KEY_CREATE_SUB_KEY and KEY_SET_VALUE as appropriate.

    EDIT From MSDN:
    Unlike the RegCreateKeyEx function, the RegOpenKeyEx function does not create the specified key if the key does not exist in the registry.
    The initial error will actually occur in RegOpenKeyEx.
    Last edited by schoolbusdriver; Mar 7th, 2007 at 12:31 PM.

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Thanks schoolbusdriver

    That is correct. RegSetValueEx requires an pre-existing subkey if you're not writing a value directly to the hive's root. Use RegCreateKeyEx to create the subkey first.
    So I would create the Parent Key first, and I'm guessing the sub keys, if needed.
    Then, each time the app runs, just check the key exists.

    Is this correct?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Nearly . RegCreateKeyEx has a double purpose. If the subkey already exists, it opens it. Whether the key pre-exists or not, it returns a handle to it. The branch to the subkey is created automatically.

    Typically, to create a value, irrespective of whether you know it already exists, you'd do: RegCreateKeyEx > RegOpenKeyEx > RegSetValueEx > RegCloseKey.

    To read a value, irrespective of whether you know it already exists, you'd do: RegOpenKeyEx > RegQueryValueEx (to get the type of data) > RegQueryValueEx (to get the actual data) > RegCloseKey. And return a default value or error code if RegOpenKeyEx or the first RegQueryValueEx failed.

    If you're feeling lazy , search my previous posts using the above function names as keywords.

  9. #9

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Thanks schoolbusdriver.

    So this answer would answer my question here I would think
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Yes. Some of my posts show how to write/read DWORDs (for the Left, Top etc values). If you have any trouble.......

    There's also way to write/read a UDT containing all the values to a REG_BINARY or REG_NONE, in one call. but If you're happy saving them as individual values....

  11. #11

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Thanks alot schoolbusdriver, you've been a great help.

    I think I will stick with the individual values to start with.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  12. #12

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Still struggling a bit here ..

    I have the following, but am getting an error message here:

    Code:
    RegSetValueEx Ret, HKEY_LOCAL_MACHINE, vSubKey, 0, "REG_DWORD",
    I know there is something missing from the end, but I'm not quite sure what goes there!!

    vb Code:
    1. Dim Ret
    2. Dim vSubKey As String
    3. vSubKey = "Software\Deftech\CodeLibrary\2.0"
    4.  
    5. Dim Result As Long
    6. 'Check if the specified key exists
    7. RegOpenKeyEx HKEY_LOCAL_MACHINE, vSubKey, 0, KEY_ALL_ACCESS, Ret
    8. 'If the key doesn't exist, we create it
    9. If Result = 0 Then
    10.     'Create a new key
    11.     RegCreateKeyEx HKEY_LOCAL_MACHINE, vSubKey, 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
    12.     If Result = 0 Then
    13.         MsgBox "Error while creating the Key!!"
    14.         Exit Sub
    15.     End If
    16. End If
    17.  
    18. RegOpenKeyEx HKEY_LOCAL_MACHINE, vSubKey, 0, KEY_ALL_ACCESS, Result
    19. RegSetValueEx Ret, HKEY_LOCAL_MACHINE, vSubKey, 0, "REG_DWORD",
    20. RegCloseKey Ret

    Any pointers in the right direction would be great.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Hmmm... a few issues... (I've not actually run this....)

    vb Code:
    1. Dim vSubKey As String
    2. Dim Ret As Long
    3. Dim Result As Long
    4. Dim lngKeyHandle As Long
    5.  
    6.    vSubKey = "Software\Deftech\CodeLibrary\2.0"
    7.  
    8. 'Check if the specified key exists
    9.    Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, vSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle)
    10.    If Result <> 0 Then 'Key doesn't exist. 0 signifies success.
    11. 'Create a new key
    12.       Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, vSubKey, ByVal 0&, REG_DWORD, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lngKeyHandle, Ret)
    13.       If Result <> 0 Then '0 signifies success.
    14.          MsgBox "Error while creating the Key!!"
    15.          Exit Sub
    16.       End If
    17. 'Open the new key.
    18.       RegOpenKeyEx HKEY_LOCAL_MACHINE, vSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle
    19.    End If
    20.  
    21. 'Now that we have a valid handle...
    22.  
    23. 'Give it a value of say, 25
    24. 'The 4 signifies the length of the value. For a DWORD (Long) it's always 4.
    25.     RegSetValueEx lngKeyHandle, HKEY_LOCAL_MACHINE, vSubKey, 0&, "REG_DWORD", 25, 4
    26.     RegCloseKey lngKeyHandle
    Last edited by schoolbusdriver; Mar 7th, 2007 at 06:11 PM. Reason: Found out how the =vb tag works

  14. #14

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    I am now getting an error here on this line - wrong number of arguments:

    vb Code:
    1. RegSetValueEx lngKeyHandle, HKEY_LOCAL_MACHINE, vSubKey, 0&, "REG_DWORD", 25, 4

    I have removed the "lngKeyHandle" part and now get type mismatch!!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    My fault, it was getting late . It's the "HKEY_LOCAL_MACHINE" parameter that wants removing - the handle to the open key is contained in lngKeyHandle.
    =vb Code:
    1. RegSetValueEx lngKeyHandle,  vSubKey, 0&, REG_DWORD, 25, 4
    EDIT: REG_DWORD should not be in quotation marks.
    Last edited by schoolbusdriver; Mar 8th, 2007 at 11:47 AM.

  16. #16

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Thanks for all your help with this problem.

    So once I check, for the key, create it, add a value to it and then close it, that should be it for that one key. Yes?

    What or where have I names the key?

    Do I have to add a value to the key straight away?

    For example, I create a key to hold the form top value. Do I need to give it a value straight away, or once it's created, add the value at form_unload.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    As it stands the code will create the subkey [HKEY_LOCAL_MACHINE\SOFTWARE\Deftech\CodeLibrary\2.0] and a value name of "Software\Deftech\CodeLibrary\2.0" with the DWORD value of 25. As you want to save "Left = XXX", "Top = XXX" etc, you need to something like this in the "Query_Unload" event, ie before the form starts unloading - maybe a "Save changes ?" message.

    RegSetValueEx lngKeyHandle, "Left", 0&, REG_DWORD, Form1.Left, 4
    RegSetValueEx lngKeyHandle, "Top", 0&, REG_DWORD, Form1.Top, 4
    RegSetValueEx lngKeyHandle, "Width", 0&, REG_DWORD, Form1.Width, 4
    RegSetValueEx lngKeyHandle, "Height", 0&, REG_DWORD, Form1.Height, 4

    That does the saving. You'd read the values in the Form_Load event. Do you have reading code ?

  18. #18

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Ok, so I don't have to assign a value straight away, thats good, I've got that.

    So with your example above, the name of the key would be "Left", is that right?


    Sorry for beng a bit slow here, but where did I name the actual key
    a value name of "Software\Deftech\CodeLibrary\2.0"
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    No worries - at least you're trying . (Some people seem to expect you to write their projects for them )

    The value name is the second parameter "vSubKey" in
    vb Code:
    1. RegSetValueEx lngKeyHandle,  vSubKey, 0&, REG_DWORD, 25, 4
    which (originally) references
    vb Code:
    1. vSubKey = "Software\Deftech\CodeLibrary\2.0"

    Actually, I've just spotted a flaw in my previous post . RegSetValueEx expects a pointer to a variable, not the property of the form directly. So to save the values you have to do:

    vb Code:
    1. Dim FrmLeft as long
    2. 'etc
    3. 'etc...
    4.  
    5. FrmLeft = Form1.Left
    6. 'etc
    7. 'etc...
    8.  
    9. RegSetValueEx lngKeyHandle, "Left", 0&, REG_DWORD, FrmLeft, 4
    10. 'etc
    11. 'etc...

  20. #20

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Quote Originally Posted by schoolbusdriver
    No worries - at least you're trying
    that has been said many times

    Quote Originally Posted by schoolbusdriver
    Some people seem to expect you to write their projects for them
    Well, that would certainly be a hell of a lot quicker, but then again, I am doing this because I am trying to teach myself something.

    Thanks alot for all this. It now gives me a decent basic understanding of how to read/write to the registry.

    Quote Originally Posted by schoolbusdriver
    Do you have reading code ?
    Not too sure, but an example would be good please. Is it using the GetSetting?

    Is this a speciallity of yours, as there seem to be a lot of posts from you on this topic?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Quote Originally Posted by aikidokid
    ...I am doing this because I am trying to teach myself something.

    Quote Originally Posted by aikidokid
    ...Is it using the GetSetting?
    Nah! See below.
    Quote Originally Posted by aikidokid
    Is this a speciallity of yours, as there seem to be a lot of posts from you on this topic?
    Yes

    vb Code:
    1. Dim lngKeyHandle As Long
    2. Dim lngDataType As Long
    3. Dim lngDataSize As Long
    4. Dim lngRetval As Long
    5.    
    6. Const lnghKey = HKEY_LOCAL_MACHINE
    7. Const strSubKey = "Software\Deftech\CodeLibrary\2.0"
    8. Const strValueName = "Left"
    9.  
    10. 'Open the key.
    11.    If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle) = ERROR_SUCCESS Then
    12. 'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
    13.       If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) _
    14.          = ERROR_MORE_DATA Then
    15. 'Test for DWORD value.
    16.          If lngDataType = REG_DWORD Then
    17. 'Get the key`s content.
    18.             If RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, lngRetval, lngDataSize) <> ERROR_SUCCESS Then MsgBox "OOPS!"
    19.          End If
    20.       End If
    21.    End If
    22. 'Close any key opened with RegOpenKeyEx.
    23.    Call RegCloseKey(lngKeyHandle)
    24.  
    25. Debug.Print lngRetval

    As before, I've done this on the fly, so I've not run it.....

  22. #22

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    I am now getting an error on this line, saying type mismatch!

    vb Code:
    1. RegSetValueEx lngKeyHandle, "Left", 0&, "REG_DWORD", lngFrmLeft, 4

    But from what you have shown above, this should be correct, shouldn't it?

    lngKeyHandle = HKEY_LOCAL_MACHINE
    "Left" = The name given to the key
    lngFrmLeft = Me.Left

    vb Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2. Dim lngKeyHandle As Long
    3. Dim lngFrmLeft As Long
    4. Dim lngFrmTop As Long
    5. Dim Ret As Long
    6. Dim Result As Long
    7.  
    8. Const strSubKey = "Software\Deftech\CodeLibrary\2.0"
    9.  
    10. FrmLeft = Me.Left
    11. FrmTop = Me.Top
    12.  
    13. 'open keys
    14. 'Check if the specified key exists
    15. Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle)
    16.  
    17. If Result <> 0 Then
    18.     'Key doesn't exist. 0 signifies success.
    19.     'Create a new key
    20.     Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Left", ByVal 0&, REG_DWORD, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lngKeyHandle, Ret)
    21.     If Result <> 0 Then '0 signifies success.
    22.         MsgBox "Error while creating the Key!!"
    23.         Exit Sub
    24.     End If
    25.    
    26.     'Open the new key.
    27.     RegOpenKeyEx HKEY_LOCAL_MACHINE, "Left", 0&, KEY_ALL_ACCESS, lngKeyHandle
    28. End If
    29.  
    30. 'Now that we have a valid handle - Give it a value
    31. 'The 4 signifies the length of the value. For a DWORD (Long) it's always 4.
    32. RegSetValueEx lngKeyHandle, "Left", 0&, "REG_DWORD", lngFrmLeft, 4
    33. 'close the opened key
    34. RegCloseKey lngKeyHandle
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    See my little (almost unnoticeable) edit in post 15
    Quote Originally Posted by schoolbusdriver

    RegSetValueEx lngKeyHandle, vSubKey, 0&, REG_DWORD, 25, 4

    EDIT: REG_DWORD should not be in quotation marks.

  24. #24

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Quote Originally Posted by schoolbusdriver
    See my little (almost unnoticeable) edit in post 15
    I do now

    When I am opening the form, and checking to see if the key exists, I am now getting an error on this line saying "ERROR_SUCCESS" is not defined.

    [B]EDIT[\B]Also with ERROR_MORE_DATA as well, same error.

    Is this your variable, or is it from the API?

    vb Code:
    1. If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle) = ERROR_SUCCESS Then

    I won't give up if you dont!
    Last edited by aikidokid; Mar 9th, 2007 at 07:27 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    Quote Originally Posted by aikidokid
    I won't give up if you dont!
    I won't, but will have to go and do a couple of hours work soon

    ERROR_SUCCESS is a standard API constant of 0. REGISTRY_SUCCESS, which I noticed somewhere in the first couple of posts is the same.

    EDIT:

    'Registry return values.
    Public Const ERROR_SUCCESS = 0&
    Public Const ERROR_MORE_DATA = 234
    Public Const ERROR_NO_MORE_ITEMS = 259&
    Last edited by schoolbusdriver; Mar 9th, 2007 at 07:31 AM.

  26. #26

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Thanks once again.

    Quote Originally Posted by schoolbusdriver
    I won't, but will have to go and do a couple of hours work soon
    I have a couple of days off now, so I am trying to get stuck into this.

    Not sure where you are, but from something you said in one of your threads, I think there is a time difference between us.

    I'm sure I will post again, I am just grateful for an answer whenever you can
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: Save to registry, but it doesn't!

    No problem, (UK based). I'll help when I can

  28. #28

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    YEAHHHH

    I've finally got this sorted, reading and writing

    A big thanks to SCHOOLBUSDRIVER for all you help, time and patience.

    It's people like you that make this forum what it is.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  29. #29

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED by schoolbusdriver]Save to registry, but it doesn't!

    Hmm, thought I did.

    I am getting the error:
    User defined type may not be passed ByVal

    on this line of code:

    vb Code:
    1. Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "StartOption", ByVal 0&, REG_DWORD, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lngKeyHandle, Ret)

    on the "ByVal 0&" part.

    I have this line of code elsewhere and it works without this error:

    vb Code:
    1. Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Left", ByVal 0&, REG_DWORD, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lngKeyHandle, Ret)

    Now I'm not too sure what this means, but looking at the API code it says:
    lpSecurityAttributes As Any
    But when I type the code out, the intellisense (or what ever the tooltiptext is called) it says
    lpSecurityAttributes As SECURITY_ATTRIBUTES

    Does this matter, or make any sense?

    vb Code:
    1. Dim lngKeyHandle As Long
    2. Dim Ret As Long
    3. Dim Result As Long
    4.  
    5. Const strSubKey = "Software\Deftech\CodeLibrary\2.0"
    6.  
    7. 'SelectedOption holds the option button index number
    8. 'Check if the specified key exists
    9. Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle)
    10.  
    11. If Result <> 0 Then
    12.     'Key doesn't exist. 0 signifies success.
    13.     'Create a new key
    14.     Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "StartOption", ByVal 0&, REG_DWORD, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lngKeyHandle, Ret)
    15.     If Result <> 0 Then '0 signifies success.
    16.         MsgBox "Error while creating the Key!!"
    17.         Exit Sub
    18.     End If
    19.  
    20.     'Open the new key.
    21.     RegOpenKeyEx HKEY_LOCAL_MACHINE, "StartOption", 0&, KEY_ALL_ACCESS, lngKeyHandle
    22. End If
    23.  
    24. 'Now that we have a valid handle - Give it a value
    25. 'The 4 signifies the length of the value. For a DWORD (Long) it's always 4.
    26. RegSetValueEx lngKeyHandle, "StartOption", 0&, REG_DWORD, SelectedOption, 4
    27. 'close the opened key
    28. RegCloseKey lngKeyHandle
    Last edited by aikidokid; Mar 9th, 2007 at 08:46 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  30. #30

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Save to registry, but it doesn't!

    Hmmm again!

    Tried it a few times and it didn't work, now I've tried it again, after restarting VB and it works each time!

    Time for a coffee break
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

    Ah!. (Just got back). That error message may be because of the way the RegCreateKeyEx API is declared. The 7th parameter may be declared as "lpSecurityAttributes as SECURITY_ATTRIBUTES" - a UDT. Declaring it as a Long should stop any errors. ie:

    vb Code:
    1. Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    2.    (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
    3.    ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    4.    lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long

  32. #32

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

    Thanks for getting back.

    Quote Originally Posted by schoolbusdriver
    Declaring it as a Long should stop any errors
    I only had the problem in one place, and as I said it doesn't happen anymore.

    Should I still change it, and would it affect the other places where I have used it?

    I would guess not, but I aint that sure
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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

    Re: [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

    If it aint broke....

    If lpSecurityAttributes is declared as "Any" it won't matter, as we're passing a NULL (0&) anyway. For myself, I'd be inclined to either change it or at least add some comments (such as the URL to this thread ! ) for future reference. The SECURITY_ATTRIBUTES structure is useful if you're intending to create (child) subkeys that, for instance, inherit the permissions of the parent subkey - not really an issue here as it's for your personal use (logged in as administrator).

  34. #34

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: [RESOLVED by schoolbusdrive]Save to registry, but it doesn't!

    Quote Originally Posted by schoolbusdriver
    I'd be inclined to either change it or at least add some comments (such as the URL to this thread ! ) for future reference.
    An excellent idea, I never thought of this, but it's what I will do for now, as you said, "if it aint broke ..."
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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