Results 1 to 11 of 11

Thread: [RESOLVED] Elevate Code in Vista using VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    10

    Resolved [RESOLVED] Elevate Code in Vista using VB6

    I was unable to find any code on how to elevate to run as an administrator portions of my VB6 code under Vista. So, I tried it on my own, and I am getting really close, but not quite there.

    Note: I could simply run a separate EXE using ShellExecute with the "Runas" verb, but I would like to avoid the extra warning and therefore, I am trying to use the COM route.

    To summarize, this is what I have done so far:
    1. Created a new VB6 ActiveX EXE project.
    2. Added a couple of methods
    3. Compiled.
    4. Added the needed keys to the registry, namely:
      - Localized String
      - Elevation\Enabled = 1
    5. Created a new VB6 Project as a normal EXE
      - Declared the object, and
      - Used the CreateObject Function so that I can use the Moniker:

    Code:
    Dim MyObj As My_Vista.My_Vista_Cmds
    Set MyObj = CreateObject("Elevation:Administrator!new:My_Vista.My_Vista_Cmds")
    MyObj.Test_Elevate "This Elevation"
    Set MyObj = Nothing
    Running the program, this is what I get:

    Running as an Administrator
    • I get the request to elevate
    • Click "Allow"
    • Object is created
    • Methods are available and can be called.


    Running as a Standard Vista User
    • I get the request to elevate
    • Enter the password of the Administrator account
    • The COM object appears as a process in the Task Manager running under the Admin's account
    • The "Initialize" code of the COM runs correctly
    • In the calling program, "CreateObject" fails (70 - Permission denied)

    Since the changes I need to make are minor (I am changing the name of a printer), I can run what I need under the "Initialize" sub and then simply quit: it works in that the code is indeed running under the Administrator account, but I would rather use the proper methods/properties to return the results to the user (ie it passed or failed).

    Any ideas? Thx!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Elevate Code in Vista using VB6

    Welcome to the Forums.

    Well there is very little nw documentation or support for VB 6 on Vista. What I do know about the security process is that a new security token should be creaetd for minor admin tasks. If multiple tasks are to be done uder the admin account or if one elevation may involve another admin task you need to restart the app using an admin account. I read it on the MS site white paper fo r Vista Security somewhere.

    I am not good at security tokens but that is what I remember reading on how it should be done. Should be worth looking into.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    10

    Re: Elevate Code in Vista using VB6

    Thank you for the welcome.

    You make an interesting point: I could move all of the program's preferences to a separate application, and make sure that it is called with an elevated token, or use ShellExecute with the "runas" verb. However, only two out of 30 different settings actually require admin priviledges, so I would rather have it elevate only for those two settings if possible.

    Thanks!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Elevate Code in Vista using VB6

    Is there any way to separate those two?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    New Member
    Join Date
    Mar 2007
    Posts
    3

    Re: Elevate Code in Vista using VB6

    I have an application that only requires 2 admin functions. I decided to do these operations in a separate ActiveX exe and use the COM Moniker to elevate this exe.

    Set srv = CreateObject("Elevation:Administrator!new:testSrv.Admin")

    I was getting the same Permission Error when making a method call into this object as a restricted user.

    It seems the ActiveX exe object needs to CoInitializeSecurity.

    I was able to get it working using the registry AppID technique refered to at this link: http://na.tm.agilent.com/pna/DCOMSecurity.html

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    10

    Re: Elevate Code in Vista using VB6

    Hi Mojo,

    Thanks for the link. Unfortunately, I had no luck with CoInitializeSecurity. This function needs to be called by the main application, not the ActiveX (although I tried it both ways), and it made no difference. I still get the "Permission Denied (70)" error.

    The registry settings for AppID apply only to getting CoInitializeSecurity to work properly when you run the code from within the VB IDE, and it does not fix the problem.

    The only thing that I found today is this article [here] which may contain useful background information. I have not finished studying it. However, as far as I can tell, it will not give a solution to VB6.

    But I am keeping the faith...

  7. #7
    New Member
    Join Date
    Mar 2007
    Posts
    3

    Re: Elevate Code in Vista using VB6

    This is working just fine for me when running an exe (no IDE involved) and instantiating the ActiveX exe server.

    FYI - Here are the registry settings

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}]
    @="testSrv.Admin"
    "LocalizedString"="@C:\\Users\\Public\\Documents\\TestApp\\testSrv.exe,-101"

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\Elevation]
    "Enabled"=dword:00000001

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\Implemented Categories]

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\Implemented Categories\{40FC6ED5-2438-11CF-A3DB-080036F12502}]

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\LocalServer32]
    @="C:\\Users\\Public\\Documents\\TestApp\\testSrv.exe"

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\ProgID]
    @="testSrv.Admin"

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\Programmable]

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\TypeLib]
    @="{3F7CFB62-967C-4563-B54B-2A37FD851E10}"

    [HKEY_CLASSES_ROOT\CLSID\{29FD271B-F212-4A6A-9A23-67F15F33927C}\VERSION]
    @="1.0"



    [HKEY_CLASSES_ROOT\AppID\{29FD271B-F212-4A6A-9A23-67F15F33927C}]
    @="testSrv.exe"
    "AuthenticationLevel"=dword:00000001



    [HKEY_CLASSES_ROOT\AppID\testSrv.exe]
    @="testSrv.exe"
    "AppID"="{29FD271B-F212-4A6A-9A23-67F15F33927C}"

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    10

    Re: Elevate Code in Vista using VB6

    Hi Mojo,

    Thank you for your last reply: it worked! It also works if I remove the call to "CoInitializeSecurity", so that does not seem to be related to the problem I was experiencing.

    By the way, I added the missing "AppID" entries like you showed, but, until I saw your example, I was using [HKEY_LOCAL_MACHINE\Software\Classes] and your example has the entries in [HKEY_CLASSES_ROOT]. Is there a difference?

    Also, it seems like a need to refresh the registry (F5) before the new entries I added are recognized: Is a "refresh" also required if I write the entries to the registry via code?

    Anyway, I am very pleased that it is finally working properly: thank you for the help.

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    10

    Re: Elevate Code in Vista using VB6

    OK. I got my own answer on where to write to the registry from Microsoft's web site [here]. The short answer is to write to: [HKEY_LOCAL_MACHINE\Software\Classes].

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Elevate Code in Vista using VB6

    Not sure if its still the correct place because the KBA doesnt apply to Vista.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    New Member
    Join Date
    Apr 2008
    Posts
    1

    Re: [RESOLVED] Elevate Code in Vista using VB6

    Hi, can you please post an example project for us coders. I would love to see this. Thanks in advance.

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