Solved

Set REG_EXPAND_SZ" value in the registry

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 Error 7 56
DataGridView 2 29
Total in textboxes 9 35
Find value in LINQ result ? 4 18
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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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