Solved

Set REG_EXPAND_SZ" value in the registry

Posted on 2004-08-27
4
1,169 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

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

19 Experts available now in Live!

Get 1:1 Help Now