Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Set REG_EXPAND_SZ" value in the registry

Posted on 2004-08-27
4
Medium Priority
?
1,203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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