Results 1 to 11 of 11

Thread: Run as Administrator Registry

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    13

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    13

    Re: Run as Administrator Registry

    Quote Originally Posted by westconn1 View Post
    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
    When I try it, it give Compile error: Argument not optional.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Run as Administrator Registry

    When I try it
    try what?

    it give Compile error:
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    13

    Re: Run as Administrator Registry

    Quote Originally Posted by westconn1 View Post
    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.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Run as Administrator Registry

    A way to tell Windows that a program must run with admin privilleges is to make a manifest file.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2017
    Posts
    13

    Re: Run as Administrator Registry

    Quote Originally Posted by Eduardo- View Post
    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?

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Run as Administrator Registry

    Quote Originally Posted by wordtopdf0 View Post
    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

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Run as Administrator Registry

    Quote Originally Posted by wordtopdf0 View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    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
  •  



Click Here to Expand Forum to Full Width