Wednesday, April 13, 2011

FreeBasic : Code : runas in FreeBasic

#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
'#############################################
'                   RunAsUser for Windows 2000 and Later
'#############################################

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)
 

No comments: