0

I am programming a App that adds Strings to the Registry to add custom URLs and Programs/ShortCuts to the context menu. I'm stuck at this point because it doesn't let me write a string to the registry and says some weird error.

Here is a photo (sorry for some things in German :D ):

Error

And here is the text what is missing / cut on the photo:

Error2

I tried it with this code:

My.Computer.Registry.ClassesRoot.CreateSubKey("DesktopBackground\Shell\Settings")
            My.Computer.Registry.ClassesRoot.CreateSubKey("DesktopBackground\Shell\Settings\command")
            My.Computer.Registry.ClassesRoot.SetValue("Computer\HKEY_CLASSES_ROOT\DesktopBackground\Shell\Settings", "(Default)", "@shell32.dll,-4161")
            My.Computer.Registry.ClassesRoot.SetValue("Computer\HKEY_CLASSES_ROOT\DesktopBackground\Shell\Settings", "icon", "Control.exe")
            My.Computer.Registry.ClassesRoot.SetValue("Computer\HKEY_CLASSES_ROOT\DesktopBackground\Shell\Settings", "Position", "Middle")
            My.Computer.Registry.ClassesRoot.SetValue("Computer\HKEY_CLASSES_ROOT\DesktopBackground\Shell\Settings\command", "(Default)", "Control.exe")

I think it comes because it tries to write it as a integer and i try to wrtie a string, but i dont know how to use a String as a type.

Here is the full code: Form1: http://txt.do/drqp1 Form2: txt.do/drqpt

[SOLVED] Availible on GitHub: https://github.com/amir00t/LvL-up

Max
  • 5
  • 4
  • I think this should be tagged with vb.net, not vba. – Rich Holton Apr 20 '17 at 16:10
  • You should include the exception and its stack trace in the post, not just a screenshot of it. – Mathieu Guindon Apr 20 '17 at 16:14
  • Sorry i dont know what you mean because im new with programming – Max Apr 20 '17 at 16:23
  • I added the entire Code for the two Forms – Max Apr 20 '17 at 16:31
  • You are getting an UnauthorizedAccessException. Writing to HKCR probably requires admin rights (i.e. you need to "Run as administrator" - or whatever that is in German). To debug, you may need to run Visual Studio as administrator. – Mark Apr 20 '17 at 17:16
  • Possible duplicate of [Unhandled Exception when trying to add registry key](http://stackoverflow.com/questions/43404463/unhandled-exception-when-trying-to-add-registry-key) – Visual Vincent Apr 20 '17 at 19:08
  • i tried to build it and run it outside as a Admin and it spit out another error that basically said that it couldn't convert "@shell32.dll" to an integer – Max Apr 21 '17 at 08:37
  • and no i have a different problem because it tries to write it as an integer and doesnt work on my pc aswell @Visual Vincent – Max Apr 21 '17 at 08:40
  • See my answer... – Visual Vincent Apr 21 '17 at 09:49

1 Answers1

1

For an UnauthorizedAccessException see this.

For your second problem: You're using the SetValue() overload incorrectly, you ought to check IntelliSense when writing the code.

  • The first parameter is the name of the value.

  • The second parameter is the value to store in the registry.

  • The third parameter is an enumeration value (of type Integer) indicating what kind of data you're entering in the registry.

The correct use is:

My.Computer.Registry.ClassesRoot.OpenSubKey("<relative\sub\keys>", True).SetValue("<value name>", "<value>")

'For instance:
My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\Settings", True).SetValue("(Default)", "@shell32.dll,-4161")
Community
  • 1
  • 1
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • @Max if it helped, consider accepting. Please see [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Bugs Apr 21 '17 at 10:06
  • im changing the code then im doing it :DD (I know its the checkmark ;D ) – Max Apr 21 '17 at 10:10
  • still spits errors out idk why (its a different error putting the new code and pics in the question) – Max Apr 21 '17 at 10:21
  • heres the new code and the new error message Form1 v2: http://txt.do/drqe6 Form2 v2:http://txt.do/drqej Error (the new one): http://txt.do/drqe4 the custom url doenst spit errors out but it doesnt do the task because the Variables dont get added – Max Apr 21 '17 at 10:27
  • @Max : That's a difference question, the answer can be found in: [**What is a NullReferenceException, and how do I fix it?**](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) Presumably the `BoM` variable is null, but without knowing the exact line I cannot tell for sure. – Visual Vincent Apr 21 '17 at 11:08
  • it is in Form1 in line 7 to 14 because it needs to check the Position the user wants i gave it string in the part too – Max Apr 21 '17 at 11:17
  • @Max : I can't see line numbers on my phone. Either way you have to make sure `BoM` isn't null. The answers in the post I linked to contain good information on how to find the cause of an NRE. -- You can inspect variables when the exception is thrown via the `Autos` window (can be opened via menu `Debug > Windows > Autos`). – Visual Vincent Apr 21 '17 at 11:32
  • oh sorry but i dont have this tool in vs – Max Apr 21 '17 at 12:02
  • You should? Are you using VS Express? Otherwise there's also the `Locals` window (`Debug > Windows > Locals`). It should exist in any VS version. More info here: https://msdn.microsoft.com/en-us/library/aa290840(v=vs.71).aspx – Visual Vincent Apr 21 '17 at 12:20
  • i have none of these both EDIT: oh i need to be in debug mode sorry – Max Apr 21 '17 at 12:37
  • @Max : That's why you do it _**"when the exception is thrown"**_. ;) – Visual Vincent Apr 21 '17 at 12:44
  • i got it i defined a start parameter for the Position Variable if it is detected you'll get a messagebox thank you im trying the custom one now – Max Apr 21 '17 at 12:45
  • the custom one doesnt work i tried to do it like this *My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("(Default)", "@shell32.dll")* It doesnt recognize the variable and creates everthing without it – Max Apr 21 '17 at 12:46
  • What is this `givenName`? Also, you can do `ElseIf CheckBox2.Checked Then` instead of making an entire new `If`-statement. – Visual Vincent Apr 21 '17 at 12:51
  • it should be able to add a custom chosen url in a batch script (which i have done) which the context menu refers to but the varialbe that lets you choose the name of the registry key and the batch script doesnt get added where i tried to add it (in the code lines which need the variable) I tried it like this : *My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("(Default)", "@shell32.dll")* – Max Apr 21 '17 at 12:53
  • @Max : You're just asking more and more here... Not that I don't want to help you, but it's very hard to do so without a proper perspective. If my answer solved your initial problem please accept it. If you need more help then you should rather ask a new question where you can provide code, screenshots, examples, etc. properly (do provide examples of what values your variables have got). – Visual Vincent Apr 21 '17 at 12:58
  • @Max : You can place a breakpoint in the code to stop execution at a specific line in order to inspect variables. Step-through debugging using breakpoints is a very efficient way of finding out the problem yourself. Here's some info: https://msdn.microsoft.com/en-us/library/5557y8b4.aspx – Visual Vincent Apr 21 '17 at 13:07
  • i posted the second question here: http://stackoverflow.com/questions/43543526/variables-cant-get-embedded – Max Apr 21 '17 at 13:07