Link to home
Start Free TrialLog in
Avatar of nvms
nvms

asked on

Easy way to make Get(save)Setting Local Machine not user

Re: Easy way to make Get(save)Setting Local Machine not user
I have an NT client with multiple users visiting a terminal each day. I have written a program which used SaveSetting and GetSetting to save to the registry, however, it defaults to the
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\omniterm folder.

This is a pain because program settings change between users.

Is there a SIMPLE way to have the data stored in the LOCALMACHINE area so settings are common to the PC not the user. I have GetSetting calls all over my app and so dont want to complex a solution. Maybe having WriteSetting and ReadSetting function that require the same parameters passed to them but save the data in the desired location. That way I could just store the functions in my global.bas and do a global replace on the function names

Cheers as always

Stewart (NVMS)
Avatar of AssafLavie
AssafLavie

You can write your own function which will look very similar to the GetSetting function but will save the data to the HKLM branch. It should be easy enough using the WSH registry functions.
Try with Windows API like

Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegSetValueExA Lib "advapi32.dll" (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

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long


bye
Check out http://www.carobit.com/PandE/VBSamples/VBAll/shellexecute.zip

Never mind the shellexecute functionality in this project, but look at the handy Registry class that comes with it, that allows you to read and write to any location you fancy in the Registry.

Of course, use at your own peril. <g>
Here is another quick and dirty method, the following code goes in a module. You then only need to globally replace the SaveSetting/GetSetting/DeleteSetting in your project with OSaveSetting/OGetSetting/ODeleteSetting. (Assumes that you have the windows scripting host installed on the machine  - WSHOM.OCX).

Private wshShell As Object

Public Function OGetSetting(ByVal AppName As String, ByVal Section As String, ByVal KeyName As String, ByVal Default As Variant, Optional ByVal System As Boolean = True) As String
    Set wshShell = CreateObject("WScript.Shell")
    On Error GoTo errBadRead
    OGetSetting = wshShell.RegRead(IIf(System, "HKLM", "HKCU") & "\Software\My Company\" & AppName & "\" & Section & "\" & KeyName)
    Set wshShell = Nothing
    Exit Function
errBadRead:
    OGetSetting = Default
    Set wshShell = Nothing
End Function

Public Sub OSaveSetting(ByVal AppName As String, ByVal Section As String, ByVal KeyName As String, ByVal Value As Variant, Optional ByVal System As Boolean = True)
    Set wshShell = CreateObject("WScript.Shell")
    On Error GoTo errBadWrite
    Call wshShell.RegWrite(IIf(System, "HKLM", "HKCU") & "\Software\My Company\" & AppName & "\" & Section & "\" & KeyName, Value)
    Set wshShell = Nothing
    Exit Sub
errBadWrite:
    Set wshShell = Nothing
End Sub

Public Sub ODeleteSetting(ByVal AppName As String, ByVal Section As String, ByVal KeyName As String, Optional ByVal System As Boolean = True)
    Set wshShell = CreateObject("WScript.Shell")
    On Error GoTo errBadDelete
    Call wshShell.RegDelete(IIf(System, "HKLM", "HKCU") & "\Software\My Company\" & AppName & "\" & Section & "\" & KeyName)
    Set wshShell = Nothing
    Exit Sub
errBadDelete:
    Set wshShell = Nothing
End Sub
<Ping>
ASKER CERTIFIED SOLUTION
Avatar of LadyVyxen
LadyVyxen
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
LadyVyxen is back ;-)
*smiles warmly*
Did you miss me?
LV.
<hug>
<Blushes>

Apparently. Never frequented you in the Lounge, but let's say I've heared about you ;-)
Avatar of nvms

ASKER

Wow! So much dialog. I have been overseas and have just returned to much including this project. Ms Vixen, that's an awfully large code segment, is it all required ? So tempting to use an .ini file! Shall get into code mode shortly and follow up on this

Thanks again

Stewart
LOL. Now I'm worries. (Heard about me...? *ponders*)
*tickles you all mercilessly*

Stewart: There are a number of functions in there for reading/writing to any part of the registry, and to any registry type (DWORD/Extended String format, etc.)

Remember: Only the *functions used* in a module get compiled. That's why we have file libraries :>

*bounce*

LV.
Hello nvms,
Please respond to the experts informing them of your progress here.


TonyS
Community Support Moderator @ Experts Exchange
There has been no activity in this question in quite some time, and it looks like it has been abandoned. As part of our ongoing mission to clean up the topic areas, a Moderator will finalize this question within the next seven (7) days. At that time, either I or one of the other Moderators will force/accept the comment of LadyVyxen.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER. If you have further comments on this question or the recommendation, please leave them here.

nvms,

Some of these questions have been open for some time. Please resolve them appropriately as soon as possible.

https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20091307
https://www.experts-exchange.com/jsp/qShow.jsp?ta=hardgen&qid=20189857
https://www.experts-exchange.com/jsp/qShow.jsp?ta=fonts&qid=11466658
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20246539
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20169234
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20156218
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20148915
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20144898
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20113147
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20091309
https://www.experts-exchange.com/jsp/qShow.jsp?ta=msoutlook&qid=20185178

Thanks,

Netminder
Community Support Moderator
Experts Exchange
Force/accepted by
Netminder
Community Support Moderator
Experts Exchange