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
Solved

small registry function

Posted on 2006-06-20
15
334 Views
Last Modified: 2008-02-01

hello experts,

i am developing an application that has to be as small as possible,
but now i discover i need to write to the registry

what would be the smallest and most portable way?
0
Comment
Question by:Mark_FreeSoftware
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16941348
what do you mean by small?

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.RegWrite "HKCU\MyRegKey\", "Top level key"


from

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_11153287.html
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16942190

SaveSetting, GetSetting: are 2 functions used in vb6 mostly

SaveSetting= write to registry

GetSetting = read fro mregistry

SaveSetting "MyCompany\MyAppName", "Printers", "Pr1", "def printer"
....or...
SaveSetting "MyCompany", "MyAppName\Printers", "Pr1", "def printer"

Just be sure the args do ~not~ include leading or trailing delimiters.
VB does not filter them and the underlying registry Api chokes on
them.

If you need to go beyond "HKEY_CURRENT_USER\Software\VB and VBA
Program Settings", then you'll need to use the registry Api. There are
a number of free reg classes and code, including one of the more popular at

http://vbaccelerator.com/home/index.asp

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16942343
This depends, do you want to rely on scripting? Do you want to use API?

What exactly do you need? Do you need to write to a part of the registry that already exists or do you need to create your own key? Are you only saving values or are you going to need to query the values as well?

If you only need to write values than it can be done in 4-5 lines using API
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 250 total points
ID: 16943259
'Assuming you only want to write string values and or create a key to store your information in then API
'Its about 4 lines but how I format it looks larger.

Option Explicit

Private Declare Function RegCreateKeyExA Lib "advapi32" _
    (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal Reserved As Long, _
    ByVal lpClass As String, _
    ByVal dwOptions As Long, _
    ByVal samDesired As Long, _
    ByVal lpSecurityAttributes As Long, _
    phkResult As Long, _
    lpdwDisposition As Long) As Long

Private Declare Function RegSetValueExA Lib "advapi32.dll" _
   (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    ByVal lpValue As String, _
    ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long

Public Enum PHKEY
    HKEY_CURRENT_USER = &H80000001
    HKEY_LOCAL_MACHINE = &H80000002
End Enum

Public Function WriteValue( _
    hKey As PHKEY, _
    sKey As String, _
    vName As String, _
    vData As String) As Long
   
    'Returns 11 - if key created and call successful
    'Returns 12 - if opened existing key and successful
   
    Dim h      As Long
    Dim dp   As Long

    If RegCreateKeyExA(hKey, sKey, _
        0&, 0&, 0&, &H3F, _
        0&, h, dp) = 0 Then
      If RegSetValueExA(h, vName, 0&, _
        1, vData, Len(vData)) = 0 Then
        WriteValue = 1 & dp
      End If
    End If
    RegCloseKey h
End Function




'--- Form1 -----

Dim Success As Long
   
    Success = WriteValue(HKEY_CURRENT_USER, _
        "Software\NEWKEY", _
        "name", _
        "data to write")
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16943343

ok some more explanation:


by small i do mean as less bits as possible
(so i don't care about 100 lines code, i do care about the compiled size)

i only have to write to it (now)

most of the times i have to create new keys,
sometimes delete them


i want as few refereces as possible

i do now have references to:
kernel32 (copymem)
comdlg32l (getopenfilename)
user32 (region stuff,and sendmessage)
gdi32 (ExtCreateRegion)




@ EDDYKT :
do i have to include some dll for wscript?


@kaliyugkaarjun :
i have to go beyond the vb/vba key

and the vbaccelerator examples are great, but they are not really "small"
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16943440
>>do i have to include some dll for wscript?

no, it's already come with the system
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16943483

maybey i am doing somthing wrong, but when i use the wscript example,
vb states that "WScript" is an unknow variable
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 16943879
Oooops

try this

Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.RegWrite "HKCU\MyRegKey\", "Top level key"
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16944046

thank you!

that is indeed really small, but i don't understand it very good.

please see this question for exaplaining it a bit:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21892807.html




i am thinking about a split between eddykt (350)
and egl1044 (150)


is that ok for both of you?
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16944152
there both the same size in bytes
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16944213

not really:

the wscript exe:
16.384 bytes

the api exe:
20.480 bytes

what i did to test it:

open a new default project
now doubleclick on the form to go to the Form_Load
for the wscript i pasted all the code in the form_load (3 lines)

for the api i pasted all code above the form load, exept for the last lines
(Dim Success As Long
    Success = WriteValue(HKEY_CURRENT_USER, _
        "Software\NEWKEY", _
        "name", _
        "data to write"))

compiled the first as wscript.exe
and the second as api.exe

and the filesize is different
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16944281
not if you put the code in a module
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16944892

20.480 bytes when i put that code in a module


what are your compiler options?

i have it set to compile for small exe
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16945392
i get 16K for both but this is without using a form
0
 
LVL 13

Author Comment

by:Mark_FreeSoftware
ID: 16952568

weird,


then it's a even split between you and eddykt
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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