Passer de VB à VBScript

Résolu
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006 - 4 août 2006 à 15:06
Dolphin Boy Messages postés 630 Date d'inscription vendredi 5 mai 2006 Statut Membre Dernière intervention 17 février 2007 - 4 août 2006 à 18:37
Bonjour,

J'ai le code ci-dessous qui fonctionne très bien avec VB (fonction sans interface, pour la lancer sur un serveur), mais je n'arrive pas à exécuter l'exe sur mon serveur.
J'aurai don besoin de faire la même chose en VBScript, mais je ne sais pas du tout comment faire...
Mon but est de pouvoir appeler mon script depuis une dll écrite en C++ et tournant sur mon serveur.
MErci de de votre aide !

Pour info, le programme VB permet de modifier le propriétaire d'un fichier physique.

Merci d'avance pour votre aide.

Oliv.


Option Explicit


' Global constants we must use with security descriptor
Private Const SECURITY_DESCRIPTOR_REVISION = 1
Private Const OWNER_SECURITY_INFORMATION = 1&


' Access Token constants
Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = &H2
Private Const TOKEN_IMPERSONATE = &H4
Private Const TOKEN_QUERY = &H8
Private Const TOKEN_QUERY_SOURCE = &H10
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_ADJUST_GROUPS = &H40
Private Const TOKEN_ADJUST_DEFAULT = &H80
Private Const TOKEN_ALL_ACCESS = TOKEN_ASSIGN_PRIMARY _
+ TOKEN_DUPLICATE + TOKEN_IMPERSONATE + TOKEN_QUERY _
+ TOKEN_QUERY_SOURCE + TOKEN_ADJUST_PRIVILEGES _
+ TOKEN_ADJUST_GROUPS + TOKEN_ADJUST_DEFAULT
Private Const ANYSIZE_ARRAY = 1


' Token Privileges constants
Private Const SE_RESTORE_NAME = "SeRestorePrivilege"
Private Const SE_PRIVILEGE_ENABLED = 2&


' ACL structure
Private Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type


Private Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As ACL
Dacl As ACL
End Type


' Token structures
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type


Private Type LUID
lowpart As Long
highpart As Long
End Type


Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type


Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type


' Win32 API calls
Private Declare Function LookupAccountName Lib "advapi32.dll" _
Alias "LookupAccountNameA" (ByVal lpSystemName As String, _
ByVal lpAccountName As String, Sid As Byte, cbSid As Long, _
ByVal ReferencedDomainName As String, _
cbReferencedDomainName As Long, peUse As Integer) As Long


Private Declare Function InitializeSecurityDescriptor _
Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, _
ByVal dwRevision As Long) As Long


Private Declare Function SetSecurityDescriptorOwner _
Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, _
pOwner As Any, ByVal bOwnerDefaulted As Long) As Long


