Solved

Writing to registry

Posted on 2001-06-29
4
255 Views
Last Modified: 2012-06-27
Hi All,
I would like to know how can i write to the registry
to this direction:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\margin_top .

i would like to change it.
if anyone can show me an example that doing that, it will be greate.

and another thing i have notice that if i am changing
the margin from the explorer, for example puting 19.05,
in the registry it will be 0.75000.
so what i am asking is how does it happen ?

Thanks & Regards.
0
Comment
Question by:Caesar-man
4 Comments
 
LVL 20

Expert Comment

by:hes
ID: 6238643
0
 

Accepted Solution

by:
dignardg earned 75 total points
ID: 6239284
You can use the following code below for registry key setting and editting..

With regards to the changing of 19.05 to 0.7500
Pehaps it is a metric to imperial conversion.

19mm = 0.75 Inches?? (no time to calculate thie to see, but sounds right)

Regards..

Glen Dignard
dignardg@hotmail.com



Usage
------

If SetRegistryValue(HKEY_CURRENT_USER , "Software\MySoft\RegisteredTO", "Me") <> 0 Then
     'Success
Else
     'Error
End If


Code
-------


'ID:            Registry
'Description:   Module to hold Registry/API functions
'Created:       February 05, 2001

Option Explicit     'Require declaration

Private Const g_modname = "Registry"

'Standard general registry constants
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const ERROR_SUCCESS = 0&
Private Const REG_SZ = 1
Private Const REG_DWORD = 4

'Standard registry security constants
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const SYNCHRONIZE = &H100000
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _
Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))

'Security UDT used by registry functons
Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

'Declare API Registry function

Private 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

'Private 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 SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long

Private Declare Function RegSetLongValue Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Long, _
ByVal cbData As Long) As Long

Private Declare Function RegSetStringValue Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, _
ByVal cbData As Long) As Long

Private Declare Function RegQueryLongValue Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long

Private Declare Function RegQueryStringValue Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long

Public Declare Function GetTickCount Lib "kernel32" () As Long

'GMT SystemTime UDT used in time functions
Public Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type

'GMT TimeZOeInformation UDT used in time functions
Public Type TIME_ZONE_INFORMATION
        Bias As Long
        StandardName(32) As Integer
        StandardDate As SYSTEMTIME
        StandardBias As Long
        DaylightName(32) As Integer
        DaylightDate As SYSTEMTIME
        DaylightBias As Long
End Type

'Declare API time functions
Public Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long


' Name:         GetRegistryValue
' Author:       Glen Dignard
' Created:      February 01, 2001
'
' Description:  Gets the specified registry value on the local computer
' Inputs:       lngKey          - Previously opened key, if 0 => HKEY_LOCAL_MACHINE is assumed
'               strValueName    - String specifying the registry value name
'               varValue        - Variant which is filled with the registry value
'
' Notes:        Only Long and String values (upto 255 chars) are supported.
'               If strValueName = "" the default value is retrieved.

Public Function GetRegistryValue(ByVal lngKey As Long, ByVal strValueName As String, ByRef varValue As Variant) As Long
   
    On Error GoTo ErrorHandle
   
    ' Declare function variables
    Dim lngValue As Long
    Dim strValue As String
    Dim lngTest As Long
    Dim lngLen As Long
       
    Select Case VarType(varValue)
    Case vbInteger
        lngTest = RegQueryLongValue(lngKey, strValueName, 0, REG_DWORD, lngValue, 4)
        varValue = lngValue
   
    Case vbLong
        lngTest = RegQueryLongValue(lngKey, strValueName, 0, REG_DWORD, lngValue, 4)
        varValue = lngValue
     
    Case vbString
        strValue = Space$(255)
        lngLen = CLng(Len(strValue))
        lngTest = RegQueryStringValue(lngKey, strValueName, 0, REG_SZ, strValue, lngLen)
        varValue = Left(strValue, lngLen - 1)
        varValue = Trim(varValue)
    End Select
   
    If lngTest <> ERROR_SUCCESS Then
        GetRegistryValue = lngTest
    Else
        GetRegistryValue = 0
    End If
   
