Solved

Registry Question

Posted on 1998-07-08
2
131 Views
Last Modified: 2010-05-03
I need code that creates a key in the registry.  However, one of the keys that it gets created under must be a command line variable.

The key it must create is:
HKEY_USERS\ * \Software\Microsoft\EXCHANGE

where the '*' is the command line parameter passed.

The key that it creates should be empty.

I need VB5 Source Code.  No OCX's should be used either.
0
Comment
Question by:johnstar
  • 2
2 Comments
 
LVL 4

Accepted Solution

by:
tomook earned 200 total points
ID: 1465255
This can all be done with API calls, so no problem. Error handling and other niceties are left up to you.

Option Explicit

Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Const MAX_PATH = 260
Public Const SYNCHRONIZE = &H100000
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const ERROR_SUCCESS = 0&
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Public Const REG_CREATED_NEW_KEY = &H1                      ' New Registry Key created
Public Const REG_OPENED_EXISTING_KEY = &H2                      ' Existing Key opened
Public Const STANDARD_RIGHTS_ALL = &H1F0000

Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Function CreateMyKey(ByVal MyPart As String) As Long    
    Dim lReturn As Long
    Dim hKey As Long
    Dim lpSubKey As String
    Dim Reserved As Long
    Dim lpClass As String
    Dim samDesired As Long
    Dim phkResult As Long
    Dim lpdwDisposition As Long
    Dim bTmp1 As Boolean
    Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
    Dim dwOptions As Long

    '   Initialize Objects
    hKey = HKEY_USERS
    lpSubKey = MyPart & "\Software\Microsoft\EXCHANGE"
    Reserved = 0
    lpClass = ""
    samDesired = KEY_WRITE
    dwOptions = REG_OPTION_NON_VOLATILE
    With lpSecurityAttributes
        .nLength = 0
        .lpSecurityDescriptor = 0
        .bInheritHandle = 0
    End With
   
    lReturn = RegCreateKeyEx(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
   
    If lReturn = REG_CREATED_NEW_KEY Then
        ' Created new key
    ElseIf lReturn = REG_OPENED_EXISTING_KEY Then
        ' Key already existed.
    End If
   
    lReturn = RegCloseKey(phkResult)
End Function

0
 
LVL 4

Expert Comment

by:tomook
ID: 1465256
I should also note that this will work on 95 and NT.
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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

17 Experts available now in Live!

Get 1:1 Help Now