befadudesert
Messages postés17Date d'inscriptionjeudi 22 février 2007StatutMembreDernière intervention28 janvier 2008
-
24 sept. 2007 à 16:43
befadudesert
Messages postés17Date d'inscriptionjeudi 22 février 2007StatutMembreDernière intervention28 janvier 2008
-
25 sept. 2007 à 10:26
Bonjour,
j'essaye de faire un proramme avec une variable globale.
J'ai essayé de la déclarer avant toutes procédures ou fonctions mais "type mismatch"
J'ai ensuite essayé de la passer en paramètre de la fonction par référence mais ça ne marche toujours pas.
Pourtant lorsque je fais des petits tests ca fontionne.
Voici deux exemples qui fonctionnent pourtant le second ne devrait pas fonctionner.
Premier exemple :
chaine1 = "première chaine"
chaine2 = "deuxième chaine"
Sub affichage(tab)
tab = tab & " !!!"
Wscript.echo tab
End Sub
affichage chaine1
affichage chaine2
Deuxième exemple :
chaine1 = "première chaine"
chaine2 = "deuxième chaine"
Sub affichage(byref tab)
tab = tab & " !!!"
Wscript.echo tab
End Sub
cs_casy
Messages postés7741Date d'inscriptionmercredi 1 septembre 2004StatutMembreDernière intervention24 septembre 201440 24 sept. 2007 à 17:13
Les 2 cas devraient marcher, et ça n'a rien à voir avec ByRef.
Dans le cas ou la variable est passée comme ByVal, toute modification interne à la fonction sur la variable ne sortira pas de la fonction puisque effectuée sur une copie de la variable originale. Donc par exemple Chaine1 après appel = Chaine 1 avant appel.
Dans le cas ou la variable est passée comme ByRef, toute modification interne à la fonction sur la variable est immédiatement reportée sur la variable originale puisque c'est bien celle-ci (plus exactement son adresse mémoire) qui est réellement passé à la fonction. Donc dans ce cas là, Chaine 1 après appel est différent de Chaine 1 avant appel.
Par contre dans ton code tu ne type pas tes variables, et ton problème vient surrement de là. En VB6 toute variable non typée est automatiquement typée comme Variant, type entre-autre gros consomateur de mémoire.
Prend l'habitude de typer correctement toutes tes variables
Kristof_Koder
Messages postés918Date d'inscriptionvendredi 3 août 2007StatutMembreDernière intervention27 octobre 200810 24 sept. 2007 à 22:38
D'accord avec les explications de Casy, sauf pour le typage ! Regardes bien Casy, il semble que ce soit du VBScript !
BefaDuDesert < Tes deux exemples fonctionnent en effet correctement. Enfin, tout dépend de ce que tu penses qu'il devrait faire ?!
Pour ton problème à la base, j'avoue ne pas bien voir quel est ton problème ? "Type Mismatch" !! Etrange ! Donne ous donc copie de ton script, on verra ce qui cloche ?
befadudesert
Messages postés17Date d'inscriptionjeudi 22 février 2007StatutMembreDernière intervention28 janvier 2008 25 sept. 2007 à 10:23
'liste des variables globales
cheminFichierDroits = "C:\tst.txt"
ligne = nbLignesDansFichier (cheminFichierDroits)
Dim tab()
Redim tab(ligne,8)
Dim ligneCourante
ligneCourante = -1
Function nbLignesDansFichier(cheminFichier)
'déclaration file system object
Dim FSO
'instanciation
Set FSO = CreateObject("Scripting.FileSystemObject")
'on instance le fichier texte
Set Ftxt = FSO.OpenTextFile(cheminFichier)
Dim cpt
cpt = 0
'on parcours chaque ligne du fichier texte
Do While Not Ftxt.AtEndOfStream
MaVariable = Ftxt.Readline
if MaVariable <> "" Then
cpt=cpt+1
traitementLigne MaVariable,tab
End If
Loop
nbLignesDansFichier = cpt
Ftxt.Close
End Function
'_________________________LECTURE d'un fichier_____________________________
'déclaration file system object
Dim FSO
'instanciation
Set FSO = CreateObject("Scripting.FileSystemObject")
'on instancie le fichier texte
Set Ftxt = FSO.OpenTextFile(cheminFichierDroits)
'on parcours chaque ligne du fichier texte
Do While Not Ftxt.AtEndOfStream
MaVariable = Ftxt.Readline
if MaVariable <> "" Then
traitementLigne MaVariable, tab
End If
Loop
Ftxt.Close
'__________________________Traitement de chaque ligne_________________________
Sub traitementLigne(ligne, byref tableau)
'si ligne normale
WScript.echo tableau(0,0)
If left(ligne,1) <> " " Then
'on passe à une nouvelle ligne
ligneCourante = ligneCourante + 1
chemin = motChoisi(ligne," ",1)
cheminRelatif = "\" + NomMachine + right(chemin, len(chemin)-2)
tab(ligneCourante,0) = cheminRelatif
repertoireCourant = cheminRelatif
'on crée une nouvelle ligne
'on remplit la premiere ligne à savoir le chemin relatif
reste = right(ligne,len(ligne)-len(cheminRelatif)-1)
util = motChoisi(motChoisi(reste,"",2),":",1)
'on remplit la 2è colonne
tab(ligneCourante,1) = util
domaineEtUtil=motChoisi(reste,":",1)
droits = right(reste,len(reste)-len(domaineEtUtil)-1)
'On s'occupe maintenant des droits
If Instr(droits,"(CI)") <> 0 OR Instr(droits,"(IO)") <> 0 OR Instr(droits,"(OI)") <> 0 Then
tab(ligneCourante,2) = "X"
End If
If Instr(droits,"F") <> 0 Then
tableau(ligneCourante,3) = "X"
tableau(ligneCourante,4) = "X"
tableau(ligneCourante,5) = "X"
tableau(ligneCourante,6) = "X"
tableau(ligneCourante,7) = "X"
Else If Instr(droits,"C") <> 0 Then
tableau(ligneCourante,3) = "X"
tableau(ligneCourante,4) = "X"
tableau(ligneCourante,5) = "X"
tableau(ligneCourante,6) = "X"
Else If Instr(droits,"(accŠs sp‚cialÿ") <> 0 Then
WScript.echo "ligne suivante"
End If
End If
End If
'pas ligne normale donc soit ligne utilisateur sup. soit ligne de droits
Else
'dans ce cas la nous sommes dans le cas d'une ligne d'un nouvel utilisateur
If Instr(ligne,"") <> 0 OR Instr(ligne,"Tout le monde") <> 0 OR Instr(ligne,"CREATEUR PROPRIETAIRE") <> 0 Then
WScript.echo "ligneCourante " & ligneCourante
If ligneCourante <> 0 Then
tableau(ligneCourante,0) = tableau(ligneCourante-1,0)
End If
ligne=sansEspaces(ligne)
'on vérifie les utilisateurs spéciaux
If Instr(ligne,"Tout le monde") <> 0 Then
util = "Tout le monde"
Else If Instr(ligne,"CREATEUR PROPRIETAIRE") <> 0 Then
util = "CREATEUR PROPRIETAIRE"
Else util=motChoisi(motChoisi(ligne,"",2),":",1)
End If
End If
tab(ligneCourante,1) = util
domaineEtUtil=motChoisi(reste,":",1)
droits = motChoisi(ligne,":",2)
'heritage
If Instr(droits,"(CI)") <> 0 OR Instr(droits,"(IO)") <> 0 OR Instr(droits,"(OI)") <> 0 Then
tableau(ligneCourante,2) = "X"
End If
If Instr(ligne,"F") <> 0 Then
tableau(ligneCourante,3) = "X"
tableau(ligneCourante,4) = "X"
tableau(ligneCourante,5) = "X"
tableau(ligneCourante,6) = "X"
tableau(ligneCourante,7) = "X"
Else
If Instr(ligne,"C") <> 0 Then
tableau(ligneCourante,3) = "X"
tableau(ligneCourante,4) = "X"
tableau(ligneCourante,5) = "X"
tableau(ligneCourante,6) = "X"
Else
If Instr(ligne,"(accŠs sp‚cialÿ") <> 0 Then
'il faut analyser les lignes suivantes afin de connaitre les droits de cet utilisateur
WScript.echo "cas special"
End If
End If
End If
'ligne de droits
Else
'ici nous sommes dans les cas d'une ligne de droit pure
ligneM=sansEspaces(ligne)
If ligneM = "" Then
If ligneM = "READ_CONTROL" Then
tableau(ligneCourante,3) = "X"
Else
If ligneM = "FILE_WRITE_DATA" Then
tableau(ligneCourante,4) = "X"
Else
If ligneM = "DELETE" Then
tableau(ligneCourante,5) = "X"
Else
If ligneM = "FILE_EXECUTE" Then
tableau(ligneCourante,6) = "X"
Else
If ligneM = "WRITE_OWNER" Then
tableau(ligneCourante,7) = "X"
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
Function motChoisi(chaine,separateur,numMot)
chaineU=chaine
cpt = 1
nb = -1
Do While nb <> 0
'nb renvoie le premier caractère " " trouvé dans la chaine
nb=Instr(chaineU,separateur)
'si aucun espace a été trouvé
If nb <> 0 Then
'un espace est trouvé comme mot
If nb = 1 Then
cpt = cpt-1
chaineU = right(chaineU, len(chaineU)-1)
Else
mot_courant = left(chaineU, nb-1)
If numMot=cpt Then
motChoisi=mot_courant
End If
End If
chaineU=right(chaineU, len(chaineU) - nb+1)
cpt = cpt+1
Else
If numMot=cpt Then
motChoisi=chaineU
End If
End If
Loop
End Function
Function sansEspaces(mot)
Do While left(mot,1) = " "
mot=right(mot,len(mot)-1)
Loop
sansEspaces=mot
End Function
Sub affichage (byref tab)
ligneTemp = 0
colonneTemp = 0
Do While ligneTemp < ligne
Do While colonneTemp < 8
WScript.echo tab(ligneTemp, colonneTemp)
colonneTemp = colonneTemp + 1
Loop
ligneTemp = ligneTemp + 1
colonneTemp = 0
Loop
End Sub
Avec le C:\tst.txt,
E:\ BUILTIN\Administrateurs:(OI)(CI)F
AUTORITE NT\SYSTEM:(OI)(CI)C
befadudesert
Messages postés17Date d'inscriptionjeudi 22 février 2007StatutMembreDernière intervention28 janvier 2008 25 sept. 2007 à 10:26
En effet je m'étais planté sur mes exemples les voici corrigés à tête reposé. En effet se prendre pendant une journée ça n'aide pas à avoir les idées clairs ^^.
Premier exemple
chaine1 = "premiere chaine"
chaine2 = "deuxième chaine"
Function modification(tab)
tab = tab & " !!!"
affichage = tab
End Function