473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Writing DWORD values to the registry.

2
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 10060
iburyak
1,017 Expert 512MB
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.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[/PHP]

Also I would do this

[PHP]If RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey) = ERROR_SUCCESS Then
RegSetValueEx nBufferKey, "SandBoxMode", 0, REG_DWORD, nVal, Len(nVal)
End If[/PHP]
Feb 7 '07 #2
Bomber
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.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[/PHP]

Also I would do this

[PHP]If RegOpenKey( HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey) = ERROR_SUCCESS Then
RegSetValueEx nBufferKey, "SandBoxMode", 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
That happends because DWORD way for "RegSetValueEx" 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, "SandBoxMode", 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
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...
6
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...
6
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...
0
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...
9
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...
4
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...
1
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...
3
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....
2
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.