Solved

SaveSetting position

Posted on 2000-05-12
9
320 Views
Last Modified: 2010-05-02
When I use the SaveSetting / GetSetting commands in VB to write / retrieve registry values, it is stored always in the branch "VB and VBA Program Settings" under "Software" under "HKEY_CURRENT_USER".

The question is: how can I make the values to be forced to be stored in other place (i.e. under "Software" under "HKEY_LOCAL_MACHINE")?
0
Comment
Question by:BETTY
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
u need to use the registry API.
This is not mine but i was provided this link when i asked the same q.
http://homepage.eircom.net/~carafa/VBSamples/VBAll/shellexecute.zip
0
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
0
 
LVL 9

Accepted Solution

by:
GivenRandy earned 50 total points
Comment Utility
Use these functions:

-----
Public Function ReadRegistry(ByVal Key As String, ByVal Name As String, ByVal DefaultValue) As String
    Dim KeyHandle As Long
    Dim Result As Long
    Dim LType As Long
    Dim Data As String
    Dim DataLen As Long
     
    ReadRegistry = DefaultValue
    Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, KeyHandle)
    If (Result = ERROR_SUCCESS) Then
        DataLen = 500
        Data = String$(DataLen, 0)
        Result = RegQueryValueEx(KeyHandle, Name, 0, LType, ByVal Data, DataLen)
        If (Result = ERROR_SUCCESS) Then
            ReadRegistry = Left$(Data, DataLen - 1)
        End If
    End If
    Call RegCloseKey(KeyHandle)
End Function

Public Sub WriteRegistry(ByVal Section As String, ByVal Key As String, ByVal ValType As Integer, ByVal Value As Variant)
    Dim lResult As Long
    Dim lKeyValue As Long
    Dim InLen As Long
    Dim lNewVal As Long
    Dim sNewVal As String
     
    On Error Resume Next
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, Section, lKeyValue)
    If ValType = ValDWord Then
       lNewVal = CLng(Value)
       InLen = 4
       lResult = RegSetValueExLong(HKEY_LOCAL_MACHINE, Key, 0&, ValType, lNewVal, InLen)
    Else
       If ValType = ValString Then Value = Value & Chr(0)
       sNewVal = Value
       InLen = Len(sNewVal)
       lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
    End If
    lResult = RegFlushKey(lKeyValue)
    lResult = RegCloseKey(lKeyValue)
    On Error GoTo 0
End Sub
-----

You'll need this information in a module:

-----
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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
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
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
-----
0
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
I use a class that works as a wrapper around the registry API's, you can get it from..

http://www.cyd.liu.se/~freqv416/clsRegistry.cls
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
if you go to project -> references and add an reference to the "windows scripting runtime you can implement something like this:

Dim wshShell As New IWshShell_Class
wshShell.RegWrite _
"HKEY_CURRENT_USER\" _
      & "Software\" _
      & "Microsoft\" _
      & "Internet Explorer\" _
      & "Main\" _
      & "Start Page" _
      , strMyRegistryValue

wshShell.Regwrite will overwrite the value if the key is found or add it to the registry if it is not

<code compliments of another expert at this site...wsh2>


     
0
 
LVL 9

Expert Comment

by:GivenRandy
Comment Utility
Did we help any?
0
 
LVL 1

Author Comment

by:BETTY
Comment Utility
I'm sorry for the delay.
I'm changing my job place and I have been very very busy. I think in a week I will be able to evaluate and compare your comments.
Thanks for the patience...
0
 
LVL 1

Author Comment

by:BETTY
Comment Utility
Hi after this long delay! I'm very sorry.

Well, and the winner is... all of you, for trying to help me.
The points go to GivenRandy, and that's why: all the options finally worked in a simple testing, with a maximum effort of retouching a couple of lines, but GivenRandy was a "direct" answer, without having to search for any other place (Anyway the links were interesting, CrazyMan). And I wasn't able to locate the Windows Scripting Runtime under References nor under Components. Finally, GivenRandy's option does not use classes, where I am a complete newbie.

Thanks to all, and sorry one more time for the delay.
0
 
LVL 1

Author Comment

by:BETTY
Comment Utility
Comment accepted as answer
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now