Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create the following Key at SchUseStrongCrypto
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319

Can any one please help me how to proceed with it

What I have tried:

Option Explicit

Private Const REG_SZ As Long = 1 'REG_SZ represents a fixed-length text string.
Private Const REG_DWORD As Long = 4 'REG_DWORD represents data by a number that is 4 bytes long.

Private Const HKEY_CLASSES_ROOT = &H80000000 'The information stored here ensures that the correct program opens when you open a file by using Windows Explorer.
Private Const HKEY_CURRENT_USER = &H80000001 'Contains the root of the configuration information for the user who is currently logged on.
Private Const HKEY_LOCAL_MACHINE = &H80000002 'Contains configuration information particular to the computer (for any user).
Private Const HKEY_USERS = &H80000003 'Contains the root of all user profiles on the computer.

'Return values for all registry functions
Private Const ERROR_SUCCESS = 0
Private Const ERROR_NONE = 0

Private Const KEY_QUERY_VALUE = &H1 'Required to query the values of a registry key.
Private Const KEY_ALL_ACCESS = &H3F 'Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.


'API Calls for writing to Registry
'Close Registry Key
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
'Create Registry Key
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'Open Registry Key
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
'Query a String Value
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
'Query a Long Value
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
'Query a NULL Value
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
'Enumerate Sub Keys
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
'Store a Value
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, lpData As Any, ByVal cbData As Long) As Long
'Delete Key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Private Sub SaveValue(hKey As Long, strPath As String, strvalue As String, strData As String)

Dim ret
'Create a new key
RegCreateKey hKey, strPath, ret
'Save a string to the key
RegSetValueEx ret, strvalue, 0, REG_DWORD, ByVal strData, Len(strData)
'close the key
RegCloseKey ret

End Sub

Private Sub QueryValue(sKeyName As String, sValueName As String)

Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value
Dim Data As Long
Dim retval As Long 'Return value of RegQuery functions
Dim lType As Long 'Determine data type of present data
Dim lValue As Long 'Long value
Dim sValue As String 'String value

lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, KEY_QUERY_VALUE, hKey) 'Open Key to Query a value
'Manowara Changed
' lRetVal = QueryValueEx(hKey, sValueName, vValue) 'Query (determine) the value stored
retval = RegQueryValueExNULL(hKey, sValueName, 0&, lType, 0&, Data)
If retval <> ERROR_NONE Then MsgBox "The Regisrt Key is not existing exist"
If retval = ERROR_NONE Then
Select Case lType
' Determine strings
Case REG_SZ:
sValue = String(Data, 0)

retval = RegQueryValueExString(hKey, sValueName, 0&, lType, sValue, Data)

If retval = ERROR_NONE Then
vValue = Left$(sValue, Data - 1)
Else
vValue = Empty
End If

' Determine DWORDS
Case REG_DWORD:
retval = RegQueryValueExLong(hKey, sValueName, 0&, lType, lValue, Data)

If retval = ERROR_NONE Then vValue = lValue

Case Else
'all other data types not supported
retval = -1
End Select

'frmRegistryChng.Caption = vValue 'Set the Form's Caption to whatever text was stored
MsgBox "The Regisrt Settings exist"

RegCloseKey (hKey) 'Close the Key
End If
Call SaveValue(HKEY_LOCAL_MACHINE, "Software\Microsoft\.NETFramework\v4.0.30319", "SchUseStrongCrypto", "9999") 'Call SaveValue Sub to save a value in the Registry

End Sub



Private Sub Command1_Click()

QueryValue "Software\Microsoft\.NETFramework\v4.0.30319", "SchUseStrongCrypto"

End Sub
Posted
Updated 12-Jul-18 0:04am
Comments
Kornfeld Eliyahu Peter 12-Jul-18 5:30am    
Please format your code and remove irrelevant parts... It is next to impossible to read it in this form...

1 solution

Basically: don't. Access to the Registry is restricted in modern OSes and will get more restricted, not less. You should not be trying to use the Registry in new projects, you should store your data in configuration files instead.
 
Share this answer
 
Comments
Member 13879661 12-Jul-18 6:11am    
I need to enable the registry to make the application communicate via TLS1.2. I am not sure if i can do it via configuration files.
Can I ?
Dave Kreskowiak 12-Jul-18 9:00am    
No, you can't, but you also cannot do this in the registry from your code. Modifying anything under HKEY_LOCAL_MACHINE is restricted to administrator accounts only.

This is something you would set in the installer for your app, not in your app code.
Member 13879661 13-Jul-18 3:23am    
True, I thought of editing it in the installer, but what if the registry gets altered. My application needs this functionality at some specific time.
The application checked for .net framework 4.5 and installs that framework during installation. After studying through some articles i found out that for framework 4.7 the keys (i require to create) are there by default. However I analysed another use case where if some kind of installation/installation is done for higher/lower frameworks they key may/may not be there.
Simple works having it at installation is not best the solution(I may be in correct and suggestions from you would help me get a better resolution :) )
So i thought of getting it done via application.

All users using the app generally are administrators( i agree on the administrator rights parts)

Thank you for all the help so far !
Dave Kreskowiak 13-Jul-18 8:04am    
"Making sure it's there" isn't up to you or your code. It's up to the customer.

That stuff can also be set and reset by Group Policy, even WHILE YOUR CODE IS RUNNING.

You won't have complete control over the settings. All you can do is put that information in the documentation on your app and let the customer manage it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900