Author Topic: Example how to run something with "CreateProcessWithLogonW()"  (Read 5646 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 966
    • it-berater
Example how to run something with "CreateProcessWithLogonW()"
« on: October 31, 2009, 07:21:19 PM »
Code: [Select]
DECLARE FUNCTION CreateProcessWithLogonW LIB "ADVAPI32.DLL" ALIAS "CreateProcessWithLogonW" ( _
BYVAL lpUsername AS DWORD, _
BYVAL lpDomain AS DWORD, _
BYVAL lpPassword AS DWORD, _
BYVAL dwLogonFlags AS DWORD, _
BYVAL lpApplicationName AS DWORD, _
BYVAL lpCommandLine AS DWORD, _
BYVAL dwCreationFlags AS DWORD, _
BYVAL lpEnvironment AS DWORD, _
BYVAL lpCurrentDirectory AS DWORD, _
lpStartupInfo AS STARTUPINFO, _
lpProcessInfo AS PROCESS_INFORMATION) AS LONG


%X_CJ_LOGON_WITH_PROFILE=1
%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 LONG

    LOCAL si AS STARTUPINFO,pi AS PROCESS_INFORMATION

LOCAL wCurrentDir,wCommandLine,wPassword,wUser,wDomain AS STRING
REGISTER R01 AS LONG,R02 AS LONG

    si.cb = LEN(si)

    wUser = UCODE$(UserName + CHR$(0))
    wDomain = UCODE$(DomainName + CHR$(0))
    wPassword = UCODE$(Password + CHR$(0))
    wCommandLine = UCODE$(CommandLine + CHR$(0))
    wCurrentDir = UCODE$(CurrentDirectory + CHR$(0))

    R01 = CreateProcessWithLogonW(STRPTR(wUser),STRPTR(wDomain),STRPTR(wPassword), _
          %X_CJ_LOGON_WITH_PROFILE, 0&, STRPTR(wCommandLine), _
          %X_CJ_CREATE_DEFAULT_ERROR_MODE, 0&, STRPTR(wCurrentDir), si, pi)
    ' CreateProcessWithLogonW() does not
    IF R01 <> 0 THEN
        CloseHandle pi.hThread
        CloseHandle pi.hProcess
        R02 = 0
    ELSE
        R02 = ERR
        MSGBOX "CreateProcessWithLogonW() failed with error " + ERROR$
    END IF
    FUNCTION=R02
END FUNCTION

FUNCTION PBMAIN () AS LONG
    REGISTER R01 AS LONG
    LOCAL S01,S02,S03,S04,S05 AS STRING
    S01="Login"
    S02="Password"
    S03="Domain"
    S04="IsAdminTest.exe"
    S05=EXE.PATH$
    
    R01=RunAsUser(S01,S02,S03,S04,S05)
    ? STR$(R01)

END FUNCTION    
                   
« Last Edit: October 31, 2009, 07:26:30 PM by Theo Gottwald »