-
Mar 15th, 2017, 02:23 PM
#1
Thread Starter
New Member
Run as Administrator Registry
Hello, today I'm working on about the save my program to registry for run as administrator. I know "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" we should write our programs directory in here but I couldn't do it correct. Look my code;
Code:
Set objWshShell = New WshShell
objWshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\C:\Users\Word\Desktop\WordToPdf\WordToPdf.exe", "RUNASADMIN"
It's not right REG_SZ it's opening subfolders in Layers folder like C: > Users > Word ... Please help me.
-
Mar 16th, 2017, 04:23 AM
#2
Re: Run as Administrator Registry
you can not do like this from wscript
for more explanation, workaround and why you shouldn't see http://stackoverflow.com/questions/3...s-registry-key
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 16th, 2017, 07:58 AM
#3
Thread Starter
New Member
Re: Run as Administrator Registry
Originally Posted by westconn1
When I try it, it give Compile error: Argument not optional.
-
Mar 16th, 2017, 03:22 PM
#4
Re: Run as Administrator Registry
try what?
for what line?
show the code you are trying
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 16th, 2017, 04:11 PM
#5
Thread Starter
New Member
Re: Run as Administrator Registry
Originally Posted by westconn1
try what?
for what line?
show the code you are trying
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
'create .reg file:
Set reg_file = fs.CreateTextFile("slash.reg")
reg_file.WriteLine "Windows Registry Editor Version 5.00"
reg_file.WriteLine "[HKEY_CLASSES_ROOT\.txt]" 'put your path here
key_name = "D:\\Program Files\\test.exe" 'must be escaped inside the .reg file, so they enter as single slash in the registry
key_value = "RUNASADMIN"
reg_file.WriteLine """" & key_name & """=""" & key_value & """" 'escaping quotes inside vbscript string literal
reg_file.Close
'run it automatically to insert data (may ask for elevated privileges):
path = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
shell.run "regedit.exe /s """ & path & "slash.reg"""
I try your link. It gives error at second line "shell" word.
-
Mar 17th, 2017, 02:35 AM
#6
Re: Run as Administrator Registry
It gives error at second line "shell" word.
shell is a reserved word in VB, use some other name for your variable (eg wsh)
path = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
this line of wscript will also error, convert it to VB6
actually if you supply a fully qualified path to fs.createtextfile, in a variable, you can use the same path in wsh.run, then the line above is not needed at all
path should also be avoided as a variable name
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 17th, 2017, 06:39 AM
#7
Re: Run as Administrator Registry
A way to tell Windows that a program must run with admin privilleges is to make a manifest file.
-
Mar 18th, 2017, 02:57 PM
#8
Thread Starter
New Member
Re: Run as Administrator Registry
Originally Posted by Eduardo-
A way to tell Windows that a program must run with admin privilleges is to make a manifest file.
Manifest file? Can you explain it?
-
Mar 18th, 2017, 04:32 PM
#9
Re: Run as Administrator Registry
Originally Posted by wordtopdf0
Manifest file? Can you explain it?
It is a file with XML code inside that has the same name as the exe plus ".manifest".
For example, if your program is program1.exe, then the manifest file must be named program1.exe.manifest
But there is another option than having a separate file, it is to embed this manifest file into the exe (so you don't need to distribute another file).
This manifest file has information about the exe, and one thing is whether it must run as administrator or not.
An example manifest to tell that the program require admin rights would be:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
<assemblyIdentity name="MyCompany.MyProgram" processorArchitecture="X86" type="win32" version="1.0.0.0" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
More info:
https://en.wikipedia.org/wiki/Manifest_file
https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
And more important: http://www.vbforums.com/showthread.p...nifest-Creator
https://www.google.com/search?q=site...s.com+manifest
-
Mar 19th, 2017, 12:12 PM
#10
Re: Run as Administrator Registry
Originally Posted by wordtopdf0
Manifest file? Can you explain it?
With the sample manifest provided above by Eduardo, know that it is not valid while in the IDE (as-is). The manifest applies to the compiled application. Just FYI since you were unfamiliar with these files.
-
Mar 18th, 2017, 04:41 PM
#11
Re: Run as Administrator Registry
Or... a better thing to do is to remove the API calls or whatever that require that your program must run as administrator, if you can (and perhaps replace with something else).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|