Partager via


FindFile, exemple

Cet exemple montre comment passer une structure qui contient une seconde structure incorporée vers une fonction non managée. Il montre également comment utiliser l'attribut MarshalAsAttribute pour déclarer un tableau de longueur fixe au sein de la structure. Dans cet exemple, les éléments de structure incorporés sont ajoutés à la structure parente. Pour obtenir un exemple de structure incorporée non aplatie, consultez Structures, exemple.

L'exemple FindFile utilise la fonction non managée suivante, illustrée avec sa déclaration de fonction d'origine :

  • FindFirstFile exportée à partir de Kernel32.dll.

    HANDLE FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData);
    

La structure d'origine passée vers la fonction contient les éléments suivants :

typedef struct _WIN32_FIND_DATA 
{
  DWORD    dwFileAttributes; 
  FILETIME ftCreationTime; 
  FILETIME ftLastAccessTime; 
  FILETIME ftLastWriteTime; 
  DWORD    nFileSizeHigh; 
  DWORD    nFileSizeLow; 
  DWORD    dwReserved0; 
  DWORD    dwReserved1; 
  TCHAR    cFileName[ MAX_PATH ]; 
  TCHAR    cAlternateFileName[ 14 ]; 
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;

Dans cet exemple, la classe FindData contient des données membres correspondantes pour chaque élément de la structure d'origine et de la structure incorporée. À la place des deux mémoires tampons de caractères d'origine, la classe substitue des chaînes. MarshalAsAttribute affecte à l'énumération UnmanagedType la valeur ByValTStr, qui est utilisée pour identifier les tableaux de caractères de longueur fixe inline qui apparaissent au sein des structures non managées.

La classe LibWrap contient un prototype managé de la méthode FindFirstFile qui passe la classe FindData comme paramètre. Le paramètre doit être déclaré à l'aide des attributs InAttribute et OutAttribute car les classes, qui sont des types référence, sont passées comme paramètres en entrée par défaut.

Déclaration de prototypes

' Declares a class member for each structure element.
< StructLayout(LayoutKind.Sequential, CharSet := CharSet.Auto)> _
Public Class FindData
    Public fileAttributes As Integer = 0
    ' creationTime was a by-value FILETIME structure.
    Public creationTime_lowDateTime As Integer = 0
    Public creationTime_highDateTime As Integer = 0
    ' lastAccessTime was a by-value FILETIME structure.
    Public lastAccessTime_lowDateTime As Integer = 0
    Public lastAccessTime_highDateTime As Integer = 0
    ' lastWriteTime was a by-value FILETIME structure.
    Public lastWriteTime_lowDateTime As Integer = 0
    Public lastWriteTime_highDateTime As Integer = 0
    Public nFileSizeHigh As Integer = 0
    Public nFileSizeLow As Integer = 0
    Public dwReserved0 As Integer = 0
    Public dwReserved1 As Integer = 0
    < MarshalAs(UnmanagedType.ByValTStr, SizeConst := 260)> _
    Public fileName As String = Nothing
    < MarshalAs(UnmanagedType.ByValTStr, SizeConst := 14)> _
    Public alternateFileName As String = Nothing
End Class 'FindData

Public Class LibWrap
   ' Declares a managed prototype for the unmanaged function.
   Declare Auto Function FindFirstFile Lib "Kernel32.dll" _
      (ByVal fileName As String, <[In], Out> ByVal findFileData As _
      FindData) As IntPtr
End Class
// Declares a class member for each structure element.
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class FindData
{
    public int  fileAttributes = 0;
    // creationTime was an embedded FILETIME structure.
    public int  creationTime_lowDateTime = 0 ;
    public int  creationTime_highDateTime = 0;
    // lastAccessTime was an embedded FILETIME structure.
    public int  lastAccessTime_lowDateTime = 0;
    public int  lastAccessTime_highDateTime = 0;
    // lastWriteTime was an embedded FILETIME structure.
    public int  lastWriteTime_lowDateTime = 0;
    public int  lastWriteTime_highDateTime = 0;
    public int  nFileSizeHigh = 0;
    public int  nFileSizeLow = 0;
    public int  dwReserved0 = 0;
    public int  dwReserved1 = 0;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
    public String  fileName = null;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=14)]
    public String  alternateFileName = null;
}

public class LibWrap
{
    // Declares a managed prototype for the unmanaged function.
    [DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr FindFirstFile(string fileName, [In, Out]
        FindData findFileData);
}
// Declares a class member for each structure element.
[StructLayout(LayoutKind::Sequential, CharSet=CharSet::Auto)]
public ref class FindData
{
public:
    int  fileAttributes;
    // creationTime was an embedded FILETIME structure.
    int  creationTime_lowDateTime;
    int  creationTime_highDateTime;
    // lastAccessTime was an embedded FILETIME structure.
    int  lastAccessTime_lowDateTime;
    int  lastAccessTime_highDateTime;
    // lastWriteTime was an embedded FILETIME structure.
    int  lastWriteTime_lowDateTime;
    int  lastWriteTime_highDateTime;
    int  nFileSizeHigh;
    int  nFileSizeLow;
    int  dwReserved0;
    int  dwReserved1;
    [MarshalAs(UnmanagedType::ByValTStr, SizeConst=260)]
    String^  fileName;
    [MarshalAs(UnmanagedType::ByValTStr, SizeConst=14)]
    String^  alternateFileName;
};

public ref class LibWrap
{
public:
    // Declares a managed prototype for the unmanaged function.
    [DllImport("Kernel32.dll", CharSet=CharSet::Auto)]
    static IntPtr FindFirstFile(String^ fileName, [In, Out]
        FindData^ findFileData);
};

Fonctions d'appel

Public Class App
    Public Shared Sub Main()
        Dim fd As New FindData()
        Dim handle As IntPtr = LibWrap.FindFirstFile("C:\*.*", fd)
        Console.WriteLine("The first file: {0}", fd.fileName)
    End Sub
End Class
public class App
{
    public static void Main()
    {
        FindData fd = new FindData();
        IntPtr handle = LibWrap.FindFirstFile("C:\\*.*", fd);
        Console.WriteLine("The first file: {0}", fd.fileName);
    }
}
public ref class App
{
public:
    static void Main()
    {
        FindData^ fd = gcnew FindData();
        IntPtr handle = LibWrap::FindFirstFile("C:\\*.*", fd);
        Console::WriteLine("The first file: {0}", fd->fileName);
    }
};

Voir aussi

Concepts

Marshaling de classes, de structures, et d'unions

Types de données d'appel de code non managé

Création de prototypes dans du code managé