Private Declare Function SetFileSecurity Lib "advapi32.dll" _
Alias "SetFileSecurityA" (ByVal lpFileName As String, _
ByVal SecurityInformation As Long, _
pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long


Private Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long


Private Declare Function GetCurrentProcess Lib "kernel32" () As Long


Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" _
Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long


Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" _
(ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long


Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long


Public Function ChangeOwnerOfFile(FileName As String, _
OwnerAccountName As String)


' variables for the LookupAccountName API Call
Dim Sid(255) As Byte ' Buffer for the SID
Dim nBufferSize As Long ' Length of SID Buffer
Dim szDomainName As String * 255 ' Domain Name Buffer
Dim nDomain As Long ' Length of Domain Name buffer
Dim peUse As Integer ' SID type
Dim Result As Long ' Return value of Win32 API call


' variables for the InitializeSecurityDescriptor API Call
Dim SecDesc As SECURITY_DESCRIPTOR
Dim Revision As Long


Enable_Privilege (SE_RESTORE_NAME)


nBufferSize = 255
nDomain = 255


Result = LookupAccountName(vbNullString, OwnerAccountName, _
Sid(0), nBufferSize, szDomainName, nDomain, peUse)
If (Result = 0) Then
'MsgBox "LookupAccountName failed with error code " _
'& Err.LastDllError
Exit Function
End If


Result = InitializeSecurityDescriptor(SecDesc, _
SECURITY_DESCRIPTOR_REVISION)
If (Result = 0) Then
'MsgBox "InitializeSecurityDescriptor failed with error code " _
'& Err.LastDllError
Exit Function
End If


Result = SetSecurityDescriptorOwner(SecDesc, Sid(0), 0)
If (Result = 0) Then
'MsgBox "SetSecurityDescriptorOwner failed with error code " _
'& Err.LastDllError
Exit Function
End If


Result = SetFileSecurity(FileName, OWNER_SECURITY_INFORMATION, _
SecDesc)
If (Result = 0) Then
'MsgBox "SetFileSecurity failed with error code " _
'& Err.LastDllError
Exit Function
Else
'MsgBox "Owner of " & FileName & " changed to " _
'& OwnerAccountName
End If


Disable_Privilege (SE_RESTORE_NAME)


End Function


Public Function Enable_Privilege(Privilege As String) As Boolean
Enable_Privilege = ModifyState(Privilege, True)
End Function


Public Function Disable_Privilege(Privilege As String) As Boolean
Disable_Privilege = ModifyState(Privilege, False)
End Function


Public Function ModifyState(Privilege As String, _
Enable As Boolean) As Boolean


Dim MyPrives As TOKEN_PRIVILEGES
Dim PrivilegeId As LUID
Dim ptrPriv As Long ' Pointer to Privileges Structure
Dim hToken As Long ' Token Handle
Dim Result As Long ' Return Value


Result = OpenProcessToken(GetCurrentProcess(), _
TOKEN_ADJUST_PRIVILEGES, hToken)
If (Result = 0) Then
ModifyState = False
'MsgBox "OpenProcessToken failed with error code " _
'& Err.LastDllError
Exit Function
End If


Result = LookupPrivilegeValue(vbNullString, Privilege, PrivilegeId)
If (Result = 0) Then
ModifyState = False
'MsgBox "LookupPrivilegeValue failed with error code " _
'& Err.LastDllError
Exit Function
End If


MyPrives.Privileges(0).pLuid = PrivilegeId
MyPrives.PrivilegeCount = 1
If (Enable) Then
MyPrives.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
Else
MyPrives.Privileges(0).Attributes = 0
End If


Result = AdjustTokenPrivileges(hToken, False, MyPrives, 0, 0, 0)
If (Result = 0 Or Err.LastDllError <> 0) Then
ModifyState = False
'MsgBox "AdjustTokenPrivileges failed with error code " _
'& Err.LastDllError
Exit Function
End If


CloseHandle hToken


ModifyState = True


End Function




Sub Main()
    Dim Fname As String
    Dim User As String
    Dim rc As Boolean
        
    Dim cmdLine() As String
    Dim tmpArg As Variant
    Dim i As Integer
    i = 1
cmdLine() = Split(Command, ",")
For Each tmpArg In cmdLine()
'MsgBox tmpArg
If i = 1 Then
    Fname = tmpArg
Else
    User = tmpArg
End If
i = i + 1
Next




rc = ChangeOwnerOfFile(Fname, User)
End
End Sub


 

13 réponses

Dolphin Boy Messages postés 630 Date d'inscription vendredi 5 mai 2006 Statut Membre Dernière intervention 17 février 2007
4 août 2006 à 18:37
Tout à fait normal s'il posséde les dll, ocx, runtime,... nécessaires. Contrairement sans doute aux autres postes d'où tu voudrais le faire exécuter.
Comprenons nous bien, un prog (exécutable) est tributaire de la machine à partir de laquelle on le lance. Pas de la machine où il se trouve.
Dans certains cas, il suffit de mettre les dll, ... dans le même dossier (répertoire, directory) que l'exécutable sinon, il faut faire un package et l'installer sur les machines utilisatrices.
3
Dolphin Boy Messages postés 630 Date d'inscription vendredi 5 mai 2006 Statut Membre Dernière intervention 17 février 2007
4 août 2006 à 15:30
Salut, as-tu fait une installation sur la machine qui doit exécuter ton exe ?
0
cs_darunia Messages postés 354 Date d'inscription mercredi 18 décembre 2002 Statut Membre Dernière intervention 24 mars 2011 2
4 août 2006 à 15:31
Conseil : traduit le C++ !

A ma connaissance, y a pas moyen d'exploiter le win32 en VBS

D@runia
0
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006
4 août 2006 à 15:42
Non, pas d'installation de faite... je poste simplement mon exe dans un répertoire.


 


Pour le C++, j'ai pas réussi à le faire... il me plante toujours mon application cliente....


Mais je suis ouvert à toute proposition en C++ !


 


Merci




















    /\_    /\ 
  /      \/    \
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_darunia Messages postés 354 Date d'inscription mercredi 18 décembre 2002 Statut Membre Dernière intervention 24 mars 2011 2
4 août 2006 à 15:48
Il te plante ?!?
Pourtant c'est quasiment que des appels à du Win32, c'est beaucoup plus facile en C++ (enfin je pense

D@runia
0
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006
4 août 2006 à 15:51
Malheureusement je maitrise tellement peu le C++ ...

ma dll est générée automatiquement...
je peux juste y ajouter un script (en c ou en vb, égal)...

mais je me perd un peu.

    /\_    /\ 
  /      \/    \
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
4 août 2006 à 15:52
Salut,

pour info, dans MS Excel, menu Outils, Macro..., VBScript.
Tu as une aide (comme dans Excel) relativement complête. N'hésite pas à t'aider de ce support.

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
cs_darunia Messages postés 354 Date d'inscription mercredi 18 décembre 2002 Statut Membre Dernière intervention 24 mars 2011 2
4 août 2006 à 15:57
1ere question : "
ma dll est générée automatiquement... " Comment ça ?
2e question : pourquoi tu ne fais pas tourner ton exe VB sur le serveur ? (si ça marche pas, il te manque peut etre le runtime vb)

D@runia
0
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006
4 août 2006 à 16:12
c'est une application qui génère le code C++...

J'aimerai bien faire tourner mon exe VB sur le serveur... mais quand je lance le Shell Execute, il me retourne une erreur...

    /\_    /\ 
  /      \/    \
0
Dolphin Boy Messages postés 630 Date d'inscription vendredi 5 mai 2006 Statut Membre Dernière intervention 17 février 2007
4 août 2006 à 16:41
2 autres question :
c'est une application qui génère le code C++ : laquelle ?
il me retourne une erreur : laquelle ?
0
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006
4 août 2006 à 16:46
L'application c'est COOL:PLEX


L'erreur... bonne question...


Moi je vois simplement "ERR"... je ne sais pas comment récupérer l'erreur.
A noter également que je ne passe pas d'Handle, car je n'en ai pas (je suis sur le serveur... aucun interface)


{
#ifdef WIN32
HINSTANCE hinst = ::ShellExecute(NULL,
NULL,
&(1:).GetText(),
&(2:).GetText(),
NULL,
SW_SHOWNORMAL) ;
if (hinst < (HINSTANCE) HINSTANCE_ERROR)
{
v->st_returned=ObCharFld("ERR");
}
else
{
v->st_returned=ObCharFld(" ");
}
#endif
}






 




















    /\_    /\ 
  /      \/    \
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 131
4 août 2006 à 16:57
Heu lancer l'exe vb sans aucune installation, c'est un peu normal que l'exe en question ne marche pas : il faut au moins installer le runtime vb pour pouvoir lancer un exe vb !
0
flyingfish Messages postés 41 Date d'inscription jeudi 19 février 2004 Statut Membre Dernière intervention 4 août 2006
4 août 2006 à 17:03
OK...
Mais si je le lance manuellement (je vais sur le serveur et double click), là il fonctionne...

C'est bien normal ?

    /\_    /\ 
  /      \/    \
0
Rejoignez-nous