Unable to change registry on Windows 10 - windows

I'm trying to change the value of the Shell registry key on Windows 10 using the following code:
Public Function overwriteStartup()
Try
Dim winlogon As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
winlogon.SetValue("AutoRestartShell", 0, RegistryValueKind.DWord)
winlogon.SetValue("Shell", Application.ExecutablePath, RegistryValueKind.String)
winlogon.Flush()
winlogon.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
The issue is that Shell and AutoRestartShell are not changing.
If I add MessageBox.Show(My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon").GetValue("Shell")) between winlogon.Close() and Return True, I get a message box that shows the correct value (which means that the value was changed), but when I check regedit, it shows the original value, so it did not actually change the value.

If your app can read the registry value - You must need to provide administrative privileges before letting your app write registry values. Go to the app manifest and set the privilege to requireAdministrator then try again, it should work.
If you remove the exception handling Try Catch, then you'll get to know the exact reason.

I changed the target framework to .NET 4 and set target CPU to AnyCPU, it works now.

Related

Create System Restore Point in Windows 10

I want to use Window API to create System Restore Point (SRP). However, creating SRP is limited in 24 hours.
Here're my steps
Add SystemRestorePointCreationFrequency under the registry key HKLM\Software\Microsoft\Windows NT\CurrentVersion\SystemRestore and set to Zero.
In source code
Call LoadLibraryW(L"srclient.dll")
Get GetProcAddress of SRSetRestorePointW
Call SRSetRestorePointW method.
this method only create SRP in the first calling. In the second calling, it always return previous sequenceNumber(STATEMGRSTATUS.llSequenceNumber).
it seem that SRSetRestorePointW does not refer SystemRestorePointCreationFrequency.
I tried using powershell to create 2 SRPs, it works well (without reboot system )
Checkpoint-Computer -Description 'Install_TEST' -RestorePointType 'APPLICATION_INSTALL'
This source code is referred from MSD. But it has this issue. (learn.microsoft.com/en-us/windows/win32/sr/using-system-restore)
You must be using Windows 8 or later for getting SystemRestorePointCreationFrequency to work.
On first call set eventtype to BEGIN_SYSTEM_CHANGE.
On second call set eventtype to END_SYSTEM_CHANGE and set
SequenceNumber returned from the first call.

GetValueNames() of protected registry in vb.net

I have the following folder in registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths
But how can I show their value name to listbox?
Here is my code:
Dim FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths")
For Each ValueName As String In FontKey.GetValueNames()
Dim Value As Object = FontKey.GetValue(ValueName) 'Get the value (data) of the specified value name.
If Value IsNot Nothing Then 'Make sure it exists.
ListBox1.Items.Add(Value.ToString())
End If
Next
FontKey.Close()
P/s: I get this error: (Because I do not have the requisite permissions to create a new key)
System.NullReferenceException: 'Object reference not set to an instance of an object.'
FontKey was Nothing.
If you want to show the value name then just ignore retrieving the value and add the ValueName variable to the list box instead:
For Each ValueName As String In FontKey.GetValueNames()
ListBox1.Items.Add(ValueName)
Next
As for the error:
There's a difference between a NullReferenceException and the SecurityException that is actually thrown when you don't have access to a registry key. In this case the former occurs because the key you opened doesn't exist, which is likely caused by your application viewing the 32-bit version of the registry key (HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\...) rather than the 64-bit version (HKEY_LOCAL_MACHINE\SOFTWARE\...).
To fix this, either compile your application as x64 or AnyCPU, or force it to view the 64-bit registry. See my answer here for more information: NullReferenceException when opening a subkey I know exists

Suppressing Visio.Application alerts

I have a VBScript that opens a Visio file in the background by using Visio.Application in invisible mode.
Set Visioapp = CreateObject("Visio.Application")
Visioapp.Visible = False
Set Visio = Visioapp.Documents.Open(VisioFile)
This works fine except when I try to open a file that generates a pop-up while I'm processing it. If that happens, the application would show an alert to notify the user but since the application is invisible to the user (or running without a user present), the script just hangs indefinitely, waiting for input that won't be coming.
If I were writing VBA code for Excel or Word I could use Application.DisplayAlerts = False (and/or possibly DisplayEvents). But in my VBScript the Visio application doesn't have that property. Visioapp.DisplayAlerts = False will give me the error "Object doesn't support this property or method".
How do I suppress the popups generated by a Visio application opened from VBScript?
Summarizing the comments from #Dave and #Noodles, the Visio Application object does not have a property DisplayAlerts like other Office applications have. Instead it provides a property AlertResponse that allows you to define whether the application should respond to alerts with OK, Cancel, Abort, Retry, …
To have the application respond with OK to all alerts change your code to something like this:
Set Visioapp = CreateObject("Visio.Application")
visioapp.AlertResponse = vbOk
Set Visio = Visioapp.Documents.Open(VisioFile)
Note that in this case you can use the symbolic constants that VBScript already provides (vbOk, vbCancel, vbAbort, vbRetry, …). For application-specific constants (e.g. the SaveFlags for the SaveAsEx method) this won't work, though. In those cases you'll have to either use the numeric value:
Visio.SaveAsEx "C:\path\to\output.vsd", 1
or define the constant in your script:
Const visSaveAsRO = 1
...
Visio.SaveAsEx "C:\path\to\output.vsd", visSaveAsRO

QTP recognising a JavaEdit object but not able to set a value when running the script

I have written a simple script to log into a Java app where it fills in username and password, and then clicks on the "Connect" button".
Set UVC = JavaDialog("UVC")
wait(20)
If UVC.Exist Then
UVC.JavaEdit("JTextField").Set "admin"
wait(2)
UVC.JavaEdit("PSW").SetSecure "5256833195fsdqsdsqd447e4beefsdsdqd"
wait(5)
UVC.JavaButton("Connect").Click
Else
print "Console is not present"
End If
It's strange as QTP is identifying my password field properly. When running the following code I get a value back as expected:
MsgBox Main.JavaEdit("password").GetROProperty("attached_text")
I have also tried to set the password without encrypting it but it's also not working.
PS: the same script was working before and has since stopped working for an unknown reason!!!
Thanks in advance.
Replace
UVC.JavaEdit("PSW").SetSecure "5256833195fsdqsdsqd447e4beefsdsdqd"
with
UVC.JavaEdit("PSW").Click 1,1
UVC.JavaEdit("PSW").SetSecure "5256833195fsdqsdsqd447e4beefsdsdqd"
and it will work even with replay mode = "event". If you want to beautify this, you can use a click in the middle of the field, like in:
With UVC.JavaEdit("PSW")
.Click .GetROProperty ("width")\2, .GetROProperty ("height")\2
.SetSecure "5256833195fsdqsdsqd447e4beefsdsdqd"
End With
It seems that most Java password fields must first be focussed to be SetSecure-able.
Just to be sure.. check whether the field is enabled by testing .getroproperty("editable").
Use any of these methods to set text in Java Edit field.
You could use JavaEdit("PSW").Object.Settext method - this uses the JTextField in JavaSwing object properties
You could use setfocus method before entering the string in the field
Get the position of the test field
x = JavaEdit("PSW").Getroproperty("abs_x")
y = JavaEdit("PSW").Getroproperty("abs_y")
Set DRP = CreateObject("Mercury.DeviceReplay")
DRP.MouseClick x,y,"0"
DRP.SendString "the string"
You could also use JavaEdit's type object
Any of these methods should work for you. If not tough luck.. :)
Thanks for your answers but none of your suggestions worked, I have ended up using a basic turnaround :
UVC.JavaEdit("JTextField").Set"admin"
UVC.JavaEdit("PSW").Click 1,1
UVC.JavaEdit("PSW").SetSecure"52581237d889935df36ae78587773a641f40"
UVC.JavaButton("Connect").Click
wait (5)
While JavaDialog("Login Error").Exist
JavaDialog("Login Error").JavaButton("Ok").click
UVC.JavaEdit("PSW").RefreshObject
UVC.JavaEdit("PSW").SetSecure"52581237d889935df36ae78587773a641f40"
UVC.JavaButton("Connect").Click
Wend
I really don't get it how could the same function work sometimes and sometimes not!!

