PDA

View Full Version : Writing registry keys with API Calls



Bhatti1981
05-05-2017, 04:28 AM
Hello

the link bellow has a lot but I could not figure out how can I write a value to a registry with API calls.
https://support.microsoft.com/en-us/help/145679/how-to-use-the-registry-api-to-save-and-retrieve-setting#top

What way can we RegWrite to a registry REG_DWORD value from 1 to 0 or from 0 to 1? using API functions and not using the shell command.

Thoughts

Best Regards
Imran Bhatti

Bob Phillips
05-05-2017, 10:54 AM
Why not just use SaveSetting https://msdn.microsoft.com/en-us/library/aa155437(office.10).aspx

Bhatti1981
05-06-2017, 01:48 AM
Thank you Lord for reviewing my thread.
There are some certain reasons that I want to use Windows API calls.

snb
05-06-2017, 03:01 AM
Is this a crosspost ?

https://www.excelforum.com/excel-programming-vba-macros/1183596-about-registry-keys.html

Paul_Hossler
05-08-2017, 06:56 AM
There are many sites that have examples and/or instructions for using APIs to read/write to the registry

Chip's site is always a good source of information

http://www.cpearson.com/excel/registry.htm



This is very complete, but for the most part you'll just need a few functions (probably)

Bhatti1981
05-08-2017, 09:32 AM
Thanks sir Paul for reviewing my thread.
I have used VBA to some extend but no experience with API calls. I just know that they are stronger and more reliable than sendkeys. Your prescribed web site is really full of knowledge but I am really unable to as to how these can be used.for example bellow is the function ( I believe it is the function which I need)


Public Function RegistryCreateKey(BaseKey As Long, KeyName As String) As Boolean


Public Function RegistryCreateValue(BaseKey As Long, KeyName As String, _
ValueName As String, ValueValue As Variant, _
Optional CreateKeyIfNotExists As Boolean = False) As Boolean


How can I now set this

HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options\FormulaBarEx panded
REG_DWORD value to 0 or 1?

This is where we are ordered to ask the people with knowledge.

Paul_Hossler
05-08-2017, 10:41 AM
Here's two subs, one to set it to 1, and the other to set it to 0



Option Explicit

'HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options\FormulaBarE xpanded

Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const REG_DWORD As Long = 4

Private Declare PtrSafe Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, phkResult As LongPtr) As Long
Private Declare PtrSafe Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long

Sub FormulaBarExpanded1()
Dim iReturnValue As Long
Dim iKeyHandle As Long
Dim iDataType As Long
Dim iKeyValue As Long
Dim sKeyValue As String

'Query the key path
iReturnValue = RegCreateKey(HKEY_CURRENT_USER, "Software\Microsoft\Office\16.0\Excel\Options\", iKeyHandle)

'Update the sub key based on the data type
iKeyValue = CLng(1)

iReturnValue = RegSetValueEx(iKeyHandle, "FormulaBarExpanded", 0&, REG_DWORD, iKeyValue, 4&)

iReturnValue = RegCloseKey(iKeyHandle)

End Sub


Sub FormulaBarExpanded0()
Dim iReturnValue As Long
Dim iKeyHandle As Long
Dim iDataType As Long
Dim iKeyValue As Long
Dim sKeyValue As String

'Query the key path
iReturnValue = RegCreateKey(HKEY_CURRENT_USER, "Software\Microsoft\Office\16.0\Excel\Options\", iKeyHandle)

'Update the sub key based on the data type
iKeyValue = CLng(0)

iReturnValue = RegSetValueEx(iKeyHandle, "FormulaBarExpanded", 0&, REG_DWORD, iKeyValue, 4&)

iReturnValue = RegCloseKey(iKeyHandle)

End Sub

Zack Barresse
05-08-2017, 02:53 PM
Imran,

It is against site policy to post anything which is construed as hacking, which your thread implies, and the linked thread on another forum outright states. If any code which states how to hack access to the VBProject is found, the thread will be closed and you will be banned from the site. Right now this is a friendly conversation about showing how to utilize the registry using Windows API calls. Let's keep it there. Please don't post malicious code. Thanks. :)