Solved

Set REG_EXPAND_SZ" value in the registry

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

632 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