Page 1 sur 1

RUNAS avec Parametres

Publié : mer. 16/févr./2005 19:24
par Droopy
J'ai trouvé ce code sur le Forum Anglais

Il fonctionne correctement mais renvoie une erreur quand je tente
de lancer une commande avec paramètre ( CommandLine)
exemple "C:\winnt\notepad.exe c:\test.txt"

Code : Tout sélectionner

*lpUserName = AllocateMemory(0, 1000, 0)
*lpDomainName = AllocateMemory(1, 1000, 0)
*lpPassword = AllocateMemory(2, 1000, 0)
*lpApplication = AllocateMemory(3, 1000, 0)
*lpCommandLine = AllocateMemory(4, 1000, 0)
lpProcessInfo.PROCESS_INFORMATION
lpStartUpInfo.STARTUPINFO
;convert ansi strings to unicode
MultiByteToWideChar_(#CP_ACP, 0, "UserName", -1, *lpUserName, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "DomainOrLocalMachine", -1, *lpDomainName, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "Password", -1, *lpPassword, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "CommandLine", -1, *lpApplication, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "", -1, *lpCommandLine, 1000)

If OpenLibrary(0, "ADVAPI32.DLL")
  *F = IsFunction(0, "CreateProcessWithLogonW")
  If *F
    If CallFunctionFast(*F, *lpUserName, *lpDomainName, *lpPassword, 0, *lpApplication,*lpCommandLine,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
Je ne maitrise pas du tout l'API Win32
Si quelqu'un pouvais m'aider
merci

Publié : mer. 16/févr./2005 20:09
par Droopy
désolé il faut enlever les 0 dans allocatememory
*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, "DomainOrLocalMachine", -1, *lpDomainName, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "Password", -1, *lpPassword, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "CommandLine", -1, *lpApplication, 1000)
MultiByteToWideChar_(#CP_ACP, 0, "", -1, *lpCommandLine, 1000)

If OpenLibrary(0, "ADVAPI32.DLL")
*F = IsFunction(0, "CreateProcessWithLogonW")
If *F
If CallFunctionFast(*F, *lpUserName, *lpDomainName, *lpPassword, 0, *lpApplication,*lpCommandLine,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

Publié : mer. 16/févr./2005 21:19
par Droopy
J'ai trouvé sur le site de MS

Code : Tout sélectionner


Procedure runas(Username.s,Domain.s,Password.s,CommandLine.s,Argument.s)
  FlagErreur=0
  
  *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
  
  ; UserName
  MultiByteToWideChar_(#CP_ACP, 0, Username, -1, *lpUserName, 1000)
  
  ; DomainOrLocalMachine
  MultiByteToWideChar_(#CP_ACP, 0, Domain, -1, *lpDomainName, 1000)
  
  ; Password
  MultiByteToWideChar_(#CP_ACP, 0, Password, -1, *lpPassword, 1000)
  
  ; Application a lancer
  MultiByteToWideChar_(#CP_ACP, 0, CommandLine, -1, *lpApplication, 1000)
  
  ; Arguments 
  If Argument<>"" And Left(Argument,1)<>" " : Argument=" "+Argument:EndIf ; ajout espace
  MultiByteToWideChar_(#CP_ACP, 0, Argument, -1, *lpCommandLine, 1000)
  
  If OpenLibrary(0, "ADVAPI32.DLL")
    *F = IsFunction(0, "CreateProcessWithLogonW")
    If *F
      If CallFunctionFast(*F, *lpUserName, *lpDomainName, *lpPassword, 0, *lpApplication,*lpCommandLine,0,0,0,@lpStartUpInfo,@lpProcessInfo) = 0
        FlagErreur=1
      EndIf
    Else
      FlagErreur=1
    EndIf
    CloseLibrary(0)
  EndIf
  
  ProcedureReturn FlagErreur
   
EndProcedure


If runas("utilisateur",".","password","c:\windows\system32\cmd.exe","/k dir c:\ /W"): MessageRequester("Runas","Erreur"):EndIf