Code: Freebasic: runas


#Include "windows.bi"
#inclib "advapi32"

Declare FUNCTION CreateProcessWithLogonW LIB "ADVAPI32" ALIAS "CreateProcessWithLogonW" ( _
        byval lpUsername As LPCWSTR  , _
        BYVAL lpDomain AS LPCWSTR , _
        BYVAL lpPassword AS LPCWSTR, _
        BYVAL dwLogonFlags AS DWORD, _
        BYVAL lpApplicationName AS LPCWSTR , _
        BYVAL lpCommandLine AS LPCWSTR , _
        BYVAL dwCreationFlags AS DWORD, _
        BYVAL lpEnvironment AS DWORD, _
        BYVAL lpCurrentDirectory AS LPCWSTR , _
        lpStartupInfo AS STARTUPINFO, _
        lpProcessInfo AS PROCESS_INFORMATION) AS LONG

Const X_CJ_LOGON_WITH_PROFILE=1
Const X_CJ_CREATE_DEFAULT_ERROR_MODE = &H04000000

FUNCTION RunAsUser(BYVAL UserName AS STRING, _                   
        BYVAL Password AS STRING, _
        BYVAL DomainName AS STRING, _
        BYVAL CommandLine AS STRING, _
        BYVAL CurrentDirectory AS STRING) AS boolean

        Dim si AS STARTUPINFO
        Dim pi AS PROCESS_INFORMATION
        Dim AS String wCurrentDir,wCommandLine,wPassword,wUser,wDomain
        Dim R01 AS LONG

        si.cb = LEN(si)
        wUser = UserName
        wDomain = DomainName
        wPassword = Password
        wCommandLine = CommandLine
        wCurrentDir = CurrentDirectory
        Print wCommandLine & "TEST"
        R01 = CreateProcessWithLogonW(wUser,wDomain,wPassword, _
                        X_CJ_LOGON_WITH_PROFILE, 0&, wCommandLine, _
                        X_CJ_CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
        IF R01  0 THEN
                CloseHandle pi.hThread
                CloseHandle pi.hProcess
                Return TRUE
        ELSE
                Return FALSE
        END If
End FUNCTION

Dim As Long R01
Dim As String  user_name,password,domain,program,workingDir

user_name="username"
password="pa$$word"
domain="domain"
program="C:\\windows\\notepad.exe"
workingDir="C:\\windows"

R01=RunAsUser(user_name,password,domain,program,workingDir)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s