CustomActionData does not seem to be populated or accessable by my vbscript

Ok, this is driving me crazy.
I have a CA that needs to know the path of the INSTALLDIR to edit an XML file.
So, I set up a set property custom action that sets a property named RemoveAuthTypesNode to [INSTALLDIR]. Then I have a RemoveAuthTypesNode CA that is sequenced after SetConfigFolder (a set property that sets installdir to a system searched path) in the Install Execute Sequence, Deferred in System Context (doesn't work when just Deferred Exec either).
In the log I see that RemoveAuthTypesNode is set:
MSI (c) (D4:EC) [16:12:05:314]:
PROPERTY CHANGE: Adding
RemoveAuthTypesNode property. Its
value is 'C:\Program Files\Microsoft
SQL
Server\MSRS10.MSSQLSERVER\Reporting
Services\ReportServer\'.
The custom action errors:
Error 1720.There is a problem with
this Windows Installer package. A
script required for this install to
complete could not be run. Contact
your support personnel or package
vendor. Custom action
RemoveAuthTypesNode script error
-2146827864, Microsoft VBScript runtime error: Object required:
'objXMLDOMNode' Line 9, Column 1, MSI
(s) (78:EC) [16:12:23:916]: Product:
ASMI User Defined Reports -- Error
1720.There is a problem with this Windows Installer package. A script
required for this install to complete
could not be run. Contact your support
personnel or package vendor. Custom
action RemoveAuthTypesNode script
error -2146827864, Microsoft VBScript
runtime error: Object required:
'objXMLDOMNode' Line 9, Column 1,
This is failing because the path isn't correct so the XMLDom object never loads. I know this because if I hardcode the path everything works fine.
Also, when I search the log for CustomActionData I expected that it would be in there as being set.
Here is the code from the custom action. The msgbox is just for debugging. It is always displaying nothing.
strConfigFile = session.Property("CustomActionData") & "rsreportserver.config"
MsgBox session.Property("CustomActionData")
Set xDoc = CreateObject("Microsoft.XMLDOM")
xDoc.async = False
xDoc.Load(strConfigFile)
set objXMLDOMNode = xDoc.selectSingleNode("//Configuration/Authentication/AuthenticationTypes")
set objParentNode = objXMLDOMNode.parentNode
objParentNode.removeChild(objXMLDOMNode)
xDoc.save(strConfigFile)
Set xDoc = Nothing
What am I doing wrong? I'm sure it's something simple stupid. Help greatly appreciated.
The custom action that sets the property named for the vbscript custom action was setting a private property (not all upper case). So, the set property custom action had to be sequenced in the Execute sequence rather than the UI sequence. Once I made this change the correct data was being retrieved in the script.
It is expected if I have made a public property (all UPPER case) it would have work being in the UI sequence, however, I didn't test that theory.

Resources