Solved

Read and write to the Registry with VB

Posted on 2014-12-11
8
124 Views
Last Modified: 2014-12-11
I am trying to write a small application using Visual Basic 2013 that will read the port number for Remote Desktop from the registry then pass that information to a text box. Then if I need to change the port number I can enter that port number in the text box and pass it back to the registry.

This bit of code works to get the number and is pretty straight forward:

 Dim readValue = My.Computer.Registry.GetValue(
        "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", "PortNumber", Nothing)
        PortTxtBx.Text = readValue

My problem is trying to write the value back to the registry.

I know that I want build a test this application in the normal way because it has to be run elevated to administrator so testing is a little unwieldy in so far as I have to build the app then run it.

I need to keep this as simple as possible because only have to use it about 40 times.

If someone has another idea such as script I am all ears.

Thanks
0
Comment
Question by:Pat Clancy
8 Comments
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
ID: 40493692
Your tests can be done manipulating the hkey_current_user chain.

Are you adding or modifying existing keys?
http://msdn.microsoft.com/en-us/library/aa289494(v=vs.71).aspx#vbtchaccessingregistrywithvisualbasicnetanchor5

The link includes an example with tests to determine whether you are adding a new pair, or modifying an existing one.

Always test as well as backup/record what was there before you make changes.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40493698
HI,

pls try

  Public Sub Main
      Dim regData As Byte() = {&HF0, &HFF, &H20, &H00}
      Dim regTopKey As String = "HKEY_LOCAL_MACHINE"
      Dim regPath As String = "\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"

'      My.Computer.Registry.SetValue(regTopKey & regPath, "BinData", PortNumber, RegistryValueKind.Binary)
   End Sub

Open in new window

Regards
0
 

Author Comment

by:Pat Clancy
ID: 40493700
Yes I understand that and I can manually go in and change the numbers that need to be changed however in this day and age there should be a script that I can use to manipulate the part number.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40493781
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Pat Clancy
ID: 40493853
Thank you Rgonzo1971 I appreciate your effort however, I may be missing something. I tried putting the code into a button event and I'm just getting error messages indicating that I do not have access to the registry.

I do note that "BinData" is used and the port number is actually a DWORD 32-bit and RegistryValueKind.Binary is not declared.

Thanks for your help.
0
 
LVL 76

Expert Comment

by:arnold
ID: 40493936
Are you testing with the HKEY_CURRENT_USER which allows the user to make changes without the need to elevate one's rights?

You have to take account within your app whether you have elevated rights and if not to get them.

To test your script you have to use resources to which you have access rights.  Once the functionality is established, you can change to the HKEY_LOCAL_MACHINE at which point Administrative rights will be required.
0
 

Author Comment

by:Pat Clancy
ID: 40494392
Thank you Arnold I will try changing the key to the current user to test the application.
0
 

Author Closing Comment

by:Pat Clancy
ID: 40494691
The answer helped me find a way to troubleshoot the issue. I was able gomplete my tast.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now