Page 1 of 1

RunAs program with parameters!

Posted: Tue Feb 20, 2007 11:18 pm
by zikitrake
Hi, I was using the original RunAs of a forums user: http://www.purebasic.fr/english/viewtop ... 6&start=15,

but these code don't accepts parameters, so I modified it to accept params :D here is:

Code: Select all

Procedure Runas2(Username.s, Domain.s, Password.s, program.s, parameters.s)
  
  program = program + " " + Chr(34) + parameters + Chr(34)
  
  *lpUserName = AllocateMemory(1000)
  *lpDomainName = AllocateMemory(1000)
  *lpPassword = AllocateMemory(1000)
  *lpApplication = AllocateMemory(1000)
  *lpCommandLine = AllocateMemory(1000)
  lpProcessInfo.PROCESS_INFORMATION
  lpStartUpInfo.STARTUPINFO
  ;convert ansi strings to unicode
  MultiByteToWideChar_(#CP_ACP, 0, Username, -1, *lpUserName, 1000)
  MultiByteToWideChar_(#CP_ACP, 0, Domain, -1, *lpDomainName, 1000)
  MultiByteToWideChar_(#CP_ACP, 0, Password, -1, *lpPassword, 1000)
  MultiByteToWideChar_(#CP_ACP, 0, program, -1, *lpApplication, 1000)
  
  If OpenLibrary(0, "ADVAPI32.DLL")
    *F = GetFunction(0, "CreateProcessWithLogonW")
    If *F
      If CallFunctionFast(*F, *lpUserName, *lpDomainName, *lpPassword, 0, 0,*lpApplication,0,0,0,@lpStartUpInfo,@lpProcessInfo) = 0
        Buffer.s = Space(200)
        LastError.l = GetLastError_()
        FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, LastError, #LANG_NEUTRAL, @Buffer, 200, 0)
        MessageRequester("Error",Str(LastError)+" "+Buffer,0)
      EndIf
    Else
      MessageRequester("","Sorry - This function is not available.",0) 
    EndIf
    CloseLibrary(0)
  EndIf
  
EndProcedure

;Example

username.s = "zikitrake"
domain.s = "." ; . for local user
pwd.s = "tarari"

Runas2(username, domain, pwd, "YourProgram.EXE" , "param1 param2 param3") 

Bye!

Re: RunAs program with parameters!

Posted: Thu Dec 03, 2009 4:15 pm
by Haruks
Thank you! it's very usefull for me!!
one question: how can i run it in HIDE mode?

-Haruks