473,804 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing DWORD values to the registry.

2 New Member
Hi all,

I have this code

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  3. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
  4. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  5. Private Const HKEY_LOCAL_MACHINE = &H80000002
  6. Private Const REG_DWORD = 4
  7. Dim nBufferKey As Long
  8. Dim nVal As Long
  9.  
  10. Private Sub Form_Load()
  11.     nVal = 2
  12.     RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey
  13.     RegSetValueEx nBufferKey, "SandBoxMode", 0, REG_DWORD, nVal, Len(nVal)
  14.     'MsgBox "Sandbox Mode Changed", vbInformation, "Sandbox"
  15.     RegCloseKey nBufferKey
  16.     Unload Me
  17. End Sub
All I am try to do is write a DWORD value of 2 to the registry. The above code does work without error but the key has a value of 32(50), if I change nVal to 0 the key reads 30(48). Can anyone assist in get the key to read 2.
Feb 7 '07 #1
3 10112
iburyak
1,017 Recognized Expert Top Contributor
Hi all,

I have this code

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  3. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
  4. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  5. Private Const HKEY_LOCAL_MACHINE = &H80000002
  6. Private Const REG_DWORD = 4
  7. Dim nBufferKey As Long
  8. Dim nVal As Long
  9.  
  10. Private Sub Form_Load()
  11.     nVal = 2
  12.     RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey
  13.     RegSetValueEx nBufferKey, "SandBoxMode", 0, REG_DWORD, nVal, Len(nVal)
  14.     'MsgBox "Sandbox Mode Changed", vbInformation, "Sandbox"
  15.     RegCloseKey nBufferKey
  16.     Unload Me
  17. End Sub
All I am try to do is write a DWORD value of 2 to the registry. The above code does work without error but the key has a value of 32(50), if I change nVal to 0 the key reads 30(48). Can anyone assist in get the key to read 2.
I didn't test your code but at a first glance

You are passing integer to a string requested value so convert to string before passing.

Dim nVal As Long
ByVal lpData As String

[PHP]Private Declare Function RegSetValueEx Lib "advapi32.d ll" Alias "RegSetValueExA " (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long[/PHP]

Also I would do this

[PHP]If RegOpenKey( HKEY_LOCAL_MACH INE, "SOFTWARE\Micro soft\Jet\4.0\En gines", nBufferKey) = ERROR_SUCCESS Then
RegSetValueEx nBufferKey, "SandBoxMod e", 0, REG_DWORD, nVal, Len(nVal)
End If[/PHP]
Feb 7 '07 #2
Bomber
2 New Member
I didn't test your code but at a first glance

You are passing integer to a string requested value so convert to string before passing.

Dim nVal As Long
ByVal lpData As String

[PHP]Private Declare Function RegSetValueEx Lib "advapi32.d ll" Alias "RegSetValueExA " (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long[/PHP]

Also I would do this

[PHP]If RegOpenKey( HKEY_LOCAL_MACH INE, "SOFTWARE\Micro soft\Jet\4.0\En gines", nBufferKey) = ERROR_SUCCESS Then
RegSetValueEx nBufferKey, "SandBoxMod e", 0, REG_DWORD, nVal, Len(nVal)
End If[/PHP]
If I dim nVal as a string, then the result is the DWORD value becomes invalid once the code is ran. So I'm not really sure I'm passing nVal as a string by declaring it as a string, is that enough?

I may also use the second code quote, thanks.
Feb 7 '07 #3
Airton Barbosa
1 New Member
That happends because DWORD way for "RegSetValu eEx" reads the "nVal" component of the pinvoke as an ASCII character. A string that receives "0" is "30" in ASCII hex, same for "1" -> "31" and "2" -> "32".

To set the DWORD registry "0x00000000 " just use the string representation of 0 ASCII member which is "\u0000".

Example:

string value = "\u0000"; //To set 0 for DWORD registry

RegSetValueEx nBufferKey, "SandBoxMod e", 0, REG_DWORD, value, Len(nVal)//Setting the string "value" for the pinvoke
May 4 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
22316
by: Lee K | last post by:
This is not strictly a C++ issue but if anyone will know the answer its you guys! I am trying to figure out a date format stored in the registry by a piece of software (I am trying to write something to enable me to script a configuration). The program stores a createdate for a set of registry keys held in a DWORD value. I assumed it would be the number of seconds since epoch (1/1/1970 00:00:00) - but its not. Here is a couple of ...
6
9925
by: Action | last post by:
I can add a systray icon in normal windows application... but when i do it in windows service, the systray icon just don't show up is that because i don't have any "component" to contain the systray icon? please give direction... thx
6
16819
by: CMG | last post by:
I am writing a little code to associate an extention with my program. And as far as i can see, i need to do the following: Public Function associatefile(ByVal FILE_EXTENTION_TO_ASSOCIATE As String, ByVal FILE_EXTENTION_DESCRIPTION As String, ByVal EXE_NAME As String, ByVal DEFAULT_OPEN_COMMAND As String, ByVal DEFAULT_ICON As String, ByVal ACTION_NAME As String) Dim RegKey As RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey
0
2259
by: bohuge | last post by:
Hey! At the time being I'm working on a backup solution for a Qtek9090 pocketpc, which should be able to find and backup outlook data to a server, local files, messages and contact from the sim card (and backup them to a server as well) all written in C#. So I'm making a wrapper for the sim manager api. In order to use it's functions in my device application, I've used platform invoke method, to acces the simmanager functions from...
9
3858
by: xieliwei | last post by:
Hello, I'm attempting to write a WinNT service that basically changes the time zone and forces a resync with a time server on initialisation. The service will then restore the original time zone upon stopping and force another resync. I have two versions that does the same thing, the older one works perfectly but I did not like its structure. Thus I followed another template, which formed the new version. However, the new version works,...
4
3637
by: RhavoX | last post by:
Hi. This may be a very stupid question but I'll leave you to judge it ;) I know there were lots of questions about this but none of the answers suits me. I'm wondering how to get the BINARY type data from the registry and how to display it as hex in a way I'm showing below. Let's say I use this key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion \Prefetcher The value of LastDiskLayoutTime is: 6A C7 49 ED 50 5F C7 01
1
1839
by: projectVBA | last post by:
Hi , I wrote some add in for PowerPoint and i'm trying to find an automatic way to load it every time PowerPoint starts. I found Microsoft Code example (see below ) BUT the problem is : I can't find all appear function declaration and VBA also errors regarding all the default variable values give in the code like ERROR_SUCCESS etc. May be you can help me to make this code run?? Function RegisterAddin( _ strBranch As String, _...
3
4001
by: teddysnips | last post by:
I have a notional Boolean value stored in the Registry. Actually it's a REG_DWORD capable of taking values of either 0 or 1. So, I need to populate a checkbox on a form dependent on this value. 1. I tried this: chkLogToFile.Checked = Boolean.Parse(LogFileKey.GetValue("LogToFile"));
2
2821
by: terryastone | last post by:
I have an application that reads some registry values stored in the Local Machine\Software\... key. In XP, I can see these values using RegEdit. In my application I can read the values at startup, and change the values during the application. In Vista, I CANNOT see the values using RegEdit but I can read the values using the application. I created some values using RegEdit in the place where my values would be. Now I can see them...
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.