Solved

Set REG_EXPAND_SZ" value in the registry

Posted on 2004-08-27
4
1,175 Views
Last Modified: 2012-05-05
Hi all,

This code create a REG_SZ value, I want to create a REG_EXPAND_SZ value !
                        Dim regKey As RegistryKey
                        regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer)
                        regKey = regKey.OpenSubKey("SYSTEM\CurrentControlSet\Services\MyService", True)
                        regKey.SetValue("Path", "%systemroot%\system32" & Chr(0))
                        regKey.Close()
ARGH!

Do you have any idea ?

Thanks in advance,

Didier


0
Comment
Question by:Yerk
  • 2
  • 2
4 Comments
 
LVL 41

Expert Comment

by:graye
ID: 11923785
Sorry... you're gonna have to use the Win32 DLL versions or the WMI Registry provider.

Here is a quote from the .Net documentation:

There is no way to control whether the information being passed is stored as an sz, or an expanded_sz string, and therefore, all string values are interpreted as standard sz values.

Let us know if you need examples....
0
 

Author Comment

by:Yerk
ID: 11925668
Thanks for your help, if you have any examples, they are welcome...

0
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 11927916
Here you go....

    'LONG RegConnectRegistry(
    '  LPCTSTR lpMachineName,
    '  HKEY hKey,
    '  PHKEY phkResult
    ');
    Declare Auto Function RegConnectRegistry Lib "Advapi32" ( _
    ByVal lpMachineName As String, _
    ByVal hKey As Integer, _
    ByRef phkResult As Integer) As Integer

    'LONG RegCloseKey(
    '  HKEY hKey
    ');
    Declare Auto Function RegCloseKey Lib "Advapi32" ( _
    ByVal hKey As Integer) As Integer

    'LONG RegOpenKeyEx(
    '  HKEY hKey,
    '  LPCTSTR lpSubKey,
    '  DWORD ulOptions,
    '  REGSAM samDesired,
    '  PHKEY phkResult
    ');
    Declare Auto Function RegOpenKeyEx Lib "Advapi32" ( _
    ByVal hKey As Integer, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Integer, _
    ByVal samDesired As Integer, _
    ByRef phkResult As Integer) As Integer

    'LONG RegSetValueEx(
    '  HKEY hKey,
    '  LPCTSTR lpValueName,
    '  DWORD Reserved,
    '  DWORD dwType,
    '  const BYTE* lpData,
    '  DWORD cbData
    ');
    Declare Auto Function RegSetValueEx Lib "Advapi32" ( _
    ByVal hKey As Integer, _
    ByVal lpValueName As String, _
    ByVal Reserved As Integer, _
    ByVal dwType As Integer, _
    ByVal lpData As String, _
    ByVal cbData As Integer) As Integer

    ' Pre-defined Key values
    Const HKEY_CLASSES_ROOT = &H80000000
    Const HKEY_CURRENT_USER = &H80000001
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const HKEY_USERS = &H80000003
    Const HKEY_PERFORMANCE_DATA = &H80000004
    Const HKEY_CURRENT_CONFIG = &H80000005
    Const HKEY_DYN_DATA = &H80000006

    ' Registry data types
    Const REG_SZ = 1
    Const REG_EXPAND_SZ = 2
    Const REG_BINARY = 3
    Const REG_DWORD = 4
    Const REG_MULTI_SZ = 7I

    ' Access Control for Registry
    Const READ_CONTROL = &H20000
    Const SYNCHRONIZE = &H100000
    Const STANDARD_RIGHTS_ALL = &H1F0000
    Const STANDARD_RIGHTS_READ = READ_CONTROL
    Const STANDARD_RIGHTS_WRITE = READ_CONTROL
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_CREATE_LINK = &H20
    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))
    Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
       KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
    Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE _
       Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

        'Dim regKey As RegistryKey
        'regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer)
        'regKey = regKey.OpenSubKey("SYSTEM\CurrentControlSet\Services\MyService", True)
        'regKey.SetValue("Path", "%systemroot%\system32" & Chr(0))
        'regKey.Close()

        Dim regKey, subKey As Integer
        RegConnectRegistry(strComputer, HKEY_LOCAL_MACHINE, regKey)
        RegOpenKeyEx(regKey, "SYSTEM\CurrentControlSet\Services\MyService", 0, KEY_WRITE, subKey)
        RegSetValueEx(subKey, "Path", 0, REG_EXPAND_SZ, "%SystemRoot%\System32", (Len("%SystemRoot%\System32") + 1)*2)
        RegCloseKey(subKey)
        RegCloseKey(regKey)
0
 

Author Comment

by:Yerk
ID: 11929968
Thanks a lot, your code makes the job perfectly

Didier
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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