Bonjour à tous,

Je suis en train de developper une application en vb.net qui utilise les api du stockage amovible (ntmsapi.dll). pour une enumeration des objets du stockage amovible, je dois appeler une premiere fois l'api enumeratentmsobject avec un pointeur null, puis ensuite avec un pointeur sur une structure Guid.

Quand je passe intptr.zero l'api marche au 1er appel, mais je ne peux utiliser la declaration pour passer un pointeur sur la structure au second appel

Quand je passe nothing en pensant passer un pointeur null , j'ai l'erreur 4312 au 1er appel



Vous avez le code joint ....

Où est l'erreur ?

Comment passer un pointeur null ?

ou comment declarer l'api sur la fonction ?

Merci à tous



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Imports System.Runtime.InteropServices
'**********************************************************************************************
' changer la chaine de la version pour executer avec la 2ème version de l'API qui pose problème
'**********************************************************************************************
#Const version = "V1"
 
Public Class Form1
 
    Public Enum NtmsObjectsTypes
        NTMS_UNKNOWN = 0
        NTMS_OBJECT
        NTMS_CHANGER
        NTMS_CHANGER_TYPE
        NTMS_COMPUTER
        NTMS_DRIVE
        NTMS_DRIVE_TYPE
        NTMS_IEDOOR
        NTMS_IEPORT
        NTMS_LIBRARY
        NTMS_LIBREQUEST
        NTMS_LOGICAL_MEDIA
        NTMS_MEDIA_POOL
        NTMS_MEDIA_TYPE
        NTMS_PARTITION
        NTMS_PHYSICAL_MEDIA
        NTMS_STORAGESLOT
        NTMS_OPREQUEST
        NTMS_NUMBER_OF_OBJECT_TYPES
    End Enum
 
    <StructLayout(LayoutKind.Explicit, Size:=16)> _
    Public Structure NTMS_GUID
        <FieldOffset(0)> Public Data1 As UInt32
        <FieldOffset(4)> Public Data2 As UShort
        <FieldOffset(6)> Public Data3 As UShort
        <FieldOffset(8)> Public Data41 As Byte
        <FieldOffset(9)> Public Data42 As Byte
        <FieldOffset(10)> Public Data43 As Byte
        <FieldOffset(11)> Public Data44 As Byte
        <FieldOffset(12)> Public Data45 As Byte
        <FieldOffset(13)> Public Data46 As Byte
        <FieldOffset(14)> Public Data47 As Byte
        <FieldOffset(15)> Public Data48 As Byte
    End Structure ' NTMS_GUID
 
    Declare Ansi Function OpenNtmsSessionA Lib "ntmsapi.dll" (<MarshalAs(UnmanagedType.LPStr)> ByVal LpServer As String, _
                                                              <MarshalAs(UnmanagedType.LPStr)> ByVal LpApp As String, _
                                                              ByVal dwOptions As Integer) As Integer
 
    ' 2 versions d'appel différents pour la même API
    ' la version "V1" permet de passer un pointeur null à l'API
    ' la version "V2" passe un pointeur sur une structure
#If Version = "V1" Then
    Declare Function EnumerateNtmsObject Lib "ntmsapi.dll" (ByVal hSession As Integer, _
                                                               ByVal lpContainerId As IntPtr, _
                                                               ByRef lpList As NTMS_GUID, _
                                                               ByRef lpdwListSize As Integer, _
                                                               ByVal dwType As Integer, _
                                                               ByVal dwOptions As Integer) As Integer
#Else
    Declare Function EnumerateNtmsObject Lib "ntmsapi.dll" (ByVal hSession As Integer, _
                                                               ByRef lpContainerId As NTMS_GUID, _
                                                               ByRef lpList As NTMS_GUID, _
                                                               ByRef lpdwListSize As Integer, _
                                                               ByVal dwType As Integer, _
                                                               ByVal dwOptions As Integer) As Integer
#End If
 
    Declare Function CloseNtmsSession Lib "ntmsapi.dll" (ByVal hSession As Integer) As Integer
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim G1 As New NTMS_GUID
        Dim Hdle As Integer, NbObjet As Integer, ResultAPI As Integer
        Dim T() As NTMS_GUID
 
 
        NbObjet = 10
        Hdle = 0
        Hdle = OpenNtmsSessionA(Nothing, Nothing, 0)
 
        If Hdle = -1 Then
            MsgBox("service stockage amovible non démarré !", MsgBoxStyle.Critical, "Stop")
            Exit Sub
        End If
 
        MsgBox("Handle OpenNtmsSession = " & Hdle)
        ReDim T(NbObjet - 1)
 
        '***********************************************************************
        ' il semblerait que nothing en tant que variable passée en paramètre par référence 
        ' ne soit pas un pointeur NULL
#If Version = "V1" Then
        Dim P As IntPtr
        P = IntPtr.Zero
        ResultAPI = EnumerateNtmsObject(Hdle, P, T(0), NbObjet, NtmsObjectsTypes.NTMS_LIBRARY, 0)
#Else
        ResultAPI = EnumerateNtmsObject(Hdle, Nothing, T(0), NbObjet, NtmsObjectsTypes.NTMS_LIBRARY, 0)
#End If
        '************************************************************************
 
        If ResultAPI = 0 Then
            MsgBox("resultat EnumerateNtmsObject OK !")
        Else
            MsgBox("resultat EnumerateNtmsObject = " & ResultAPI)
        End If
 
        ResultAPI = CloseNtmsSession(Hdle)
 
    End Sub
 
 
End Class