Launch a file as another user

Posted on 2005-04-28
Last Modified: 2010-05-02

What I want to do is something similar to the command line runas command or just right clicking on a file and selecting run as.

I need to run this damn Fed Ex program as an administrator because it won't work otherwise

When I right click and run it as an administrator it works but it does not save the info for next time.

I want to see if I can compile this so the user can not see the admin password

Thanks in advance for your time and suggestions
Question by:jculkincys
    LVL 38

    Accepted Solution

    Here is the method to run a process under a different user account:

    Private Const LOGON_WITH_PROFILE = &H1&
    Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
    Private Const CREATE_NEW_CONSOLE = &H10&
    Private Const CREATE_NEW_PROCESS_GROUP = &H200&
    Private Const CREATE_SEPARATE_WOW_VDM = &H800&
    Private Const CREATE_SUSPENDED = &H4&
    Private Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000&
    Private Const BELOW_NORMAL_PRIORITY_CLASS = &H4000&
    Private Const HIGH_PRIORITY_CLASS = &H80&
    Private Const IDLE_PRIORITY_CLASS = &H40&
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const REALTIME_PRIORITY_CLASS = &H100&
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    Private Type STARTUPINFO
        cb As Long
        lpReserved As Long
        lpDesktop As Long
        lpTitle As Long
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    Private Declare Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" (ByVal lpUsername As Long, ByVal lpDomain As Long, ByVal lpPassword As Long, ByVal dwLogonFlags As Long, ByVal lpApplicationName As Long, ByVal lpCommandLine As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInfo As PROCESS_INFORMATION) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Sub Form_Load()
        Dim lpUsername As String, lpDomain As String, lpPassword As String, lpApplicationName As String
        Dim lpCommandLine As String, lpCurrentDirectory As String
        Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
        lpUsername = "OtherUser"
        lpDomain = ""
        lpPassword = "other_user_password"
        lpApplicationName = "C:\WINNT\NOTEPAD.EXE"
        lpCommandLine = vbNullString 'use the same as lpApplicationName
        lpCurrentDirectory = vbNullString 'use standard directory
        StartInfo.cb = LenB(StartInfo) 'initialize structure
        StartInfo.dwFlags = 0&
        CreateProcessWithLogon StrPtr(lpUsername), StrPtr(lpDomain), StrPtr(lpPassword), LOGON_WITH_PROFILE, StrPtr(lpApplicationName), StrPtr(lpCommandLine), CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, ByVal 0&, StrPtr(lpCurrentDirectory), StartInfo, ProcessInfo
        CloseHandle ProcessInfo.hThread 'close the handle to the main thread, since we don't use it
        CloseHandle ProcessInfo.hProcess 'close the handle to the process, since we don't use it
        'note that closing the handles of the main thread and the process do not terminate the process
        'unload this application
        Unload Me
    End Sub
    LVL 8

    Expert Comment

    It might be worth nosing around in the local machine registry to see if it stores these setting there or if it is trying to save settings to file which is store somewhere with restricted access.  You might be able to tweak the security settings of these stores that will allow the app work with out admin rights.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now