RunAs program with parameters!

Developed or developing a new product in PureBasic? Tell the world about it.
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

RunAs program with parameters!

Post 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!
PB 6.21 beta, PureVision User
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: RunAs program with parameters!

Post by Haruks »

Thank you! it's very usefull for me!!
one question: how can i run it in HIDE mode?

-Haruks
=D
Post Reply