Exit_Sub:
   
    Exit Function
   
ErrorHandle:
   
    CtxRaiseError Err.Number, Err.Description, g_modname, "GetRegistryValue()"
    Resume Exit_Sub
       
End Function


' Name:         OpenRegistryKey
' Author:       Glen Dignard
' Created:      February 01, 2001
'
' Description:  Opens the specified registry key on the local computer
' Inputs:       lngKey      - May specify a previously opened key
'                             If value is 0 then HKEY_LOCAL_MACHINE is assumed
'               strSubKey  - String specifying the keyname
'
' Notes:        Only Long and String values are supported. Max string size is 255.
'               If szValueName = "" the default value is retrieved.

Public Function OpenRegistryKey(ByRef lngKey As Long, ByRef strSubKey As String) As Long
   
On Error GoTo ErrorHandle
       
    ' Declare function variables
    Dim lngTest As Long
    Dim hTmpKey As Long
   
    ' Check the input parameters
    If strSubKey = "" Then
        Err.Raise EnumErrors.REGISTRY_ERROR, , "tpx"
    End If
   
    If lngKey < 1 Then
        hTmpKey = HKEY_LOCAL_MACHINE
    Else
        hTmpKey = lngKey
    End If
   
    ' Open the key
    lngTest = RegOpenKeyEx(hTmpKey, strSubKey, 0, KEY_ALL_ACCESS, hTmpKey)
   
    If lngTest <> ERROR_SUCCESS Then
        OpenRegistryKey = lngTest
    Else
        OpenRegistryKey = 0
    End If
   
    lngKey = hTmpKey

Exit_Sub:
   
    Exit Function
   
ErrorHandle:
   
    CtxRaiseError Err.Number, Err.Description, g_modname, "OpenRegistryKey()"
    Resume Exit_Sub
   
End Function


' Name:         SetRegistryValue
' Author:       Glen Dignard
' Created:      February 01, 2001
'
' Description:  Sets the specified registry value on the local computer
' Inputs:       lngKey      - May specify a previously opened key
'                             If value is 0 then HKEY_LOCAL_MACHINE is assumed
'               strValueName - String specifying the registry value name
'               varValue    - Variant holding the registry value
' Notes:        Only Long and String values (max 255 char) are supported.
'               If strValueName = "" the default value is set.


Function SetRegistryValue(ByVal lngKey As Long, ByVal strValueName As String, ByVal varValue As String) As Long
   
    On Error GoTo ErrorHandle
   
    ' Declare function variables
    Dim lngValue As Long
    Dim strValue As String
    Dim lngTest As Long
       
    Select Case VarType(varValue)
    Case vbInteger
        lngValue = CLng(varValue)
        lngTest = RegSetLongValue(lngKey, strValueName, 0, REG_DWORD, lngValue, 4)
   
    Case vbLong
        lngValue = CLng(varValue)
        lngTest = RegSetLongValue(lngKey, strValueName, 0, REG_DWORD, lngValue, 4)
   
    Case vbString
        strValue = CStr(varValue)
        lngTest = RegSetStringValue(lngKey, strValueName, 0, REG_SZ, strValue, CLng(Len(strValue) + 1))
    End Select
   
    If lngTest <> ERROR_SUCCESS Then
        SetRegistryValue = lngTest
    Else
        SetRegistryValue = 0
    End If

Exit_Function:
   
    Exit Function
   
ErrorHandle:
   
    CtxRaiseError Err.Number, Err.Description, g_modname, "SetRegistryValue()"
    Resume Exit_Function
   
End Function
0
 
LVL 27

Expert Comment

by:Ark
ID: 6240103
Dim objWSHShell As Object
Set objWSHShell = CreateObject("WScript.Shell")
objWSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\margin_top","0.75000"
Set  objWSHShell = Nothing

Cheers
0
 

Author Comment

by:Caesar-man
ID: 6240624
Thanks Ark,
You Are The Best
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

18 Experts available now in Live!

Get 1:1 Help Now