You are on page 1of 1

Find Code: Search Advanced Search

Home New Hot Best Forum


News Developers Links Newsletters Submit Code

Write a Binary Value to the Registry

Author: Intelligent Solutions Inc. (Featured Developer)


Category: Registry
Type: Snippets
Difficulty: Intermediate

Version Compatibility: Visual Basic 5 Visual Basic 6

This code has been viewed 15782 times.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Option Explicit

Public Enum REG_TOPLEVEL_KEYS


HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_CONFIG = &H80000005
HKEY_CURRENT_USER = &H80000001
HKEY_DYN_DATA = &H80000006
HKEY_LOCAL_MACHINE = &H80000002
Code:
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_USERS
Private Function =WriteBinaryToRegistry(Hkey
&H80000003 As _
End Enum
REG_TOPLEVEL_KEYS, strPath As String, strValue As String, _
binData As Variant) As Boolean

Private
'WRITES Declare Function
A BINARY VALUERegCreateKey
TO REGISTRY:Lib _
"advapi32.dll" Alias "RegCreateKeyA" _
'PARAMETERS:
(ByVal Hkey As Long, ByVal lpSubKey As _
String,
'Hkey: phkResult
Top Level Key AsasLong) As Long
defined by
'REG_TOPLEVEL_KEYS Enum (See Declarations)
Private Declare Function RegCloseKey Lib _
"advapi32.dll"
'strPath (ByVal
- 'Full Path Hkey As Long) As Long
of Subkey
'if path does not exist it will be created
Private Declare Function RegSetValueEx Lib _
"advapi32.dll"
'strValue Alias "RegSetValueExA" _
ValueName
(ByVal Hkey As Long, ByVal _
lpValueName
'binData - Value As String, ByVal _
Data
Reserved As Long, ByVal dwType _
As Long,
'Returns: lpData
True As Any, ByVal
if successful, false _otherwise
cbData As Long) As Long
'EXAMPLE
Private
'Dim Const
v As REG_BINARY = 3
Variant
No Text Boxes
'Open "C:\myword.doc" For Binary As #1
'v = Input(LOF(1), #1)
About
'Close #1 Legal Awards Contact Privacy Advertise
'WriteBinaryToRegistry(HKEY_LOCAL_MACHINE, _
"Software\MySofware", "My Binary Data", v)

Dim bAns As Boolean

On Error GoTo ErrorHandler


DevX Network:
Dim keyhand As Long Code Library | Discussions | Tips | Newsletters | MarketPlace | Premier Club | Product Bank |
Dim r As Long Skill Building | White Papers
r = RegCreateKey(Hkey,
Partners: Bitpipe |strPath, keyhand)
CodeHound | CoDe Magazine | ComponentSource | DigitalGuru | IndustryBrains | Jupiter Research |
If r = 0 Then
KnowledgeStorm | NetLine | SourceBank
r = RegSetValueEx(keyhand, strValue, 0, _
REG_BINARY, binData, Len(binData))
r = RegCloseKey(keyhand)
End If

WriteBinaryToRegistry = (r = 0)

You might also like